FBSDKLogin 无法正常工作重定向到 Safari,但随后结果被取消
Posted
技术标签:
【中文标题】FBSDKLogin 无法正常工作重定向到 Safari,但随后结果被取消【英文标题】:FBSDKLogin is not working properly redirect to Safari, but then the result is cancelled 【发布时间】:2017-09-19 10:31:04 【问题描述】:我正在尝试在我的应用中实现 Facebook 登录。我已经完成了 Facebook 开发人员页面向我推荐的所有工作,但我无法正确完成这项工作。
我已经将我的包标识符放入 Facebook 开发应用程序中
Facebook 配置:
Xcode 项目配置:
在我的 AppDelegate 中,我输入了 Facebook 指示的代码
#import <FBSDKCoreKit/FBSDKCoreKit.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
[[FBSDKApplicationDelegate sharedInstance] application:application
didFinishLaunchingWithOptions:launchOptions];
return YES;
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication: sourceApplication
annotation: annotation
];
return handled;
- (void)applicationDidBecomeActive:(UIApplication *)application
[FBSDKAppEvents activateApp];
还把登录按钮放在 Facebook 文档中显示
-(void) viewDidLoad()
self.loginButton = [[FBSDKLoginButton alloc] init];
self.loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
[self.loginButton addTarget:self action:@selector(buttonFacebookLoginClicked:) forControlEvents:UIControlEventTouchUpInside];
self.manager =[[FBSDKLoginManager alloc] init];
但是在buttonFacebookLoginClicked
方法中,当我处理响应时,我总是将result.isCancelled
设为true。
在应用程序中,当我单击 FB 登录按钮时,我被重定向到 safari 中不显示任何内容的新选项卡,然后当我单击确定按钮时,结果被取消。
我的 Facebook 登录按钮:
Safari 视图:
有我的登录功能,在 Safari 中点击 OK 按钮后,如果 result.isCancelled 为 true,我总是放弃
- (IBAction)buttonFacebookLoginClicked:(id)sender
if ([FBSDKAccessToken currentAccessToken] != nil)
[self.manager logOut];
//return
[self.manager logInWithReadPermissions: self.loginButton.readPermissions fromViewController: self handler: ^(FBSDKLoginManagerLoginResult* result, NSError* error )
if (error != nil)
//According to Facebook:
//Errors will rarely occur in the typical login flow because the login dialog
//presented by Facebook via single sign on will guide the users to resolve any errors.
// Process error
[self.manager logOut];
else if (result.isCancelled)
// Handle cancellations
for (NSString * p in self.loginButton.readPermissions)
NSLog(@"PERMISSION %@", p);
[self.manager logOut];
else
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
BOOL allPermsGranted = true;
//result.grantedPermissions returns an array of _NSCFString pointers
NSArray *grantedPermissions = [[result grantedPermissions] allObjects];
// let grantedPermissions = result.grantedPermissions.allObjects.map( "\($0)" )
for (NSString *permission in self.loginButton.readPermissions)
for (NSString* grantedPerm in grantedPermissions)
NSRange range = [permission rangeOfString:grantedPerm];
if (range.length == 0)
allPermsGranted = false;
break;
if (allPermsGranted)
// Do work
NSString * fbToken = [[result token] tokenString];
NSString * fbUserID = [[result token] userID];
FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc]
initWithGraphpath:@"/me"
parameters:@@"fields": @"id,name,email"
HTTPMethod:@"GET"];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
NSLog(@"result %@", result);
NSDictionary *dict = @
@"email" : @"email" ,
@"providerID" : @"id",
@"username" : @"name",
@"provider" : @"facebook"
;
NSLog(@"DICT %@", dict);
[self.delegate loginWithCredentials:dict];
[self logUserLoggedInWithFacebookEvent];
];
else
//The user did not grant all permissions requested
//Discover which permissions are granted
//and if you can live without the declined ones
NSLog(@"FACEBOOK LOGIN ERROR");
];
我认为这是一个较少的错误,但我找不到它。
【问题讨论】:
请问,为什么不使用 Account 框架作为更好的选择?如果您想使用 FB SDK,还有一个问题,您是否配置了 .用于 Facebook sdk 的 PLIST? 你缺少这部分developers.facebook.com/docs/ios/getting-started/#xcode\ 我已经在我的 .plist 文件 @Gagan_iOS 中这样做了 如果你实现了buttonFacebookLoginClicked:
方法和FBSDKLoginManager
的委托方法中的代码,你能展示一下吗?
这是我为您上传的工作代码drive.google.com/file/d/0B7lSqbQq9-WuYU9qNlhOdWczU2M/… 去下载。它已与您的 FB 应用程序 ID 集成
【参考方案1】:
您能检查一下目标设置中的“功能”部分吗?请启用“钥匙串共享”选项。因为 FBSDK 使用钥匙串来存储访问令牌。
【讨论】:
是的,钥匙串共享选项已启用,但仍无法正常工作@Swap【参考方案2】:在我看来,我们可能缺少 URL 类型信息或 Info.plist 文件中的其他信息。通常,您需要指定您的应用程序可以处理哪种方案作为回调,因此一旦您登录,safari 就会使用正确的信息回调您的应用程序。
编辑:在我的例子中,混淆数据是一个类似fb<number>
的字符串。通常在其他 OAUTH 提供程序中显示为“重定向 url”。
【讨论】:
好的!在您的问题中没有看到它们。其他问题可能与具有不同的构建环境(调试、发布、阶段...)有关,Info.plist 可能因每个环境而异。祝你好运。找到答案后发表!以上是关于FBSDKLogin 无法正常工作重定向到 Safari,但随后结果被取消的主要内容,如果未能解决你的问题,请参考以下文章