5

我将 FBLoginView 添加到我的 ViewController < FBLoginViewDelegate >:

FBLoginView *loginview = [[FBLoginView alloc] init];    
loginview.frame = CGRectOffset(loginview.frame, 5, 5);
loginview.delegate = self;    
[self.view addSubview:loginview];    
[loginview sizeToFit];

plist 中所有必要的字段(FacebookAppID、FacebookDisplayName、URL Schemes)都是根据教程设置的。facebook 应用程序也按照教程配置(设置了捆绑 ID,启用了 Facebook 登录)。


但是登录仍然没有执行。当我按下“登录”时,我被重定向到使用 facebook 登录的浏览器,但是当它完成后,我没有登录应用程序(loginViewFetchedUserInfo:user: 没有被调用,“登录”没有改变“注销”)。可能是什么问题?

4

4 回答 4

2

您需要将以下内容添加到应用委托

- (BOOL)application:(UIApplication *)application
        openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
     annotation:(id)annotation {

  // Call FBAppCall's handleOpenURL:sourceApplication to handle Facebook app responses
  BOOL wasHandled = [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];

  // You can add your app-specific url handling code here if needed

  return wasHandled;
}
于 2014-04-09T23:13:09.550 回答
2

在我在 AppDelegate.m 中实现以下内容后,一切正常(取自官方示例之一):

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                //NSLog(@"User session found");
                [FBRequestConnection
                 startForMeWithCompletionHandler:^(FBRequestConnection *connection,
                                                   NSDictionary<FBGraphUser> *user,
                                                   NSError *error) {
                     if (!error) {
                         self.loggedInUserID = user.id;
                         self.loggedInSession = FBSession.activeSession;
                     }
                 }];
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }

    [[NSNotificationCenter defaultCenter]
     postNotificationName:FBSessionStateChangedNotification
     object:session];

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

/*
 * Opens a Facebook session and optionally shows the login UX.
 */
- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    return [FBSession openActiveSessionWithReadPermissions:nil
                                              allowLoginUI:allowLoginUI
                                         completionHandler:^(FBSession *session,
                                                             FBSessionState state,
                                                             NSError *error) {
                                             [self sessionStateChanged:session
                                                                 state:state
                                                                 error:error];
                                         }];
}

/*
 *
 */
- (void) closeSession {
    [FBSession.activeSession closeAndClearTokenInformation];
}

/*
 * If we have a valid session at the time of openURL call, we handle
 * Facebook transitions by passing the url argument to handleOpenURL
 */
- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication];
}
于 2013-07-15T07:43:59.553 回答
0

@Sergey:你想在你的原生应用程序或浏览器中打开 FBDialog 吗?如果您想在本机应用程序中打开,请使用“FBSessionLoginBehaviorForcingWebView”。这是我正在使用的代码:

NSArray *permission = [NSArray arrayWithObjects:kFBEmailPermission,kFBUserPhotosPermission, nil];

FBSession *session = [[FBSession alloc] initWithPermissions:permission];

[FBSession setActiveSession: [[FBSession alloc] initWithPermissions:permission] ];

[[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {

    switch (status) {
        case FBSessionStateOpen:
            [self  yourmethod];
            break;
        case FBSessionStateClosedLoginFailed: {
            // prefer to keep decls near to their use
            // unpack the error code and reason in order to compute cancel bool
            NSString *errorCode = [[error userInfo] objectForKey:FBErrorLoginFailedOriginalErrorCode];
            NSString *errorReason = [[error userInfo] objectForKey:FBErrorLoginFailedReason];
            BOOL userDidCancel = !errorCode && (!errorReason || [errorReason isEqualToString:FBErrorLoginFailedReasonInlineCancelledValue]);
            if(error.code == 2) {
                UIAlertView *errorMessage = [[UIAlertView alloc] initWithTitle:@"FBAlertTitle"
                                                                       message:@"FBAuthenticationErrorMessage"
                                                                      delegate:nil
                                                             cancelButtonTitle:@"Ok"
                                                             otherButtonTitles:nil];
                [errorMessage performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
                errorMessage = nil;
            }
        }
            break;
            // presently extension, log-out and invalidation are being implemented in the Facebook class
        default:
            break; // so we do nothing in response to those state transitions
    }
}];
permission = nil;

或者您想在浏览器中打开,然后使用以下内容:

In your .h file 

#import <FacebookSDK/FacebookSDK.h>  and add FBLoginViewDelegate  delegate

In you .m file  
     FBLoginView *loginview = [[FBLoginView alloc] init];
     loginview.frame = CGRectOffset(loginview.frame, 5, 5);
     loginview.delegate = self;
     [self.view addSubview:loginview];
     [loginview sizeToFit];

// use following delegate methods

- (void)loginViewShowingLoggedInUser:(FBLoginView *)loginView {
    // first get the buttons set for login mode
}

- (void)loginViewFetchedUserInfo:(FBLoginView *)loginView
                            user:(id<FBGraphUser>)user {
    // here we use helper properties of FBGraphUser to dot-through to first_name and
    // id properties of the json response from the server; alternatively we could use
    // NSDictionary methods such as objectForKey to get values from the my json object
    NSLog(@"userprofile:%@",user);
}
- (void)loginViewShowingLoggedOutUser:(FBLoginView *)loginView {
    //BOOL canShareAnyhow = [FBNativeDialogs canPresentShareDialogWithSession:nil];

}

- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
    // see https://developers.facebook.com/docs/reference/api/errors/ for general guidance on error handling for Facebook API
    // our policy here is to let the login view handle errors, but to log the results
    NSLog(@"FBLoginView encountered an error=%@", error);
}
于 2013-07-12T05:24:38.333 回答
0

您可能需要实施

(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 方法

请务必致电:

return [FBSession.activeSession handleOpenURL:url];

当适用。

于 2013-07-12T03:35:46.123 回答