0

我只是在Facebook不使用SLComposeviewcontroller. 我Facebook在设置中添加了一些帐户。在图像共享时,我的应用程序首先搜索该帐户,如果它在设置中,然后它会获取该帐户,如果没有,然后它使用户能够使用 Web 登录。但是假设如果它从设置中获取帐户,那么我如何能够在不使用 SLComposeviewcontroller. 因为正如我所看到的,当它从设置中获取帐户时,没有活动会话,因此它打开了网络。我们不能session像 SLComposeviewcontroller 那样在没有打开的情况下在该帐户上发布图像吗?

任何建议将不胜感激。

谢谢-阿舒托什

4

1 回答 1

0

我认为这很简单,因为我发现了。只需从 FB 设置中获取信息。然后制作SLRequest在该帐户墙上发布。

代码写在下面-

if (self.accountStore == nil) {
    self.accountStore = [[ACAccountStore alloc] init];
}
ACAccountType * facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

NSArray * permissions = @[@"email",@"publish_actions"];
NSDictionary * dict = @{ACFacebookAppIdKey : FacebookAppId, ACFacebookPermissionsKey : permissions, ACFacebookAudienceKey : ACFacebookAudienceEveryone};
[self.accountStore requestAccessToAccountsWithType:facebookAccountType options:dict completion:^(BOOL granted, NSError *error) {
    if (granted)
    {
        NSLog(@"granted");
        NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
        ACAccount *account = [accounts lastObject];

        // Get the access token, could be used in other scenarios
        ACAccountCredential *fbCredential = [account credential];
        NSString *accessToken = [fbCredential oauthToken];
        NSLog(@"Facebook Access Token: %@", accessToken);


        NSMutableDictionary*params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                      @"post", @"type",
                                      descriptionTxtV.text, @"message",
                                      descriptionTxtV.text, @"description",
                                      nil];

        NSData *myImageData = UIImagePNGRepresentation(postImgV.image);

        SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://graph.facebook.com/me/photos"] parameters:params];
        [facebookRequest addMultipartData: myImageData withName:@"source" type:@"multipart/form-data"filename:@"TestImage"];
        [facebookRequest setAccount:account];

        [facebookRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
            if (responseData)
            {
                NSError *jsonError =nil;
                NSDictionary *jsonDict = [ NSJSONSerialization JSONObjectWithData :responseData options : NSJSONReadingAllowFragments error:&jsonError];
                NSLog(@"-- json: %@", jsonDict);
                [descriptionTxtV resignFirstResponder];

                UIAlertView*    aAlert = [[UIAlertView alloc] initWithTitle:APP_NAME message:@"You have shared phots to Facebook successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [aAlert show];
                [self.navigationController popToRootViewControllerAnimated:YES];

            }
            else
            {
                UIAlertView*    aAlert = [[UIAlertView alloc] initWithTitle:APP_NAME message:@"Your Facebook account details can't be accessed right now. Please try later." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [aAlert show];

            }

        }];

就这样。

谢谢

于 2014-08-01T16:08:40.213 回答