2016-05-17 179 views
1
  • 我想在登錄時打開Facebook應用程序,該應用程序已經安裝在設備中進行登錄驗證,但始終在Safari瀏覽器中打開。

- Facebook的按鈕,點擊iOS:使用Facebook登錄並打開本機Facebook應用程序

-(void)loginButtonClicked 
     { 
      FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; 

     // [login setLoginBehavior:FBSDKLoginBehaviorNative]; 

       [login logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) 
       { 
        if (error) 
        { 
         NSLog(@"Login process error"); 
        } 
        else if (result.isCancelled) 
        { 
         NSLog(@"User cancelled login"); 
        } 
        else 
        { 
         NSLog(@"Login Success"); 
         if ([result.grantedPermissions containsObject:@"email"]) 
         { 
          NSLog(@"result is:%@",result); 
          [self fetchUserInfo]; 
         } 
         else 
         { 
     //      [SVProgressHUD showErrorWithStatus:@"Facebook email permission error"]; 
         } 
        } 
       }]; 
      } 
     } 

,尤其是圓形獲取用戶信息

 -(void)fetchUserInfo 
     { 
      if ([FBSDKAccessToken currentAccessToken]) 
      { 
       NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]); 

       [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, email"}] 
       startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { 
        if (!error) 
        { 
         NSLog(@"results:%@",result); 

         NSString *email = [result objectForKey:@"email"]; 
         NSString *userId = [result objectForKey:@"id"]; 

         if (email.length >0) 
         { 
          NSString *accessToken = [[FBSDKAccessToken currentAccessToken] tokenString]; 
          [ref authWithOAuthProvider:@"facebook" token:accessToken 
            withCompletionBlock:^(NSError *error, FAuthData *authData) 
           { 
             if (error) 
             { 
              NSLog(@"Login failed. %@", error); 
             } 
             else 
             { 
              NSLog(@"Logged in! %@", authData); 

              //Start you app Todo 
             } 
         else 
         { 
          NSLog(@"Facebook email is not verified"); 
         } 
        } 
        else 
        { 
         NSLog(@"Error %@",error); 
        } 
       }]; 

     } 
  • 我想打開Facebook應用與Facebook的登錄。
+0

http://stackoverflow.com/questions/32566734/native-facebook-app-does-not-open-with-facebook-login-in-ios-9 檢查此鏈接。 –

+0

此代碼已經使用,但不是在fb應用程序中打開,總是瀏覽器,我用新的sdk 4x –

+0

我不是在這個組中 –

回答

2

請使用FBLoginView如Facebook簽到按鈕,它會自動檢測的Facebook應用程序&ü可以能夠與Facebook登錄。 FBLoginView Facebook SDK中提供的類。

+0

我使用了新的sdk,所以我可以這樣做嗎? –

+0

是的,在新的SDK中你會得到這個類 – SM18

2

從V4.6.0它不會重定向到FB應用程序。見下面

(v4.6.0 - 2015年9月10日)另外,SDK對話框如 登錄,贊,分享對話框自動判斷基於 在設備上,包括SFSafariViewController的Safari瀏覽器,而不是最好的UI。 請參閱我們的準備iOS 9指南。

enter image description here

1

請正確設置參數的Info.plist,你可能沒有提供的網址這樣

<array> 
     <dict> 
      <key>CFBundleURLSchemes</key> 
      <array> 
       <string>fb776035152513539</string> 
      </array> 
     </dict> 
</array> 

<key>FacebookAppID</key> 
<string>776035152513539</string> 

如果你不能夠實現你想要什麼,那麼你可以使用這些類。

頭文件 -

// 
// LxFaceBookHandler.h 
// KickOffSlotMachine 
// 
// Created by Prakhar Goyal on 06/08/15. 
// Copyright (c) 2015 LOGICNEXT. All rights reserved. 
// 

#import <Foundation/Foundation.h> 
#import <FBSDKCoreKit/FBSDKCoreKit.h> 
#import <FBSDKLoginKit/FBSDKLoginKit.h> 

typedef NS_ENUM(NSInteger,LOGINRESPONSE) 
{ 
    LOGINRESPONSE_ERROR, 
    LOGINRESPONSE_CANCEL, 
    LOGINRESPONSE_SUCCESS 
}; 

@protocol LxFaceBookHandlerDelegate <NSObject> 

-(void)DidLogInWithResponse:(LOGINRESPONSE)type; 

@end 

@interface LxFaceBookHandler : NSObject 

@property(weak,nonatomic)id<LxFaceBookHandlerDelegate>delegate; 
-(void)InitFaceBookLogin; 

//Called from app Delegate 

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation; 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions; 

@end 

和實現文件 -

// 
// LxFaceBookHandler.m 
// KickOffSlotMachine 
// 
// Created by Prakhar Goyal on 06/08/15. 
// Copyright (c) 2015 LOGICNEXT. All rights reserved. 
// 

#import "LxFaceBookHandler.h" 

@implementation LxFaceBookHandler 
@synthesize delegate = __delegate; 

- (instancetype)init 
{ 
    self = [super init]; 
    if (self) 
    { 
     __delegate = nil; 
    } 
    return self; 
} 

-(void)InitFaceBookLogin; 
{ 
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init]; 
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) 
    { 
     if (error) 
     { 
      if (__delegate!=nil) 
      { 
       if ([__delegate respondsToSelector:@selector(DidLogInWithResponse:)]) 
       { 
        [__delegate DidLogInWithResponse:LOGINRESPONSE_ERROR]; 
       } 
      } 
     } 
     else if (result.isCancelled) 
     { 
      if (__delegate!=nil) 
      { 
       if ([__delegate respondsToSelector:@selector(DidLogInWithResponse:)]) 
       { 
        [__delegate DidLogInWithResponse:LOGINRESPONSE_CANCEL]; 
       } 
      } 
     } 
     else 
     { 
      if (__delegate!=nil) 
      { 
       if ([__delegate respondsToSelector:@selector(DidLogInWithResponse:)]) 
       { 
        [__delegate DidLogInWithResponse:LOGINRESPONSE_SUCCESS]; 
       } 
      } 

      //    if ([result.grantedPermissions containsObject:@"email"]) { 
      //     // Do work 
      //    } 
     } 
    }]; 

} 

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

} 

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    return [[FBSDKApplicationDelegate sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions]; 
} 

@end 

添加FBSDKCoreKit.Framework和FBSDKLoginKit.Framework在您的項目,並設置你的應用程序Delefate這樣的 -

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.window.backgroundColor=[UIColor blackColor]; 

SplashViewController *rVC = [[SplashViewController alloc] initWithNibName:@"SplashViewController" bundle:nil]; 

    self.mNavController=[[UINavigationController alloc]initWithRootViewController:rVC]; 
    [self.mNavController setNavigationBarHidden:YES]; 

    self.window.rootViewController=self.mNavController; 
    [self.window makeKeyAndVisible]; 

    self.handler = [[LxFaceBookHandler alloc]init]; 



    [self.handler application:application didFinishLaunchingWithOptions:launchOptions]; 
    return YES; 
} 
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{ 
    return [self.handler application:application openURL:url sourceApplication:sourceApplication annotation:annotation]; 
} 

現在你可以使用這些類作爲任何事件假設在按鈕上點擊 -

- (IBAction)FacebookLoginPressed:(UIButton *)sender 
{ 
    App.handler.delegate = self; 
    [App.handler InitFaceBookLogin]; 
} 
相關問題