1

I have followed the instruction to implement a login process to Facebook from my app:

https://developers.facebook.com/docs/mobile/ios/build/#implementsso

Here is my code:

- (IBAction)facebookButton:(id)sender {

    facebook = [[Facebook alloc] initWithAppId:@"383032031757454" andDelegate:self];

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

    if (![facebook isSessionValid]) {
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"user_likes", 
                                @"read_stream",
                                nil];
        [facebook authorize:permissions];
        permissions=nil;
    }    
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [facebook handleOpenURL:url]; 
}

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

}

After login and allow permissions to the app, Safari remains opened and I have to return back to my app manually, but the sample app "Hackbook" provided with SDK, after accepting permissions and login within Safari, it dismisses Safari and returns back to the app. I want to do the same but I wasn't successful.

This is the .plist file:

enter image description here

Thanks

4

3 に答える 3

1

内部を少し掘り下げることで、機能しない理由を簡単に判断できますFacebook.mhandleOpenURL:で方法を確認してくださいFacebook.m

    // Facebook.m
- (BOOL)handleOpenURL:(NSURL *)url {
    // If the URL's structure doesn't match the structure used for Facebook authorization, abort.
    if (![[url absoluteString] hasPrefix:[self getOwnBaseUrl]]) {
        return NO;
    }

    NSString *query = [url fragment];

    // Version 3.2.3 of the Facebook app encodes the parameters in the query but
    // version 3.3 and above encode the parameters in the fragment. To support
    // both versions of the Facebook app, we try to parse the query if
    // the fragment is missing.
    if (!query) {
        query = [url query];
    }

    NSDictionary *params = [self parseURLParams:query];

以下にブレークポイントを設定し、NSDictionary *params = [self parseURLParams:query];その内容を読み取ります。ログインが機能しなかった理由を示すエラー メッセージが表示されます。

于 2012-07-02T12:36:58.393 に答える
1

Facebook アプリ ID をinfo.plistiOS アプリのファイルに設定しましたか?

同様のケースがこのリンクこのリンクにあります。これがお役に立てば幸いです。

于 2012-07-02T11:24:46.800 に答える
0

アプリの Facebook ページで、iOS SSO 用にアプリを構成しましたか?

どのOSを使用していますか?コードは 4.2+ でのみ機能します

指示に完全に従っていませんでした.plistファイルが少し間違っています(これは問題ではないと思います)。

于 2012-07-02T12:15:30.577 に答える