1 / 14

Integrating Facebook into iOS Apps

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.

dyami
Download Presentation

Integrating Facebook into iOS Apps

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

  2. Links / References • Sample App presented (http://files.meetup.com/1774203/MyFriends.zip) • https://github.com/facebook/facebook-ios-sdk (Facebook iOS SDK)

  3. 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)

  4. 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];

  5. 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

  6. 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)

  7. 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.

  8. 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.

  9. 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]; }

  10. 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;

  11. 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.

  12. 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];

  13. 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]; • }

  14. 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"]]; • }

More Related