In this Post, I am going to share How to implement Facebook Like button without adding third Party code in your aplplication.
When I started this task, I googled it so that I can easily found the code but i didnot find any simple solution for this. then I decide to write code for this.
First Get the URL for FB Like Button Widget from this Facebook Developer Website Link. and Copy the URL.
Now open the ViewController where you want to add this FB like Button and in your viewController .h file add the WebView property. Just follow the code:
Then In Viewcontroller .m file add the following code :
And Yes the FB Like button is here. Now you can like the page through your iPhone Application.
When I started this task, I googled it so that I can easily found the code but i didnot find any simple solution for this. then I decide to write code for this.
First Get the URL for FB Like Button Widget from this Facebook Developer Website Link. and Copy the URL.
Now open the ViewController where you want to add this FB like Button and in your viewController .h file add the WebView property. Just follow the code:
//
// TestViewController.h
// PhotoApp
//
// Created by Sahil Mahajan on 11/19/13.
//
//
#import <UIKit/UIKit.h>
@interface TestViewController : UIViewController<UIWebviewDelegate>
@property (strong, nonatomic) UIWebView *fbLikeWebView;
@end
Then In Viewcontroller .m file add the following code :
//
// TestViewController.m
// PhotoApp
//
// Created by Sahil Mahajan on 11/19/13.
//
//
#import "TestViewController.h"
@implementation TestViewController
@synthesize fbLikeWebView = _fbLikeWebView;
- (id)init
{
self = [super initWithNibName: @"TestViewController"
bundle: nil];
if (self)
{
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// You can set the frame according to your view setting.
self.fbLikeWebView = [[UIWebView alloc] initWithFrame:CGRectMake(160.0, 100.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;
}
}
-(void)viewWillAppear:(BOOL)animated
{
[self embedFBLikeButton];
[_fbLikeWebView reload];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
-(void)embedFBLikeButton
{
NSString *facebookUrl = // Add here the url you cpied from Facebook developer Site;
[self.fbLikeWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:facebookUrl]]];
}
#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];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
}
#pragma mark - FbLogin Method
- (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
}
}];
}
@end
And Yes the FB Like button is here. Now you can like the page through your iPhone Application.