Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations

Presentation on theme: "Integrating Facebook into iOS Apps 08/25/2011 North Atlanta iOS Developers Meetup Group Presentation."— Presentation transcript:

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

2 Links / References Sample App presented ( sdk (Facebook iOS SDK) sdk

3 Facebook Docs / Links (Main page) /mobile/#ios (iOS Example) /mobile/#ios 2/ (Another iOS Example) 2/ ce/api/permissions/ (Facebook permissions) ce/api/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 users 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 – its 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"]]; }

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

Similar presentations

Ads by Google