Search code examples
iosfacebookslcomposeviewcontroller

Post Image to Facebook without using SLComposeviewcontroller when account accessed from Settings


I am just hanging with a problem to share image on Facebook without using SLComposeviewcontroller. I have added some account of Facebook in settings. While image sharing My app first searches the account if it is in settings then it fetches that account if not then it enables user to login with web. But suppose if it fetches account from setting then how can I be able to share image without using SLComposeviewcontroller. Because where as I am seeing that when it fetches account from setting then there is no active session and for that it opens the web. Can we don't post the image on that account without opening session as SLComposeviewcontroller does?

Any suggestion would be appreciated.

Thanks- Ashutosh


Solution

  • I think it was simple as I found. Just fetch the information from FB settings. then make SLRequest to post on that account wall.

    Code is written below-

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

    That's all.

    Thanks