Search code examples
iosobjective-cfacebook-graph-apiwatchkitapple-watch

Give iOS app permanent permission to post to Facebook


I have integrated Facebook sharing using SLRequest into my iOS app. Everything works fine, but everytime I want to post, the user is shown a hint asking whether the app should be allowed to post on his behalf.

For some reasons (I'm posting from an Apple Watch), it would be important to get permanent permission to post. Otherwise it would be a very bad user experience.

So I'm looking after a possibility to permanently enable posting on the users behalf for my app.

- (void)postToFacebook:(NSString *)postMessage toAudience:(int)audienceIndex {

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

ACAccountType *accountTypeFacebook =
[accountStore accountTypeWithAccountTypeIdentifier:
 ACAccountTypeIdentifierFacebook];

NSArray *audienceArray = [[NSArray alloc] initWithObjects:@"ACFacebookAudienceEveryone", @"ACFacebookAudienceFriends", @"ACFacebookAudienceOnlyMe", nil];

NSDictionary *options = @{ACFacebookAppIdKey: @"<HERE'S MY FACEBOOK APP ID>", ACFacebookPermissionsKey: @[@"publish_actions"], ACFacebookAudienceKey: [audienceArray objectAtIndex:audienceIndex]};

[accountStore requestAccessToAccountsWithType:accountTypeFacebook options:options completion:^(BOOL granted, NSError *error) {

    if(granted) {

        NSArray *accounts = [accountStore accountsWithAccountType:accountTypeFacebook];
        ACAccount *facebookAccount = [accounts lastObject];

        NSDictionary *parameters =
        @{@"access_token":facebookAccount.credential.oauthToken,
          @"message": postMessage};

        NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];

        SLRequest *feedRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:feedURL parameters:parameters];

        [feedRequest  performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
            NSLog(@"Request failed, %@", [urlResponse description]);
        }];

    } else {

        NSLog(@"Access Denied");
        NSLog(@"[%@]",[error localizedDescription]);

    }
}];

}

Solution

  • Unfortunately, there is no longer the notion of "permanent permission to post to Facebook".

    The offline_access permission is deprecated and was removed December 5th, 2012 (originally scheduled for July 5th).

    See: Developer Docs > Migration > Remove offline_access Permission

    It was replaced by a new 60-day long term access token.

    See: Developer Docs > Facebook Login > Access Tokens

    Short-Term and Long-Term Tokens

    User access tokens come in two forms: short-lived tokens and long-lived tokens. Short-lived tokens usually have a lifetime of about an hour or two, while long-lived tokens usually have a lifetime of about 60 days. You should not depend on these lifetimes remaining the same - the lifetime may change without warning or expire early. See more under handling errors.

    Access tokens generated via web login are short-lived tokens, but you can upgrade them to long-lived tokens. Converting short-lived tokens to long-lived tokens is covered later in this document under Expiration and Extending Tokens.

    Mobile apps that use Facebook's mobile SDKs get long-lived tokens.

    Essentially, mobile applications using newer SDKs will automatically be granted a user access_token with the longer expiration. So long as a user revisits your app within 60 days, you will be granted a new user access_token with a fresh expiration time. If your users wait longer than 60 days, they will have to log in again.