0

Facebookを使用せずに画像を共有するのに問題がありますSLComposeviewcontroller。設定にいくつかのアカウントを追加しましFacebookた。画像を共有している間、私のアプリは最初にアカウントを検索し、設定にある場合はそのアカウントを取得し、そうでない場合はユーザーが Web でログインできるようにします。しかし、設定からアカウントを取得する場合、を使用せずに画像を共有するにはどうすればよいでしょうか SLComposeviewcontroller。私が見ているように、設定からアカウントをフェッチすると、アクティブなセッションがなく、そのためにWebが開かれるためです。sessionSLComposeviewcontroller のように開かずにそのアカウントに画像を投稿することはできませんか?

任意の提案をいただければ幸いです。

ありがとう - アシュトッシュ

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 に答える