2

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

4

3 回答 3

3

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

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

一些相关的帖子 -

于 2013-04-22T09:04:34.780 回答
1

Fb like Widget 可以嵌入到我们的应用程序中。您只需添加一个 webView 并在此处获取 Fb Like Widget html 代码/URL

在 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 视图上添加 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
        }
    }];
}
于 2013-12-06T12:19:00.630 回答
1

我已经编写了一个调用方法,您可以通过在方法中提供其 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

于 2013-05-25T05:00:46.767 回答