2

I want to share photos upon my custom button click, am able to share links and messages but when it comes to the sharing of photos am not able to do.

I am using latest Facebook SDK framework (4.15.1).

This is my code in inside the button click.

 - (IBAction)btnsharecustom:(id)sender {

//Get aws s3 Image.
NSString* strS3ImageURL = @"https://TEST_IMAGE_URL/1473497380077.jpg";

//Convert to URL.
NSURL* url1 = [NSURL URLWithString:strS3ImageURL];

//Convert it into data.
NSData* data = [NSData dataWithContentsOfURL:url1];

//Convert it to image.
UIImage* image = [UIImage imageWithData:data];    

FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc]init];

photo.image = image;
photo.caption = @"Test Caption";
photo.userGenerated = YES;

FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
content.photos = @[photo];
}

I included this code in app delegate.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.

[[FBSDKApplicationDelegate sharedInstance] application:application
                         didFinishLaunchingWithOptions:launchOptions];    

return YES;
}

//FaceBook URLs.
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
 sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {

BOOL handled = [[FBSDKApplicationDelegate sharedInstance] application:application
                                                              openURL:url
                                                    sourceApplication:sourceApplication
                                                           annotation:annotation
                ];
// Add any custom logic here.
return handled;
}

Also included this code in info.plist

    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb15771408xxx409xx</string>
        </array>
    </dict>

<key>FacebookAppID</key>
<string>15771408xxx409xx</string>
<key>FacebookDisplayName</key>
<string>Cedar iOS</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbapi</string>
    <string>fb-messenger-api</string>
    <string>fbauth2</string>
    <string>fbshareextension</string>
    <string>fbapi20130214</string>
    <string>fbapi20130410</string>
    <string>fbapi20130702</string>
    <string>fbapi20131010</string>
    <string>fbapi20131219</string>
    <string>fbapi20140410</string>
    <string>fbapi20140116</string>
    <string>fbapi20150313</string>
    <string>fbapi20150629</string>
    <string>fbapi20160328</string>
    <string>fbauth</string>
    <string>fb-messenger-api20140430</string>
</array>

When I click upon my custom button Facebook App or safari browser is not launching, so can anyone tell me what I am missing here?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ganesh
  • 89
  • 3
  • 14

3 Answers3

3

You should present the dialog yourself after modelling the content.

Add this

[FBSDKShareDialog showFromViewController:self
                          withContent:content
                             delegate:nil];

to the end of your - (IBAction)btnsharecustom:(id)sender method.


Note: It will take time to download your image because it uses synchronous request.

//Convert it into data.
NSData* data = [NSData dataWithContentsOfURL:url1];

Read the documentation here https://developer.apple.com/reference/foundation/nsdata/1547245-datawithcontentsofurl

Do not use this synchronous method to request network-based URLs. For network-based URLs, this method can block the current thread for tens of seconds on a slow network, resulting in a poor user experience, and in iOS, may cause your app to be terminated.

A solution for this problem can be by downloading your image asynchronously before user tap button (if the image to upload is predictable). Usually you can do this in viewDidLoad method. Try this https://github.com/rs/SDWebImage library to make it easy for async request image.

ramacode
  • 914
  • 6
  • 14
  • Thank you for your answer. I have 2 more doubts, Yes when am adding your code am getting the dialog. so my 2 doubts are 1. Am mentioning the caption(you can see in my code), but still that is not present in my launched dialog. 2nd one is, If Facebook app is not installed then what is the code to share it through browser like safari ? – Ganesh Oct 07 '16 at 07:36
  • 1. Caption is a description per photo, so it will not shown in launch dialog because you may have more than one photo. However, I still don't know how to add the text in the dialog. 2. It will automatically detect that. You can see the documentation here https://developers.facebook.com/docs/sharing/ios – ramacode Oct 07 '16 at 10:10
  • Yes that documentation didn't help much. they didn't mention in detail, am failing to set permission & caption – Ganesh Oct 07 '16 at 11:44
0

try this code.

- (IBAction)btnsharecustom:(id)sender {

    //Get aws s3 Image.
    NSString* strS3ImageURL = @"https://placeholdit.imgix.net/~text?txtsize=15&txt=image1&w=120&h=120";

    //Convert to URL.
    NSURL* url1 = [NSURL URLWithString:strS3ImageURL];

    //Convert it into data.
    NSData* data = [NSData dataWithContentsOfURL:url1];

    //Convert it to image.
    UIImage* image = [UIImage imageWithData:data];

    FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc]init];

    photo.image = image;
    photo.caption = @"Test Caption";
    photo.userGenerated = YES;

    FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
    content.photos = @[photo];
    [FBSDKShareAPI shareWithContent:content delegate:self];
;
}
- (void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results{

}
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • This will share the post directly without showing a dialog and it needs user to extend permission to publish directly. https://developers.facebook.com/docs/facebook-login/permissions/review#reference-extended – ramacode Oct 07 '16 at 10:13
  • Am getting exception in Above code, extending permission is required or anything else ? FBShareObj1 FBSDKLog: Warning: Access token is missing publish_actions permissions FBShareObj1 -[ViewController sharer:didFailWithError:]: unrecognized selector sent to instance 0x175ce3d0 FBShareObj1 *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController sharer:didFailWithError:]: unrecognized selector sent to instance 0x175ce3d0' *** First throw call stack: libc++abi.dylib: terminating with uncaught exception of type NSException – Ganesh Oct 07 '16 at 11:33
  • add following delegate method for sharing : - (void)sharer:(id)sharer didFailWithError:(NSError *)error{ } – KKRocks Oct 07 '16 at 11:37
  • @KKRocks thank you for your answer, Yes I have included that method after the buttonshare action method, getting error something like this, "com.facebook.sdk:FBSDKErrorDeveloperMessageKey" : @"An active access token must be used to query information about the current user." – Ganesh Oct 07 '16 at 12:07
  • Please checek this i think something wrong in your appdelegate.https://developers.facebook.com/docs/ios/getting-started/#app-delegate – KKRocks Oct 07 '16 at 12:15
  • I have added code in didifinish, then also same., Updated question. – Ganesh Oct 10 '16 at 07:02
0

import

// Delegate FBSDKSharingDelegate

-(void)shareImageWithFacebook:(UIImage *)image { if(![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) { //Your device doesn't have Facebook app. return; }

FBSDKSharePhoto *photo = [FBSDKSharePhoto alloc];
photo.image = _imageSaved;
[photo setUserGenerated:YES];
photo.caption = AppName;
FBSDKSharePhotoContent *photoContent = [FBSDKSharePhotoContent alloc];
photoContent.photos = @[photo];

FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
dialog.fromViewController = self;
dialog.delegate = self;
dialog.shareContent = photoContent;
[dialog show];

}

pragma mark- FBShareKit Delegate

  • (void)sharer:(id)sharer didCompleteWithResults:(NSDictionary *)results { }
  • (void)sharer:(id)sharer didFailWithError:(NSError *)error { }
  • (void)sharerDidCancel:(id)sharer { }
Tajinder singh
  • 738
  • 1
  • 9
  • 18