SLRequest处理Facebook令牌过期等

时间:2013-01-08 22:54:48

标签: ios facebook

在我的应用程序中,我有一个UISegmentControl设置为Yes或No的设置视图,允许使用SLRequest和Accounts Framework自动发布查看到Facebook的页面。它的流程是这样的:

它会自动设置为“否”。当用户单击“是”时,它将运行以下代码:

-(void)facebookpost {
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    // We will pass this dictionary in the next method. It should contain your Facebook App ID key,
    // permissions and (optionally) the ACFacebookAudienceKey
    NSDictionary *options = @{ACFacebookAppIdKey : @"APPID",
    ACFacebookPermissionsKey : @[@"email", @"publish_stream"],
ACFacebookAudienceKey:ACFacebookAudienceFriends};

    // Request access to the Facebook account.
    // The user will see an alert view when you perform this method.
    [accountStore requestAccessToAccountsWithType:facebookAccountType
                                          options:options
                                       completion:^(BOOL granted, NSError *error) {
                                           if (granted)
                                           {
                                               // At this point we can assume that we have access to the Facebook account
                                               NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];

                                               // Optionally save the account
                                               [accountStore saveAccount:[accounts lastObject] withCompletionHandler:nil];
                                           }
                                           else
                                           {
                                               [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"facebook"];
                                               [[NSUserDefaults standardUserDefaults] synchronize];
                                               NSLog(@"Failed to grant access\n%@", error);
                                           }
                                       }];

}

对于用于facebookposting的NSUserDefault键,它还将Bool值设置为YES。

然后,在稍后的代码中,当查看页面时,如果facebookposting的值为YES,则运行此代码。如果不是,它只是从不运行此代码:

-(void)writetowall {
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
    NSArray *accounts = [accountStore accountsWithAccountType:accountType];

    // If we don't have access to users Facebook account, the account store will return an empty array.
    if (accounts.count == 0)
        return;

    // Since there's only one Facebook account, grab the last object
    ACAccount *account = [accounts lastObject];

    // Create the parameters dictionary and the URL (!use HTTPS!)
    NSDictionary *parameters = @{@"message" : @"MY MESSAGE", @"link": _entry.articleUrl};
    NSURL *URL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];

    // Create request
    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                            requestMethod:SLRequestMethodPOST
                                                      URL:URL
                                               parameters:parameters];

    // Since we are performing a method that requires authorization we can simply
    // add the ACAccount to the SLRequest
    [request setAccount:account];

    // Perform request
    [request performRequestWithHandler:^(NSData *respData, NSHTTPURLResponse *urlResp, NSError *error) {
        NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:respData
                                                                           options:kNilOptions
                                                                             error:&error];

        // Check for errors in the responseDictionary
    }];
}

我想知道的是如何处理从Facebook中删除权限或更改密码等问题。是否需要在获得权限的第一组代码中处理?因为,基本上这组代码只运行一次,其余时间只运行代码发布到墙上。

0 个答案:

没有答案