我正在关注 Facebook's own tutorial对于 iOS (Objective-C) 中的 FB 登录,但每次我登录时 - 在初始权限授权屏幕之后 - 我都会收到臭名昭著的“您已经授权此应用程序” WebView 。
我已经阅读了大量的帖子,但我无法将其整理出来,因此(重新)发布。我觉得这很奇怪,因为该应用程序除了样板登录代码外什么都没有。这种行为在模拟器和真实设备中都会发生。
此应用适用于 iOS 9.0,我使用的是 FBSDK 4.7.1(通过 CocoaPods 安装):
pod 'FBSDKLoginKit', '~> 4.6'
代码本身就是样板文件,这是我的 AppDelegate.m:
#import "AppDelegate.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
[FBSDKAppEvents activateApp];
}
- (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 {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
@end
和我的 ViewController.m:
#import "ViewController.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.readPermissions = @[@"public_profile", @"email", @"user_friends"];
[loginButton setLoginBehavior:FBSDKLoginBehaviorSystemAccount]; // No difference
loginButton.center = self.view.center;
[self.view addSubview:loginButton];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
以及 Info.plist 的屏幕截图:
最后,我还在控制台中收到了这两个错误:
2015-11-24 11:50:42.855 lixo[26941:389756] -canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
2015-11-24 11:50:42.860 lixo[26941:389756] -canOpenURL: failed for URL: "fbauth2:/" - error: "(null)"
尽管根据我的阅读,如果在 Info.plist 中设置了 LSApplicationQueriesSchemes,则可以安全地忽略它。
有什么办法可以避免这种情况吗?
最佳答案
您可以检查访问 token 的状态,而不是在每次启动时触发登录。如果未过期,则跳过登录,直接开始使用 API。可以使用以下方法检查 token 是否过期:
+(BOOL) accessTokenIsLocalyValid
{
return [FBSDKAccessToken currentAccessToken] && [[NSDate date] compare:[FBSDKAccessToken currentAccessToken].expirationDate] == NSOrderedAscending;
}
如果您有一个有效的 token ,您可以使用 [FBSDKAccessToken refreshCurrentAccessToken:]
刷新它。
关于ios - FBSDK 4.7.1登录时"You have already authorized yourApp",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33893338/