I know how to like photos/comments in Facebook. But I want to like a Facebook page through my app. Is it possible? If yes, can anyone give me some suggestions?
3 Answers
This is a problem. If you were able to do this, you would be able to "like" a page programatically without the user necessarily being aware that this is what's happening. That would violate Facebook's TOS.
I think you'll be better off simply placing a regular "like button" in your application and letting your users decide if they want to click it.
Some related posts -
-
One can write code using Graph API to like a facebook page programmaatically. – Abdul Yasin Dec 31 '13 at 10:15
-
@AbdulYasin - feel free to add you own answer. I still do not think that it is possible. Checkout the links I posted - they all contain relevant information. – Lix Dec 31 '13 at 10:20
-
-I searched a lot. And finally concluded that facebook wont allow us to like a page without user's prior permission. Thank you so much Lix... Well , what is the perfect approach for liking a public page. Looking forward to hear from you. – Abdul Yasin Jan 02 '14 at 08:13
-
In my opinion the best and easiest approach would be to display the like button to your users and let them decide whether or not to click it :-) – Lix Jan 02 '14 at 08:19
-
Should we go through a login loop if we include a Like button within the app? – sarat Apr 23 '15 at 05:06
I have written a method my calling which you can like your page by giving its url in the method. For this method you have to use facebook sdk and need to add some deprecated facebook files as.
#import "Facebook.h"
#import "FBCustomLoginDialog.h"
#import "Accounts/Accounts.h"
It is up to you how you find that files and use it.Any way this is working code for page like.
-(void)like
{
NSString *likePage=@"http://in.yahoo.com/?p=us"; // here you page url
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
likePage, @"object",[[NSUserDefaults standardUserDefaults] valueForKey:@"token"],@"access_token",
nil];
[FBRequestConnection startWithGraphPath:@"/me/og.likes" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"liked with id %@",[result valueForKey:@"id"]] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
NSLog(@"result is %@",result);
}];
}
- (IBAction)likepageonFB:(id)sender
{
if ([[FBSession activeSession] isOpen]) {
[self like];
}else
{
[appDelegate openSession];
}
}
Here is code used in app delegate file ....
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[LikeAppViewController alloc] initWithNibName:@"LikeAppViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
// To-do, show logged in view
// [self openSession];
} else {
// No, display the login page.
[self showLoginView];
}
return YES;
}
#pragma mark- Facebook Methods
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
{
[[NSUserDefaults standardUserDefaults] setValue:[[FBSession activeSession] accessToken] forKey:@"token"];
NSLog(@"token is %@",[[FBSession activeSession] accessToken]);
[self.viewController like];
}
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
[self showLoginView];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
- (void)openSession
{
NSLog(@"open session called ");
NSArray *permissions=[[NSArray alloc] initWithObjects:@"publish_stream",@"publish_actions",@"user_likes",@"user_about_me",nil];
[FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
- (void)showLoginView
{
[self.viewController presentedViewController];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}
- (void)fbDialogLogin:(NSString*)token expirationDate:(NSDate*)expirationDate
{
NSLog(@"expiry date is %@",expirationDate);
}
use this file FBCustomLoginDialog.h and FBCustomLoginDialog.m

- 12,591
- 4
- 59
- 90
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
}
}];
}

- 3,922
- 2
- 29
- 43