我正在尝试正确使用iOS Facebook-SDK,我尝试了Facebook的每一个样本,也尝试了网络上的内容,并且忘了我做错了什么。
我想要达到的目的是仅在提出请求时才要求获得Facebook许可。
目前唯一一个“工作”但没有遇到会话问题的解决方案是直接在应用启动时请求权限。那不是我想要的。
许多应用程序都使用了我想要的东西。也许是因为他们不使用系统facebook权限?他们去Safari并切换回来。感谢Anyhelp!
这是我的实际代码:
// ****************************************************************************
// App switching methods to support Facebook Single Sign-On.
// ****************************************************************************
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// Facebook SDK * login flow *
// Attempt to handle URLs to complete any auth (e.g., SSO) flow.
return [FBAppCall handleOpenURL:url sourceApplication:sourceApplication fallbackHandler:^(FBAppCall *call) {
// Facebook SDK * App Linking *
// For simplicity, this sample will ignore the link if the session is already
// open but a more advanced app could support features like user switching.
if (call.accessTokenData) {
if ([FBSession activeSession].isOpen) {
NSLog(@"INFO: Ignoring app link because current session is open.");
}
else {
[self handleAppLink:call.accessTokenData];
}
}
}];
}
// Helper method to wrap logic for handling app links.
- (void)handleAppLink:(FBAccessTokenData *)appLinkToken {
// Initialize a new blank session instance...
//Open session
NSArray *permissionsArray = @[ @"user_about_me", @"user_relationships", @"user_birthday", @"user_location"];
FBSession *appLinkSession = [[FBSession alloc] initWithAppID:nil
permissions:permissionsArray
defaultAudience:FBSessionDefaultAudienceNone
urlSchemeSuffix:nil
tokenCacheStrategy:[FBSessionTokenCachingStrategy nullCacheInstance] ];
[FBSession setActiveSession:appLinkSession];
// ... and open it from the App Link's Token.
[appLinkSession openFromAccessTokenData:appLinkToken
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// Forward any errors to the FBLoginView delegate.
if (error) {
NSLog(@"%@", error);
}
}];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
/*
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
*/
// Clear badge and update installation, required for auto-incrementing badges.
if (application.applicationIconBadgeNumber != 0) {
application.applicationIconBadgeNumber = 0;
[[PFInstallation currentInstallation] saveInBackground];
}
// Clears out all notifications from Notification Center.
[[UIApplication sharedApplication] cancelAllLocalNotifications];
application.applicationIconBadgeNumber = 1;
application.applicationIconBadgeNumber = 0;
NSArray *permissionsArray = @[ @"user_about_me", @"user_relationships", @"user_birthday", @"user_location"];
FBSession *appLinkSession = [[FBSession alloc] initWithPermissions:permissionsArray];
[FBSession setActiveSession:appLinkSession];
// ... and open it from the App Link's Token.
[appLinkSession openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// Forward any errors to the FBLoginView delegate.
if (error) {
NSLog(@"%@", error);
}
}];
}
答案 0 :(得分:0)
好的,这是我的代码: 在成功块中做任何你喜欢的事。
+ (void)checkPublishPermission:(NSString*)permission success:(void(^)(void))successCallback fail:(void(^)(void))failCallback
{
// Check for publish permissions
[FBRequestConnection startWithGraphPath:@"/me/permissions"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error){
NSDictionary *permissions= [(NSArray *)[result data] objectAtIndex:0];
if (![permissions objectForKey:permission]){
// permission not found, ask for it
[FBHelper requestPublishPermission:permission success:successCallback fail:failCallback];
} else {
// Publish permissions found, publish the OG story
successCallback();
}
} else {
// There was an error, handle it
// See https://developers.facebook.com/docs/ios/errors/
NSLog(@"%@", [error debugDescription]);
}
}];
}
+ (void)requestPublishPermission:(NSString*)permission success:(void(^)(void))successCallback fail:(void(^)(void))failCallback
{
// Request publish_actions
[FBSession.activeSession requestNewPublishPermissions:[NSArray arrayWithObject:permission]
defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
if ([FBSession.activeSession.permissions
indexOfObject:permission] == NSNotFound){
// Permission not granted, tell the user we will not publish
failCallback();
} else {
// Permission granted, publish the OG story
successCallback();
}
} else {
// There was an error, handle it
// See https://developers.facebook.com/docs/ios/errors/
NSLog(@"%@", [error localizedDescription]);
}
}];
}