Facebook页面就像iOS一样

时间:2013-04-22 08:57:21

标签: iphone ios facebook ipad

我知道如何在Facebook上喜欢照片/评论。但我希望通过我的应用程序喜欢Facebook 页面。可能吗?如果是的话,有人能给我一些建议吗?

3 个答案:

答案 0 :(得分:3)

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

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

一些相关的帖子 -

答案 1 :(得分:1)

我已经编写了一个方法我的调用,你可以通过在方法中给出你的网页来表示你的页面。 对于这种方法,你必须使用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];
    }
}

以下是app委托文件中使用的代码....

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

答案 2 :(得分:1)

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

在ViewController.h中你要添加fb之类的按钮:

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