看著文件刻了一下:
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginManager.h>
#import <FBSDKLoginKit/FBSDKLoginManagerLoginResult.h>
- (void)testFB {
if (![FBSDKAccessToken currentAccessToken]) {
NSLog(@"do login");
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithPublishPermissions:@[@"publish_actions"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
// Process error
NSLog(@"Process error");
} else if (result.isCancelled) {
// Handle cancellations
NSLog(@"Handle cancellations: %@, %@, %@", result, result.grantedPermissions, [FBSDKAccessToken currentAccessToken]);
} else {
if ([result.grantedPermissions containsObject:@"publish_actions"]) {
NSLog(@"with publish_actions");
} else {
NSLog(@"without publish_actions");
}
}
}];
return;
} else if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"]) {
NSLog(@"use publish_actions");
}
NSLog(@"token: %@", [FBSDKAccessToken currentAccessToken]);
}
很奇妙地,無論使用者如何授權此 Facebook app ,永遠都會落入 result.isCancelled == true 的事件中,然後 token 永遠都 null。
追了一下,原來還要設定 AppDelegate 太久沒用都忘記了,啊 FB iOS SDK 文件上也沒提醒 Orz
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
如此一來,有幾種情境就都能抓到了:
- 使用者不想授權此 app => Handle cancellations
- 使用者授權此 app ,但不想給 publish_actions 權限 => without publish_actions
- 使用者授權此 app ,並給予 publish_actions 權限 => with publish_actions
沒有留言:
張貼留言