在 iOS 9 中使用 Facebook 登录无法打开本机 Facebook 应用程序 [英] Native Facebook app does not open with Facebook login in iOS 9

查看:181
本文介绍了在 iOS 9 中使用 Facebook 登录无法打开本机 Facebook 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 iPhone 6 plus 更新到 iOS 9 beta 并尝试执行 Facebook 登录,但每次它都显示带有 Facebook 登录表单的 UIWebView.

我有 Facebook sdk

FB_IOS_SDK_VERSION_STRING @"3.24.0"FB_IOS_SDK_TARGET_PLATFORM_VERSION @"v2.2"

我正在使用以下方法来执行 Facebook 登录

 NSArray *permissions = @[@"email",@"user_birthday",@"public_profile"];FBSessionStateHandler completionHandler = ^(FBSession *session, FBSessionState 状态, NSError *error) {[self sessionStateChanged:session state:status error:error];};如果([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded){//我们有一个缓存的令牌,所以打开会话[[FBSession activeSession]openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent从视图控制器:无完成处理程序:完成处理程序];} 别的 {[self clearAllUserInfo];[[NSURLCache sharedURLCache] removeAllCachedResponses];//创建一个新的 Facebook 会话FBSession *fbSession = [[FBSession alloc] initWithPermissions:permissions];[FBSession setActiveSession:fbSession];[fbSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent从视图控制器:无完成处理程序:完成处理程序];}

我在 plist 文件下有以下设置

  LSApplicationQueriesSchemes<阵列><string>fbapi</string><string>fbapi20130214</string><string>fbapi20130410</string><string>fbapi20130702</string><string>fbapi20131010</string><string>fbapi20131219</string><string>fbapi20140410</string><string>fbapi20140116</string><string>fbapi20150313</string><string>fbapi20150629</string><string>fb-messenger-api20140430</string><string>fbauth</string><string>fbauth2</string><阵列>

请让我知道我在这里遗漏了什么.首先是检查 iPhone 设备设置->Facebook 凭据 但从不打开 Facebook 应用程序进行登录.它似乎无法识别设备上安装的 Facebook 应用.

解决方案

以下是新的Facebook登录"的完整流程.

<小时>

这是我修改我的 Facebook 登录集成以使其在最新更新中工作的方式.

<块引用>

Xcode 7.xiOS 9Facebook SDK 4.x

步骤 1.下载最新的 Facebook SDK(包括重大变化).

第 2 步.将 FBSDKCoreKit.frameworkFBSDKLoginKit.framework 添加到您的项目中.

第 3 步.现在转到 Project > Build Phases > add SafariServices.framework

第 4 步.我们需要验证 info.plist 中的三处更改.

4.1 确保您的 info.plist 文件中有以下内容

<块引用>

CFBundleURLTypes<阵列><字典><key>CFBundleURLSchemes</key><阵列><string><你的 fb id 在这里例如.fbxxxxxx></string></阵列></dict></阵列><key>FacebookAppID</key><string><你的 FacebookAppID></string><key>FacebookDisplayName</key><string><Your_App_Name_Here></string>

4.2 现在添加以下白名单 Facebook 服务器,这是 iOS 9 必须的

<块引用>

NSAppTransportSecurity<字典><key>NSExceptionDomains</key><字典><key>facebook.com</key><字典><key>NSIncludesSubdomains</key><真/><key>NSExceptionRequiresForwardSecrecy</key><假/></dict><key>fbcdn.net</key><字典><key>NSIncludesSubdomains</key><真/><key>NSExceptionRequiresForwardSecrecy</key><假/></dict><key>akamaihd.net</key><字典><key>NSIncludesSubdomains</key><真/><key>NSExceptionRequiresForwardSecrecy</key><假/></dict></dict></dict>

4.3 添加 URL 方案

<块引用>

LSApplicationQueriesSchemes<阵列><string>fbapi</string><string>fb-messenger-api</string><string>fbauth2</string><string>fbshareextension</string></阵列>

第 5 步.现在打开 AppDelegate.m 文件

5.1 添加下面的导入语句,(删除旧的).

<块引用>

#import #import 

5.2 更新如下方法

- (BOOL)application:(UIApplication *)applicationopenURL:(NSURL *)url源应用程序:(NSString *)源应用程序注释:(id)注释{返回 [[FBSDKApplicationDelegate sharedInstance] 应用程序:应用程序打开网址:网址源应用:源应用注释:注释];}- (void)applicationDidBecomeActive:(UIApplication *)application {[FBSDKAppEvents activateApp];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {返回 [[FBSDKApplicationDelegate sharedInstance] 应用程序:应用程序didFinishLaunchingWithOptions:launchOptions];}

第 6 步.现在我们需要修改我们的登录控制器,我们在这里做登录任务

6.1 在 Login ViewController.m

中添加这些导入<块引用>

#import #import 

6.2 添加 Facebook 登录按钮

FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];loginButton.center = self.view.center;[self.view addSubview:loginButton];

6.3 处理登录按钮点击

-(IBAction)facebookLogin:(id)sender{FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];if ([FBSDKAccessToken currentAccessToken]){NSLog(@"令牌可用:%@",[[FBSDKAccessToken currentAccessToken]tokenString]);[自取用户信息];}别的{[登录 logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error){如果(错误){NSLog(@"登录过程出错");}否则如果(result.isCancelled){NSLog(@"用户取消登录");}别的{NSLog(@"登录成功");if ([result.grantedPermissions containsObject:@"email"]){NSLog(@"结果为:%@",result);[自取用户信息];}别的{[SVProgressHUD showErrorWithStatus:@"Facebook 电子邮件权限错误"];}}}];}}

6.4 获取用户信息(姓名、电子邮件等)

-(void)fetchUserInfo{if ([FBSDKAccessToken currentAccessToken]){NSLog(@"令牌可用:%@",[[FBSDKAccessToken currentAccessToken]tokenString]);[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" 参数:@{@"fields": @"id, name, email"}]startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id 结果, NSError *error) {如果(!错误){NSLog(@"结果:%@",result);NSString *email = [result objectForKey:@"email"];NSString *userId = [result objectForKey:@"id"];如果 (email.length > 0 ){//启动你的应用程序Todo}别的{NSLog(@Facebook 邮箱未验证");}}别的{NSLog(@"错误 %@",error);}}];}}

第 7 步.现在你可以构建项目了,你应该得到下面的屏幕.

希望对大家有所帮助.

参考资料:感谢 Facebook 文档、Stackoverflow 帖子和 Google.

I have updated iPhone 6 plus to iOS 9 beta and trying to perform Facebook login but each time its presenting UIWebView with Facebook login form.

I have Facebook sdk

FB_IOS_SDK_VERSION_STRING @"3.24.0"
FB_IOS_SDK_TARGET_PLATFORM_VERSION @"v2.2"

And I am using following methods to perform Facebook Login

    NSArray *permissions = @[@"email",@"user_birthday",@"public_profile"];


     FBSessionStateHandler completionHandler = ^(FBSession *session, FBSessionState status, NSError *error) {
         [self sessionStateChanged:session state:status error:error];
     };

     if ([FBSession activeSession].state == FBSessionStateCreatedTokenLoaded) {
     // we have a cached token, so open the session
         [[FBSession activeSession]openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
                                 fromViewController:nil
                                  completionHandler:completionHandler];
     } else {

     [self clearAllUserInfo];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];

     // create a new facebook session
     FBSession *fbSession = [[FBSession alloc] initWithPermissions:permissions];
     [FBSession setActiveSession:fbSession];
     [fbSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent
              fromViewController:nil
               completionHandler:completionHandler];
     }

I have following setting under plist file

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fbapi</string>
        <string>fbapi20130214</string>
        <string>fbapi20130410</string>
        <string>fbapi20130702</string>
        <string>fbapi20131010</string>
        <string>fbapi20131219</string>
        <string>fbapi20140410</string>
        <string>fbapi20140116</string>
        <string>fbapi20150313</string>
        <string>fbapi20150629</string>
        <string>fb-messenger-api20140430</string>
        <string>fbauth</string>
        <string>fbauth2</string>
   <array>

Please let me know what I am missing here. First it is checking for iPhone device Setting-> Facebook credentials but never open Facebook app for login. Seems it does not recognize Facebook app installed on device.

解决方案

Below is complete process for new "Facebook login".


this is how I have revised my Facebook Login integration to get it work on latest update.

Xcode 7.x , iOS 9 , Facebook SDK 4.x

Step-1. Download latest Facebook SDK (it includes major changes).

Step-2. Add FBSDKCoreKit.framework and FBSDKLoginKit.framework to your project.

Step-3. Now go to Project > Build Phases > add SafariServices.framework

Step-4. There are three changes in info.plist we need to verify.

4.1 Make sure you have below in your info.plist file

<key>CFBundleURLTypes</key>
<array>
  <dict>
  <key>CFBundleURLSchemes</key>
  <array>
    <string><your fb id here eg. fbxxxxxx></string>
  </array>
  </dict>
</array>
  <key>FacebookAppID</key>
  <string><your FacebookAppID></string>
  <key>FacebookDisplayName</key>
<string><Your_App_Name_Here></string>

4.2 Now add below for White-list Facebook Servers, this is must for iOS 9

<key>NSAppTransportSecurity</key>
<dict>
  <key>NSExceptionDomains</key>
  <dict>
    <key>facebook.com</key>
    <dict>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSExceptionRequiresForwardSecrecy</key>
      <false/>
    </dict>
    <key>fbcdn.net</key>
    <dict>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSExceptionRequiresForwardSecrecy</key>
      <false/>
    </dict>
    <key>akamaihd.net</key>
    <dict>
      <key>NSIncludesSubdomains</key>
      <true/>
      <key>NSExceptionRequiresForwardSecrecy</key>
      <false/>
    </dict>
  </dict>
</dict>

4.3 Add URL schemes

<key>LSApplicationQueriesSchemes</key>
  <array>
      <string>fbapi</string>
      <string>fb-messenger-api</string>
      <string>fbauth2</string>
      <string>fbshareextension</string>
  </array>

Step-5. Now open AppDelegate.m file

5.1 Add below import statements, (remove old one).

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

5.2 update following following methods

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

- (void)applicationDidBecomeActive:(UIApplication *)application {
  [FBSDKAppEvents activateApp];
}

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

Step-6. Now we need to modify our Login Controller, where we do Login task

6.1 Add these imports in Login ViewController.m

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

6.2 Add Facebook Login Button

FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[self.view addSubview:loginButton];

6.3 Handle Login button click

-(IBAction)facebookLogin:(id)sender
{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

    if ([FBSDKAccessToken currentAccessToken])
    {
        NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
        [self fetchUserInfo];
    }
    else
    {
        [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"];

                 }
             }
         }];
    }
}

6.4 Get user info (name, email etc.)

-(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 )
                 {
                     //Start you app Todo
                 }
                 else
                 {
                    NSLog(@"Facebook email is not verified");
                 }
             }
             else
             {
                 NSLog(@"Error %@",error);
            }
         }];
    }
}

Step-7. Now you can build project, you should get below screen.

Hope this will help you guys.

References : Thanks to Facebook docs, Stackoverflow posts and Google.

这篇关于在 iOS 9 中使用 Facebook 登录无法打开本机 Facebook 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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