从“设置”访问帐户时,不使用SLComposeviewcontroller将图像发布到Facebook

时间:2014-07-31 15:48:46

标签: ios facebook slcomposeviewcontroller

我只是挂在问题上,在Facebook上分享图片而不使用SLComposeviewcontroller。我在设置中添加了一些Facebook帐户。虽然图像共享我的应用程序首先搜索帐户,如果它在设置中,则它会提取该帐户,如果没有,则它允许用户使用网络登录。但是假设如果从设置中提取帐户,那么如何在不使用SLComposeviewcontroller的情况下共享图像。因为当我从设置中获取帐户时,我看到的地方没有活动会话,因此它打开了网络。如果不像SLComposeviewcontroller那样打开session,我们是否可以不在该帐户上发布图像?

任何建议都将不胜感激。

Thanks- Ashutosh说

1 个答案:

答案 0 :(得分: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];

            }

        }];

这就是全部。

由于