【问题标题】:Facebook page like in iOSiOS 中的 Facebook 页面
【发布时间】:2013-04-15 02:46:54
【问题描述】:

我知道如何在 Facebook 中为照片/cmets 点赞。但我想通过我的应用为 Facebook 页面点赞。可能吗?如果是的话,谁能给我一些建议?

【问题讨论】:

    标签: iphone ios facebook ipad


    【解决方案1】:

    这是个问题。如果您能够做到这一点,您将能够以编程方式“喜欢”一个页面,而用户不必意识到这是正在发生的事情。这将违反 Facebook 的服务条款。

    我认为你最好在你的应用程序中简单地放置一个常规的“喜欢按钮”并让你的用户决定他们是否要点击它。

    一些相关的帖子 -

    【讨论】:

    • 可以使用 Graph API 编写代码以编程方式为 Facebook 页面点赞。
    • @AbdulYasin - 随时添加您自己的答案。我仍然不认为这是可能的。查看我发布的链接 - 它们都包含相关信息。
    • -我搜索了很多。最后得出结论,Facebook 不允许我们在未经用户事先许可的情况下点赞页面。非常感谢 Lix……嗯,喜欢公共页面的完美方法是什么。期待您的来信。
    • 在我看来,最好和最简单的方法是向您的用户显示点赞按钮,让他们决定是否点击它:-)
    • 如果我们在应用程序中包含一个 Like 按钮,我们是否应该通过登录循环?
    【解决方案2】:

    我已经编写了一个调用方法,您可以通过在方法中提供其 url 来喜欢您的页面。 对于这种方法,您必须使用 facebook sdk 并且需要添加一些已弃用的 facebook 文件。

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

    如何找到这些文件并使用它取决于您。无论如何,这都是适用于页面的代码。

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

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

    - (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

    【讨论】:

      【解决方案3】:

      类似 Widget 的 Fb 可以嵌入到我们的应用程序中。您只需添加一个 webView 并获取 Fb Like Widget html code/URL here

      在 ViewController.h 中你想添加 fb like 按钮的地方:

      #import <UIKit/UIKit.h>
      
      @interface TestViewController : UIViewController <UIWebViewDelegate>
      
      @property (strong, nonatomic) UIWebView * fbLikeWebView;
      
      -(void)embedFBLikeButton;
      
      @end
      

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

      然后在 ViewWillAppear 方法中调用 enbeddFBLikeButton 方法在 web view 上添加 fbLike 按钮 wigdet:

      -(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 现在轮到在这里定义 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
              }
          }];
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-31
        • 1970-01-01
        • 2012-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-15
        相关资源
        最近更新 更多