Alex
Alex

Reputation: 403

How to Add Facebook like button to iOS application using Webview?

Right now i am struggling with adding Facebook like button to my App.I referred many sites and even developers.Facebook also not provided proper explanation.They just gave frame for like button. Can any one please help in adding Facebook like button in my App using Webview.

Thanks in Advance

Upvotes: 1

Views: 803

Answers (3)

Sergei Nikitin
Sergei Nikitin

Reputation: 788

To get count of Likes for some URL you can use this code:

NSString *likesRequestURL = [[NSString stringWithFormat:@"https://graph.facebook.com/fql?q=SELECT+total_count,+url+FROM+link_stat+WHERE+url+=\"%@\"&access_token=%@", URL, FBSession.activeSession.accessTokenData.accessToken] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:likesRequestURL]
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                       timeoutInterval:60.0f];

    __block NSString *result = [NSString string];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                               if (error == nil) {
                                   NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   SBJsonParser *jsonParser = [[[SBJsonParser alloc] init] autorelease];
                                   result = [NSString stringWithFormat:@"%@", [[[[jsonParser objectWithString:jsonString] objectForKey:@"data"] objectAtIndex:0] objectForKey:@"total_count"]];
                                   [jsonString release];
                               } else {
                                   NSLog(@"Error = %@", error);
                               }
                           }];

Note. In this case was used SBJSONParser, you can use it or replace by something else, just slightly change corresponding code.

Now you can to create fully customized like-button.

Upvotes: 2

Sergei Nikitin
Sergei Nikitin

Reputation: 788

Ok, this is from working project:

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:URL, @"object", nil];

  if (FBSession.activeSession.isOpen) {
    if (FBSession.activeSession.accessTokenData.accessToken) {
      [FBRequestConnection startWithGraphPath:@"/me/og.likes"
                                   parameters:params
                                   HTTPMethod:@"POST"
                            completionHandler:^(FBRequestConnection *connection, id result, NSError *error) { }];
    } else NSLog(@"FBSession.activeSession.accessToken IS NULL");
  }

URL is the URL of object what you want to like. There's no code for completionHandler block and for FB-authentification, I hope you can do it as you need.

Upvotes: 2

Sergei Nikitin
Sergei Nikitin

Reputation: 788

It's not right. There is a way to realize Like feature not with UIWebView bit with FBRequestConnection. If you want to use UIWebView - colleague Shan gives right answer, but if you want to use another way - let me to know.

Upvotes: 0

Related Questions