我一直在将我的应用程序从旧的Facebook转换为新的FacebookSDK3.1,以便使用iOS6本机功能以及其他Facebook功能,而且我遇到了一个问题,这个问题一直发生在模拟器上,很少发生在设备上。
当我登录到Facebook时,而不是通过Safari或Facebook应用程序进行后台访问和身份验证,我会在我的应用程序之上获得一个模式窗口,用于登录(我相信这是从iOS6开始的,甚至在切换到FacebookSDK3.0/3.1之前就开始了)。这个窗口允许我输入用户名和密码,但是当我点击右上角的“in”进行身份验证时,什么都不会发生。application:openURL:sourceApplication:annotation:在AppDelegate中不会发生回调。

在我的共享FacebookController代码(用于多个项目)中,当我试图按照Facebook的示例代码授权时,我正在初始化FBSession:
FBSession *session = [[FBSession alloc] initWithAppID:nil permissions:readPermissions defaultAudience:FBSessionDefaultAudienceNone urlSchemeSuffix:self.fbUrlSchemeSuffix tokenCacheStrategy:nil];
if(session.state == FBSessionStateCreated || session.state == FBSessionStateCreatedTokenLoaded)
{
[FBSession setActiveSession:session];
// we open after the fact, in order to avoid overlapping close
// and open handler calls for blocks
[session openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
...完成处理程序的代码永远不会执行,除非我单击对话框上的x cancel按钮(在这种情况下,我得到了预期的错误)。
我在我的FacebookAppID目标属性(info.plist)中设置了URL,并且使用fb###url_scheme_suffix设置了URL方案。
我的代码和Facebook示例代码之间唯一的区别是,我使用的是url方案后缀,因为我有一个免费的付费版本的应用程序。
如果应用程序使用Safari登录,它可以按照给定的代码工作。如果它能快速切换到Facebook应用程序,它也能工作。此外,共享的facebook ios6帐户设置也可以工作。应用程序只有在使用in模式时才会失败。
还有其他人遇到过这个吗?
发布于 2012-10-23 13:27:52
同样的事情也发生在我身上。它只发生在模拟器(iphone5 4英寸,ios6)上,所以我不知道这个问题是否会出现在实际设备中。
经过几次调试之后,我发现FBDialog的webView:shouldStartLoadWithRequest:navigationType:方法没有处理携带access_token的url。当facebook通过access_token时,这意味着您已经通过了身份验证。所以,我想,那里少了一些东西。
下面是未被处理的url的形状:
fb122222222222222://authorize/#access_token=...&expires_in=5102464&code=...
请注意,url方案与您在应用程序.中的Info.plist文件中配置的fb+API密钥相同。
这里是我为解决这个问题所做的。请注意,我添加了一个新的else-if分支,在该分支中,如果我们有访问令牌,则调用对话框成功:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSURL* url = request.URL;
if ([url.scheme isEqualToString:@"fbconnect"]) {
if ([[url.resourceSpecifier substringToIndex:8] isEqualToString:@"//cancel"]) {
NSString * errorCode = [self getStringFromUrl:[url absoluteString] needle:@"error_code="];
NSString * errorStr = [self getStringFromUrl:[url absoluteString] needle:@"error_msg="];
if (errorCode) {
NSDictionary * errorData = [NSDictionary dictionaryWithObject:errorStr forKey:@"error_msg"];
NSError * error = [NSError errorWithDomain:@"facebookErrDomain"
code:[errorCode intValue]
userInfo:errorData];
[self dismissWithError:error animated:YES];
} else {
[self dialogDidCancel:url];
}
} else {
if (_frictionlessSettings.enabled) {
[self dialogSuccessHandleFrictionlessResponses:url];
}
[self dialogDidSucceed:url];
}
return NO;
}
//THIS IS WHAT I'VE ADDED>>>>
else if ([[[[[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleURLTypes"] firstObject] objectForKey:@"CFBundleURLSchemes"] firstObject] isEqual:url.scheme])
{
[self dialogDidSucceed:url];
return NO;
}
//<<<<<
else if ([_loadingURL isEqual:url]) {
return YES;
} else if (navigationType == UIWebViewNavigationTypeLinkClicked) {
if ([_delegate respondsToSelector:@selector(dialog:shouldOpenURLInExternalBrowser:)]) {
if (![_delegate dialog:self shouldOpenURLInExternalBrowser:url]) {
return NO;
}
}
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
} else {
return YES;
}
}值得一提的是,我遵循了所有fb准则(3.1),并且我没有对src代码做任何进一步的更改。
有了这些更改,fb登录过程就会像预期的那样工作。
我希望这能帮到你!
https://stackoverflow.com/questions/12712824
复制相似问题