0

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

它会自动设置为 No。当用户单击 Yes 时,它会运行以下代码:

-(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);
                                           }
                                       }];

}

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

然后,在稍后的代码中,当查看页面时,如果 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 中删除权限或更改密码等事情。是否需要在获得权限的第一组代码中处理?因为,基本上这组代码只运行一次,其余时间它只运行代码以发布到墙上。

4

0 回答 0