iOS requestAccessToAccountsWithType is Not Showing

2019-02-23 19:31发布

It is my understanding that when I invoke [ACAccountStore requestAccessToAccountsWithType:options:completion], the user is supposed to see an UIAlert that asks them if they grant permission to my app.

When I run this code, there is never a prompt and the granted variable is always "false"

-(void)myfunction { 
if (!_accountStore) _accountStore = [[ACAccountStore alloc] init];

    ACAccountType *fbActType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];


    NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                             (NSString *)ACFacebookAppIdKey, @"##########",  
                             (NSString *)ACFacebookPermissionsKey, [NSArray arrayWithObject:@"email"],  
                             nil];


    [_accountStore requestAccessToAccountsWithType:fbActType options:options completion:^(BOOL granted, NSError *error) {
                                                NSLog(@"Home.m - getFBDetails - Check 2");
                                                if (granted == YES) {
                                                    NSLog(@"Success");
                                                    NSArray *accounts = [_accountStore accountsWithAccountType:fbActType];
                                                    _facebookAccount = [accounts lastObject];                                                    
                                                    [self me];
                                               } else {
                                                    NSLog(@"ERR: %@ ",error);
                                                    // Fail gracefully...
                                              }
            }
     ];

}

And I get the Error: ERR: Error Domain=com.apple.accounts Code=8 "The operation couldn’t be completed. (com.apple.accounts error 8.)"

Update: I did some testing and if I run with using ACAccountTypeIdentifierTwitter, I get the prompt to connect to my Twitter accounts, so I am supposing it is something with my Facebook settings ...

2条回答
\"骚年 ilove
2楼-- · 2019-02-23 19:43

You have inverted objects and keys in your options dictionary :

NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                         @"##########",  ACFacebookAppIdKey,
                         [NSArray arrayWithObject:@"email"], ACFacebookPermissionsKey,
                         nil];
查看更多
女痞
3楼-- · 2019-02-23 19:54

You are actually missing a key: ACFacebookAudienceKey. You can change it like that:

NSDictionary *options = [[NSDictionary alloc] initWithObjectsAndKeys:
                                 (NSString *)ACFacebookAppIdKey, @"##########",  
                                 (NSString *)ACFacebookPermissionsKey, [NSArray arrayWithObject:@"email"],
                                 (NSString *)ACFacebookAudienceKey, ACFacebookAudienceEveryone,
                                 nil];

By the way, when you are using Twitter, the options must be nil.

查看更多
登录 后发表回答