Adding the Facebook Like Button in an iPhone App
Asked Answered
A

11

25

Does anyone know how I would include a facbeook "like button" in an iphone app. I tried calling the iframe inside of a UIWebView but that doesn't work.

Axum answered 6/5, 2010 at 19:20 Comment(0)
H
13

Check out this nice bit of code: http://angelolloqui.blogspot.com/2010/11/facebook-like-button-on-ios.html

Add the FBLikeButton class to your view:

FBLikeButton *likeButton = [[FBLikeButton alloc] initWithFrame:CGRectMake(0, 372, 320, 44)       
andUrl:@"http://www.facebook.com/pages/De-Zilk/108209735867960"];

Thanks a lot Angel García Olloqui

EDIT: Nice to add... For bonus points:
If you use above method the webpage is not well formatted for an iPhone. What you can do is run some JavaScript code to remove all the disturbing divs. Use this (long sentence):

  [_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"javascript:document.getElementsByClassName('uiButton')[2].style.visibility='hidden';var child1 = document.getElementById('standard_status');var parent1 = document.getElementById('login_form');parent1.removeChild(child1);var child2 = document.getElementById('pageheader');var parent2 = document.getElementById('booklet');parent2.removeChild(child2);document.getElementById('loginform').style.width = '200px';var child3 = document.getElementById('reg_btn_link');var parent3 = document.getElementsByClassName('register_link')[0];parent3.removeChild(child3);var child4 = document.getElementById('signup_area');var parent4 = document.getElementById('login_form');parent4.removeChild(child4);var child5 = document.getElementsByClassName('mbm')[0];var parent5 = document.getElementById('loginform');parent5.removeChild(child5);var child6 = document.getElementsByClassName('reset_password form_row')[0];var parent6 = document.getElementById('loginform');parent6.removeChild(child6);var child7 = document.getElementsByClassName('persistent')[0];var parent7 = document.getElementById('loginform');parent7.removeChild(child7);"]];

You can place it in the delegate method webViewDidFinishLoad from the FBDialog Facebook class.

Helfrich answered 25/9, 2011 at 14:0 Comment(1)
Replaced with FBSDKLikeButton in latest version of FB SDK. Also there is no selector initWithFrame.Diamante
A
2

Fb like Widget can be embedded in our application. You just have to add a webView and get the Fb Like Widget html code/URL here.

in ViewController.h where you want to add fb like button:

#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController <UIWebViewDelegate>

@property (strong, nonatomic) UIWebView * fbLikeWebView;

-(void)embedFBLikeButton;

@end

in TestViewController.m

#import "AboutUsViewController.h"

@implementation AboutUsViewController

@synthesize fbLikeWebView = _fbLikeWebView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Add this code for FbLike Webview

    self.fbLikeWebView = [[UIWebView alloc] initWithFrame: CGRectMake(100.0, 50.0, 55.0, 70.0)];
    _fbLikeWebView.opaque = NO;
    _fbLikeWebView.backgroundColor = [UIColor clearColor];
    _fbLikeWebView.delegate = self;
    [self.view addSubview:_fbLikeWebView];

    for (UIScrollView *subview in _fbLikeWebView.subviews)
    {
        if ([subview isKindOfClass:[UIScrollView class]]) {
            subview.scrollEnabled = NO;
            subview.bounces = NO;
        }
    }
}

then in ViewWillAppear method call the enbeddFBLikeButton Method to add the fbLike button wigdet on web view:

-(void)viewWillAppear:(BOOL)animated
{
    [self embedFBLikeButton];
    [_fbLikeWebView reload];
}

-(void)embedFBLikeButton
{
    NSString *facebookUrl =  //here paste the url you get from fb developer link above;

    [self.fbLikeWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:facebookUrl]]];
}

You conform to UIWebViewDelegate now its turn to defining th edelegate method here:

#pragma mark - WebView Delgate Methods

- (BOOL)webView:(UIWebView *)webview shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([request.URL.lastPathComponent isEqualToString:@"login.php"])
    {
        [self login];

        return NO;
    }

    return YES;
}

-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [_fbLikeWebView stopLoading];
}

This method for login the user to facebook Account:

- (void)login
{
    [FBSession setActiveSession: [[FBSession alloc] initWithPermissions:@[@"publish_actions", @"publish_stream", @"user_photos"]]];

    [[FBSession activeSession] openWithBehavior: FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
        switch (status) {
            case FBSessionStateOpen:
                // call the legacy session delegate
                //Now the session is open do corresponding UI changes
                if (session.isOpen) {
                    FBRequest *me = [FBRequest requestForMe];

                    [me startWithCompletionHandler: ^(FBRequestConnection *connection,
                                                      NSDictionary<FBGraphUser> *my,
                                                      NSError *error) {
                        if (!my) {
                            NSLog(@"Facebook error:\n%@", error.description);
                            [[[UIAlertView alloc] initWithTitle: @"Error"
                                                        message: @"Facebook Login error."
                                                       delegate: self
                                              cancelButtonTitle: @"Ok"
                                              otherButtonTitles: nil, nil] show];
                            return;
                        }
                    }];

                    [_fbLikeWebView reload];

                    [[[UIAlertView alloc] initWithTitle: @""
                                                message: @"Successfully Login. Please click on like button"
                                               delegate: self
                                      cancelButtonTitle: @"Ok"
                                      otherButtonTitles: nil, nil] show];
                }
                break;
            case FBSessionStateClosedLoginFailed:
            {
                [_fbLikeWebView reload];
            }
                break;
            default:
                break; // so we do nothing in response to those state transitions
        }
    }];
}
Amaranthine answered 6/12, 2013 at 12:17 Comment(0)
S
2

It is an old question, but we just got the answer

By using fb sdk now we can simply add the like button

FBLikeControl *likeButton = [[FBLikeControl alloc] init];
like.objectID = @"http://ihackthati.wordpress.com/photo1/";
[self addSubview:like];

https://developers.facebook.com/docs/ios/like-button/

Sur answered 15/10, 2014 at 12:11 Comment(0)
S
1

Several of the solutions above (e.g. @floorjiann, @fabiobeta, @Stephen-Darlington, @scott) assume that the iPhone application is also a Facebook application (with its own id).

However if your iPhone application just has a fan page, this is not the case.

Slype answered 28/3, 2011 at 10:37 Comment(1)
i have ios app which is also a facebook app. i've created it's facebook app page as well. I want to like facebook page programatically on behalf of user when ever registered user of my app hits like button. any suggestion plz? i found that we can't like facebook pages through built-in like actions. is it possible to like facebook page of ios facebook app?Dorsal
G
0

You can include it in a UIWebView, just make sure it's inside <html><body>. The problem is that once the user clicks the button, the UIWebView may redirect to a log-in form, so you would have to make it large enough to fit that thing.

Have fun creating a design that presents the button in a sensible fashion.

Grassofparnassus answered 4/8, 2010 at 12:7 Comment(0)
F
0

Facebook have recently updated their iPhone Facebook Connect framework to work with the Graph API. This is the API that you need to "Like" objects on Facebook.

Fare answered 27/8, 2010 at 12:39 Comment(1)
Could you be so kind to add some more details?Bosom
Q
0

Here you go - w the source code...enjoy -and please leave a comment if its useful.

http://nowbranded.com/blog/adding-a-facebook-like-button-to-an-iphone-application/

Quincentenary answered 8/9, 2010 at 3:41 Comment(1)
Code by the link base on FBSession which is deprecated now.Bosom
B
0

First check your authentication on Facebook, then use url code instead of iFrame like this:

NSURL *url = [NSURL URLWithString:@"http://www.facebook.com/plugins/like.php?href=https%3A%2F%2Fwww.facebook.com%2FRemtechConfessionChambers&width=200&height=80&colorscheme=light&layout=standard&action=like&show_faces=true&send=false"];
NSURLRequest *req = [NSURLRequest requestWithURL:url];

[webViewFb loadRequest:req];
Biddick answered 24/10, 2013 at 12:34 Comment(0)
P
0

You can use the open source SOOMLA Profile plugin: https://github.com/soomla/ios-profile

There are code examples there for lots of Facebook related actions (login, like, share status, upload image, get contacts, invite, etc.). See: https://github.com/soomla/ios-profile/blob/master/SoomlaiOSProfileExample/SoomlaiOSProfileExample/ViewController.m

Percussion answered 18/2, 2015 at 15:14 Comment(0)
C
-1

I this moment in time you can't. The mobile APIs are not exposing this capability. You can use some workaround consisting in embedding the html way of doing it in your app.

Some details on the workaround here: http://petersteinberger.com/2010/06/add-facebook-like-button-with-facebook-connect-iphone-sdk/

Cw answered 3/11, 2010 at 19:22 Comment(2)
I'd mailed to the author for the broken link! Will update here if get some reply!Erstwhile
In the reply from the author, after this post Facebook has made 50 times changes to the SDK, so its now outdated and not like to avail.Erstwhile

© 2022 - 2024 — McMap. All rights reserved.