Adding the Facebook Like Button in an iPhone App

前端 未结 11 977
忘掉有多难
忘掉有多难 2020-12-04 17:08

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.

11条回答
  •  温柔的废话
    2020-12-04 17:20

    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 
    
    @interface TestViewController : UIViewController 
    
    @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 *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
            }
        }];
    }
    

提交回复
热议问题