2013-04-22 26 views
2

我知道如何喜欢Facebook中的照片/评论。但我想通过我的应用程序喜欢Facebook 页面。可能吗?如果是的话,任何人都可以给我一些建议吗?像在iOS中的Facebook页面

回答

3

这是一个问题。如果你能够做到这一点,你将能够以编程方式“喜欢”一个页面,而用户不必知道这是发生了什么。这将违反Facebook的TOS。

我认为只需在应用程序中放置一个常规的“喜欢的按钮”,并让用户决定是否要点击该按钮,会更好。

一些相关的帖子 -

+0

我们可以使用图形API来programmaatically喜欢一个Facebook网页编写代码。 – 2013-12-31 10:15:51

+0

@AbdulYasin - 随时添加你自己的答案。我仍然认为这是不可能的。检出我发布的链接 - 它们都包含相关信息。 – Lix 2013-12-31 10:20:44

+0

- 我查了很多。最后得出的结论是,Facebook不会允许我们在未经用户事先许可的情况下喜欢网页。 非常感谢你Lix ... 那么,什么是喜欢公共页面的完美方法。 期待收到你的来信。 – 2014-01-02 08:13:23

1

我写了一个方法,我的电话,您可以通过给它的URL喜欢你的页面在该方法中。 对于这种方法,你必须使用Facebook的SDK,并需要添加一些不赞成的Facebook文件。

#import "Facebook.h" 
#import "FBCustomLoginDialog.h" 
#import "Accounts/Accounts.h" 

它是由你介绍如何找到文件,并使用it.Any的方式,这是工作的代码像页。

-(void)like 
{ 
    NSString *[email protected]"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]; 
    } 
} 

这里是在应用程序委托文件中使用的代码....

- (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); 
} 

使用该文件FBCustomLoginDialog.hFBCustomLoginDialog.m

+0

我会试试这个,让你知道.... – Venkat 2013-05-25 05:03:45

+0

输入“FBCustomLoginDialog.h”怎么办????????????? – Venkat 2013-05-25 05:13:29

+0

使用我在编辑好的代码中给出的文件 – Warewolf 2013-05-25 05:17:47

1

Fb的比如Widget可以嵌入到我们的应用程序。你只需要添加一个webView并获得Fb Like Widget html code/URL here

在ViewController.h要添加FB像按钮:

在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; 
     } 
    } 
} 

#import <UIKit/UIKit.h> 

@interface TestViewController : UIViewController <UIWebViewDelegate> 

@property (strong, nonatomic) UIWebView * fbLikeWebView; 

-(void)embedFBLikeButton; 

@end 

然后在viewWillAppear中方法调用enbeddFBLikeButton方法添加的Wigdet的fbLike按钮网页视图:

-(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]]]; 
} 

您符合UIWebViewDelegate no w ^轮到它定义日在这里edelegate方法:

#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]; 
} 

这种方法登录用户的Facebook帐户:

- (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 
     } 
    }]; 
}