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

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

Embed Size (px)

Citation preview

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

Integrating Facebook into iOS Apps

08/25/2011North Atlanta iOS Developers Meetup Group Presentation

Page 2: 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)

Page 3: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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)

Page 4: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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

Page 5: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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

Page 6: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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)

Page 7: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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.

Page 8: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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.

Page 9: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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

Page 10: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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;

Page 11: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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.

Page 12: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

Getting Movies of FriendsRequest 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];

Page 13: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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

Page 14: Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation

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