我正在尝试使用Facebook登录,它成功登录,但不能返回到应用程序 [英] I am trying login with Facebook, it succeeds at login but do not return to app

查看:164
本文介绍了我正在尝试使用Facebook登录,它成功登录,但不能返回到应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法再回到应用程序(如下图所示),无法检索任何用户的数据。



我已经搜索,但没有找到任何合适的答案我的查询。



日志的图片: p>



结束如下图所示的图片,不要回到应用程序,没有用户的数据:



解决方案

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

Step-2。将FBSDKCoreKit.framework和FBSDKLoginKit.framework添加到
您的项目。

步骤3。现在去项目>构建阶段>添加
SafariServices.framework

步骤4。 info.plist有三个变化,我们需要验证。

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



< key> CFBundleURLTypes< / key>
< array>
< dict>
< key> CFBundleURLSchemes< / key>
< array>
< string><你的fb id这里例如。 fbxxxxxx>< /串GT;
< / array>
< / dict>
< / array>
< key> FacebookAppID< / key>
< string><你的FacebookAppID>< / string>
< key> FacebookDisplayName< / key>
< string>< Your_App_Name_Here>< / string>

4.2现在添加以下白名单Facebook服务器,这是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添加URL方案
< key> LSApplicationQueriesSchemes< / key>
< array>
< string> fbapi< / string>
< string> fb-messenger-api< / string>
< string> fbauth2< / string>
< string> fbshareextension< / string>
< / array>
Step-5。现在打开AppDelegate.m文件

5.1添加以下import语句

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

5.2更新以下方法



- (BOOL)应用程序:(UIApplication *)应用程序
openURL:(NSURL *) url
sourceApplication:(NSString *)sourceApplication
注释:(id)注释{
return [[FBSDKApplicationDelegate sharedInstance]应用程序:应用程序
openURL:url
sourceApplication:sourceApplication
注释:注释];
}

- (void)applicationDidBecomeActive :( UIApplication *)应用程序{
[FBSDKAppEvents activateApp];
}

- (BOOL)应用程序:(UIApplication *)应用程序didFinishLaunchingWithOptions :( NSDictionary *)launchOptions {
return [[FBSDKApplicationDelegate sharedInstance]应用程序:应用程序
didFinishLaunchingWithOptions :launchOptions];
}

第六步。现在我们需要修改我们的登录控制器,我们在哪里登录任务

6.1在Login ViewController.m中添加这些导入

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

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]);
[self fetchUserInfo];
}

{
[login logInWithReadPermissions:@ [@email] fromViewController:self handler:^(FBSDKLoginManagerLoginResult * result,NSError * error)
{
如果(错误)
{
NSLog(@登录过程错误);
}
else if(result.isCancelled)
{
NSLog(@用户取消登录);
}
else
{
NSLog(@登录成功);

if([result.grantedPermissions containsObject:@email])
{
NSLog(@result is:%@,result);
[self fetchUserInfo];
}
else
{
[SVProgressHUD showErrorWithStatus:@Facebook电子邮件权限错误];

}
}
}];
}
}
6.4获取用户信息(名称,电子邮件等)

- (void)fetchUserInfo
{
if([ FBSDKAccessToken currentAccessToken])
{
NSLog(@令牌可用:%@,[[FBSDKAccessToken currentAccessToken] tokenString]);

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

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

if(email.length> 0)
{
//开始你的应用程序Todo
}
else
{
NSLog(@Facebook电子邮件未验证);
}
}
else
{
NSLog(@Error%@,error);
}
}];
}
}


I cannot get back to app after this (shown in the pictures below) and cannot retrieve any user's data.

I have searched but didn't got any suitable answer to my query.

Log's picture:

Ends up like the picture bellow shown, do not come back to app and there is no data of user:

解决方案

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

#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);
            }
         }];
    }
}

这篇关于我正在尝试使用Facebook登录,它成功登录,但不能返回到应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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