Facebook SDK iOS登录与Facebook应用程序安装 [英] Facebook SDK iOS login with Facebook app installed

查看:152
本文介绍了Facebook SDK iOS登录与Facebook应用程序安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我已经意识到,仅当用户已经给予Facebook访问我的应用程序,然后重新启动我的应用程序时才会发生以下问题。用户第一次在我的应用程序中选择Facebook按钮,它会转到Facebook应用程序进行身份验证,但由于身份验证状态仍保存在Facebook应用程序中,因此立即返回到我的应用程序,不会向用户解释, postToFacebookWall方法失败。

UPDATE: I've realized that the below problem only occurs if the user has already given Facebook access to my app, and then reinstals my app. The first time the user selects the Facebook button in my app, it goes to the Facebook app to get authentication, but since the authentication status is still saved in the Facebook app, it returns immediately to my app without explanation to the user, and the postToFacebookWall method fails.

更新2:我最终实现了这里找到的解决方案:,并避免Facebook的使用者通过对话框与新的Facebook-connect-ios-api应用程序。 Safari更好,因为至少用户收到一条消息,表示他们将登录。但是Feed对话仍然像以前一样第一次失败。

UPDATE 2: I ended up implementing the solution found here: How to authorize user through a DIALOG with the NEW Facebook Connect iOS API? and avoided the Facebook app. Safari is better because at least the user gets a message saying that they will be logging in. However the Feed Dialogue still fails the first time as before.

我有什么必须是一个常见的问题,但我还没有找到解决方案。请注意,我的应用程序仅限于iOS 5及更高版本。

I'm having what must be a common problem, but I haven't been able to find the solution yet. Note that my app is restricted to iOS 5 and above.

我有一个带有Facebook按钮的应用程序发布到用户墙。当按下按钮时,应用程序将检查用户是否已经允许应用程序发布到墙上。如果他们有,一个视图弹出一个Feed对话框与墙上的信息。如果没有,Safari将被打开,用户将被允许登录。登录后,他们将被收回应用程序,并且将弹出Feed对话框视图。

I have an app with a Facebook button for posting to a users wall. When the button is pressed, the app will check to see if the user has already given permission to the app to post to the wall. If they have, a view pops up with a Feed Dialog with the wall post information. If they haven't, Safari will be opened and the user will be allowed to login. After logging in they will be taken back to the app and the Feed Dialog view will pop up.

这适用于模拟器,在我的设备上,如果是Facebook应用程序没有安装如果安装了Facebook应用程序,该用户将被带到Facebook应用程序,但是立即返回到原始应用程序,而不允许用户执行任何操作。 Feed对话框弹出,但它完全为空,并自动关闭。再次按下Facebook按钮会显示Feed对话框视图,因为用户一旦允许应用程序发布到他们的墙上。

This works on the Simulator, and on my device, if the Facebook app is not installed. If the Facebook app is installed, the user is taken to the Facebook app, but then immediately returned to the original app, without allowing the user to do anything. The Feed Dialog pops up, but it is completely blank and dismisses automatically. Pressing the Facebook button again will bring up the Feed Dialog view as it is supposed to once the user has given permission to the app to post to their wall.

我几乎按照以下说明找到:
http://developers.facebook .com / docs / mobile / ios / build /#implementsso

I pretty much followed the instructions found here: http://developers.facebook.com/docs/mobile/ios/build/#implementsso

这里:
http://developers.facebook.com/docs/reference/dialogs/feed/

在AppDelegate中,我有以下代码:

In the AppDelegate I have the following code:

- (void)loginToFacebook
{
    // Set up facebook
    self.facebook = [[Facebook alloc] initWithAppId:@"YOUR_APP_ID" andDelegate:self];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        self.facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        self.facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

    if (![self.facebook isSessionValid]) {
        [self.facebook authorize:nil];
    }
}

// This is an FBSessionDelegate protocol method
// that gets called after a successful login
- (void)fbDidLogin 
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[self.facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[self.facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

    [[NSNotificationCenter defaultCenter] postNotificationName:@"POST_TO_FACEBOOK_WALL" object:nil];
}

// This is a UIApplicationDelegate protocol method
// It is called when safari is dismissed
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{
    return [self.facebook handleOpenURL:url]; 
}

这个和示例代码的唯一区别是我添加了一个NSNotification由我的视图控制器拾取,我有以下代码:

The only difference between this and the sample code is that I added an NSNotification that is picked up by my view controller, where I have the following code:

    - (void)facebookButtonPressed:(UIControl*)sender 
    {
        Facebook *facebook = [(AppDelegate*)[[UIApplication sharedApplication] delegate] facebook];

        if (![facebook isSessionValid])
        {
            // Login to facebook if not already logged in
            [(AppDelegate*)[[UIApplication sharedApplication] delegate] loginToFacebook];
        }
        else 
        {
            [self postToFacebookWall];
        }

    }

    - (void)postToFacebookWall
    {
        Facebook *facebook = [(AppDelegate*)[[UIApplication sharedApplication] delegate] facebook];

        /*Facebook Application ID*/
        // TODO: Get this either from the AppDelegate facebook property or the info plist
        NSString *client_id = @"YOUR_APP_ID";

        NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  kAppId, @"app_id",
  @"http://developers.facebook.com/docs/reference/dialogs/", @"link",
  @"http://fbrell.com/f8.jpg", @"picture",
  @"Facebook Dialogs", @"name",
  @"Reference Documentation", @"caption",
  @"Using Dialogs to interact with users.", @"description",
  nil];

        [facebook dialog:@"feed" andParams:params andDelegate:self];
    }

有谁知道如何解决我的问题? (另外,如果不清楚,YOUR_APP_ID将替换为我的代码中的应用程序ID)。

Does anyone know how to fix my issue? (Also, in case it isn't clear, YOUR_APP_ID is replaced with my app id in my code).

推荐答案

通过实施这里找到的两个解决方案来解决我的问题:

I ended up solving my questions by implementing the two solutions found here:

https: /stackoverflow.com/a/5683454/472344

https ://stackoverflow.com/a/9137887/472344

这篇关于Facebook SDK iOS登录与Facebook应用程序安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆