构建一个用于阅读文章的 iOS 应用程序。我正在使用 Facebook SDK 在 Facebook 上分享文章链接和图片。
这是过程:
1.单击右上角的栏按钮项目后,UIActivityViewController
在屏幕底部打开,提供通过 fb、google、链接等共享的选项。
2.单击中的Facebook 按钮,UIActivityVIewController
默认屏幕打开共享,但理想情况下fb SDK 代码应该被执行。
不执行以下“if 条件”。
[AVC setCompletionHandler:^(NSString *activityType, BOOL completed)
{
if([activityType isEqualToString: UIActivityTypePostToFacebook]){
FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
3.但是当点击默认屏幕的取消按钮时,“如果条件”被执行,应用程序能够按预期分享文章。
这是代码。
- (IBAction)ysshareAction:(id)sender {
NSURL *Imageurl = [NSURL URLWithString:_DetailModal1[2]];
NSData *data = [NSData dataWithContentsOfURL:Imageurl];
UIImage *image = [[UIImage alloc] initWithData:data];
NSURL *linkURL = [NSURL URLWithString:_DetailModal1[4]];//article url
NSMutableAttributedString *stringText = [[NSMutableAttributedString alloc] initWithString:_DetailModal1[0]];//_DetailModal1[0] contain article title////
[stringText addAttribute:NSLinkAttributeName value:linkURL range:NSMakeRange(0, stringText.length)];
NSArray *itemArrany = @[stringText,image,];//title is displayed but not as hyperlink.
UIActivityViewController *AVC = [[UIActivityViewController alloc] initWithActivityItems:itemArrany applicationActivities:nil];
AVC.excludedActivityTypes=@[];
[AVC setCompletionHandler:^(NSString *activityType, BOOL completed)
{
if([activityType isEqualToString: UIActivityTypePostToFacebook]){
FBLinkShareParams *params = [[FBLinkShareParams alloc] init];
params.link = [NSURL URLWithString:@"https://www.youtube.com/watch?v=pa8lsBNG31c"];
// // If the Facebook app is installed and we can present the share dialog
if ([FBDialogs canPresentShareDialogWithParams:params]) {
// Present share dialog
[FBDialogs presentShareDialogWithLink:params.link
handler:^(FBAppCall *call, NSDictionary *results, NSError *error) {
if(error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(@"Error publishing story: %@", error.description);
} else {
// Success
NSLog(@"result %@", results);
}
}];
// If the Facebook app is NOT installed and we can't present the share dialog
} else {
// FALLBACK: publish just a link using the Feed dialog
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"YourStory", @"name",
_DetailModal1[0], @"caption",
_DetailModal1[4], @"link",
_DetailModal1[2], @"picture",
nil];
// Show the feed dialog
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {
if (error) {
// An error occurred, we need to handle the error
// See: https://developers.facebook.com/docs/ios/errors
NSLog(@"Error publishing story: %@", error.description);
} else {
if (result == FBWebDialogResultDialogNotCompleted) {
// User canceled.
NSLog(@"User cancelled.");
} else {
// Handle the publish feed callback
// NSDictionary *urlParams = [self parseURLParams:[resultURL query]];
// if (![urlParams valueForKey:@"post_id"]) {
// User canceled.
NSLog(@"User cancelled.");
// } else {
// User clicked the Share button
// NSString *result = [NSString stringWithFormat: @"Posted story, id: %@", [urlParams valueForKey:@"post_id"]];
//
}
}
}];
}
}
}];
[self presentViewController:AVC animated:YES completion:nil];
}
非常感谢任何帮助。