2013-07-06 27 views
5

tôi thêm FBLoginView để ViewController tôi < FBLoginViewDelegate>:FBLoginView: đăng nhập không được thực hiện

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

Tất cả các lĩnh vực cần thiết trong plist (FacebookAppID, FacebookDisplayName, Schemes URL) đều thiết lập theo hướng dẫn . Ứng dụng facebook cũng được cấu hình theo hướng dẫn (ID gói được đặt, đăng nhập Facebook được bật).


Nhưng thông tin đăng nhập vẫn chưa được thực hiện. Khi tôi bấm vào "đăng nhập", tôi được chuyển hướng đến trình duyệt với đăng nhập facebook, nhưng khi nó kết thúc, tôi không đăng nhập vào ứng dụng (loginViewFetchedUserInfo: người dùng: không được gọi là "đăng nhập" đã không thay đổi thoát ra"). Điều gì có thể là vấn đề?

Trả lời

0

Bạn có thể cần phải thực hiện các

(BOOL) ứng dụng: (UIApplication *) ứng dụng handleOpenURL: (NSURL *) url phương pháp

Hãy chắc chắn để gọi:

return [FBSession.activeSession handleOpenURL:url];

Khi áp dụng.

0

@Sergey: Bạn có muốn mở FBDialog trên ứng dụng gốc hoặc trong trình duyệt không? Nếu bạn muốn mở trong ứng dụng gốc của mình thì hãy sử dụng "FBSessionLoginBehaviorForcingWebView". Đây là mã của tôi mà tôi đang sử dụng:

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; 

hoặc bạn muốn mở trong trình duyệt sau đó sử dụng sau đây:

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); 
} 
2

Tất cả mọi thứ đã làm việc sau khi tôi thực hiện những điều sau đây trong AppDelegate.m (lấy từ một trong những ví dụ chính thức):

- (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]; 
} 
2

Bạn cần phải thêm dòng sau vào ứng dụng đại biểu

- (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; 
} 
Các vấn đề liên quan