140 likes | 314 Views
Integrating Facebook into iOS Apps. 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation. Links / References. Sample App presented ( http://files.meetup.com/1774203/MyFriends.zip ) https://github.com/facebook/facebook-ios-sdk (Facebook iOS SDK). Facebook Docs / Links.
E N D
Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation
Links / References • Sample App presented (http://files.meetup.com/1774203/MyFriends.zip) • https://github.com/facebook/facebook-ios-sdk (Facebook iOS SDK)
Facebook Docs / Links • https://developers.facebook.com/docs/ (Main page) • https://developers.facebook.com/docs/guides/mobile/#ios (iOS Example) • http://developers.facebook.com/blog/post/532/ (Another iOS Example) • http://developers.facebook.com/docs/reference/api/permissions/ (Facebook permissions)
Sample App – Getting Friends & Their Movies • Facebook has good documentation, as far as how to setup an iOS application to use the API. Reading links should be enough to get started for Login. The only discrepancy I found, which could be because I downloaded the latest iOS API, is that a delegate is set when creating the Facebook object, and not in the call to authorize. (ie) facebook = [[Facebook alloc] initWithAppId:@"145062165564860" andDelegate:self]; [facebook authorize:permissions];
Sample App • App has a Login button, clicking on it starts the Facebook authorization process. • (myFriendButtonClicked method in MyFriendsViewController class) • If login is successful, fbDidLogin delegate method called • Code stores Access token / Expiration date to allow one to bypass Login later (SSO) • Request is made to get friends
Request Friends • [facebook requestWithGraphPath:@"me/friends" andDelegate:self] • The first parameter in the graph path (me) is for the user logged in. Replacing me with ID of another user, would get that user’s friends. • The second parameter is the request being made (friends)
Request Friends • Delegate method called once request has finished. (void)request:(FBRequest *)request didLoad:(id)result • Result data is in JSON format, and already parsed (looking at Facebook API code – it’s the same framework presented at the last meetup meeting). When determining how to use the data, the first step is to output the data.
Request Friends • A dictionary is returned that contains a single element ‘data’. The ‘data’ element is an Array of Dictionaries (with each Dictionary containing attributes about the Friend) • To display the friends a Table View Controller is created (MyFriendTableViewController) and the Array of Dictionaries passed in as an attribute.
Table view of Friends - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // Return the number of sections. return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { // Return the number of rows in the section. return [friendArray count]; }
Table view of Friends • - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath • { • // Standard Table View Cell creation (removed) // Configure the cell... NSDictionary *friendDict = [friendArray objectAtIndex:indexPath.row]; NSString *friendName = [friendDict objectForKey:@"name"]; cell.textLabel.text = friendName;
Getting Movies of Friends • Created a helper class FacebookFriendVideoRequest that encapsulates the Facebook request. • Clicking on Friend in TableView invokes requestFriendVideo method. An application delegate is passed in (FacebookFriendVideoRequestDelegate), so that the controller knows when the request is done.
Getting Movies of Friends Request to get Movies (Request Path is: FriendID/movies). Need to get Facebook object from App Delegate (unless you pass from View Controller). FB examples have it created in View Controller. Had to add accessor method. (void) requestFriendVideo: (NSDictionary *)friendInfo withDelegate:(id) aDelegate • NSString *friendID = [friendInfo objectForKey:@"id"]; NSString *requestPath = [friendID stringByAppendingString:@"/movies"]; • // get app delegate Facebook *facebook = [ appDelegate getFacebookClass]; [facebook requestWithGraphPath:requestPath andDelegate:self];
Getting Movies of Friends • Implementation of Facebook delegate • - (void)request:(FBRequest *)request didLoad:(id)result { • // Parse results • if ([result isKindOfClass:[NSArray class]]) { • result = [result objectAtIndex:0]; • } • // Log for debugging • NSLog(@"Result of API call: %@", result); • // get dictionary array, callback delegate • NSArray *movieDictionaryArray = [result objectForKey:@"data"]; • [delegate videoRequestFinishedWithData: movieDictionaryArray]; • }
Getting Movies of Friends videoRequestFinishedWithData delegate code (that displays an Alert popup – with string) NSMutableString *favoriteVideos = [[NSMutableString alloc] init]; for (int i=0; i < [friendVideoArray count]; i++) • { • NSDictionary *videoDict = [friendVideoArray objectAtIndex:i]; • NSString *category = [videoDict objectForKey:@"category"]; • if ([category compare:@"Movie"] == NSOrderedSame) • { • // Movie • [favoriteVideos appendFormat:@"%@,", [videoDict objectForKey:@"name"]]; • }