如何在Objective-C中分享照片到Facebook



我想在我的自定义按钮上点击分享照片,我可以分享链接和消息,但是当涉及到分享照片时,我不能这样做。

我使用最新的Facebook SDK框架(4.15.1)。

这是我的代码在里面的按钮点击。

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

我在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;
}

在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>

当我点击我的自定义按钮Facebook应用程序或safari浏览器没有启动,所以谁能告诉我我在这里错过了什么?

您应该在对内容建模之后自己呈现对话框。

添加这个

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

到您的- (IBAction)btnsharecustom:(id)sender方法的末尾。


注意:下载你的图片需要时间,因为它使用同步请求。

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

阅读这里的文档https://developer.apple.com/reference/foundation/nsdata/1547245-datawithcontentsofurl

不要使用这种同步方法来请求基于网络的url。对于基于网络的url,此方法可能会在慢速网络上阻塞当前线程数十秒,导致糟糕的用户体验,并且在iOS中,可能会导致您的应用程序被终止。

这个问题的解决方案可以是在用户点击按钮之前异步下载你的图像(如果要上传的图像是可预测的)。通常你可以在viewDidLoad方法中做到这一点。试试这个https://github.com/rs/SDWebImage库,让异步请求图像变得更容易。

试试这段代码。

- (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{
}

import

//委托FBSDKSharingDelegate

-(void)shareImageWithFacebook:(UIImage *)image {如果(![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"fb://"]]) {//你的设备没有Facebook应用程序。返回;}

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) {}

相关内容

  • 没有找到相关文章