简体   繁体   中英

Not able to implement “real” facebook SSO on iOS

My goal:

  • In my app I want users to be able to share stuff on theirs walls

Since facebook is built into iOS 6 directly, I want to utilise the login credentials from the settings screen on iOS so that nobody needs to enter their username / password in my app.

I want that my users jump directly to the permissions dialog on facebook.

Somebody told me to utilise the defaults object to access those login credentials:

thzAppDelegate *delegate = (thzAppDelegate *)[[UIApplication sharedApplication] delegate];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if([defaults objectForKey:@"FBAccessTokenKey"]
       && [defaults objectForKey:@"FBExpirationDateKey"]){
        [delegate facebook].accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        [delegate facebook].expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

but this is NOT working: Even when I reset my simulator, add my username pass in the settings on iOS, I still get a LOGIN SCREEN in MY APP, but I expected only the permissions.

I added also the recommended lines in the delegate.m:

#pragma mark Facebook

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return [self.facebook handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [self.facebook handleOpenURL:url];
}

I DONT want to bother my users EVERY time with a login.

What do I do wrong?

I suggest you look at Accounts.framework and Social.frameworks - the former provides you access to the user's social accounts through the ACAccount class, which you can then use to create SLRequest objects to run your social requests.

If you just want to share content on a wall, you might be able to bypass these steps completely and use the SLComposeViewController , which provides a modal view to share media via Facebook, Twitter, or Weibo. Apple have documentation and sample code available for all of these classes.

Your using the facebook sdk code. You want to use something like this which utilizes the iOS 6 social framework.

#import <Accounts/Accounts.h>
#import <Social/Social.h>


ACAccountStore *accountStore = [[[ACAccountStore alloc] init] autorelease];

 ACAccountType * facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    NSArray * permissions = @[@"publish_stream"];

    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"whateverYourFacebookKeyIs",ACFacebookAppIdKey,permissions,ACFacebookPermissionsKey,ACFacebookAudienceFriends,ACFacebookAudienceKey, nil];

    [accountStore requestAccessToAccountsWithType:facebookAccountType options:dict completion:^(BOOL granted, NSError *error) {
        if (granted && error == nil) {
            /**
             * Now we can ask for more permissions
             **/
            NSArray *readPermissions = @[@"read_stream"];
            [dict setObject:readPermissions forKey: ACFacebookPermissionsKey];

            [accountStore requestAccessToAccountsWithType:facebookAccountType options:dict completion:^(BOOL granted, NSError *error) {
                if(granted && error == nil) {

                } else {
                    NSLog(@"error is: %@",[error description]);
                    [self performSelectorOnMainThread:@selector(showFacebookAlert) withObject:nil waitUntilDone:NO];
                }
            }];
        } else {
            NSLog(@"error is: %@",[error description]);
            [self performSelectorOnMainThread:@selector(showFacebookAlert) withObject:nil waitUntilDone:NO];
        }
    }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM