我正在为 iPhone 构建一个文章阅读应用程序。我陷入了一些问题。我在社交分享方面遇到了问题。我尝试了几种方法。第一种方法
我们正在使用 Facebook sdk 来分享文章链接、标题和图片。理想情况下,当我们在 uiactivityviewcontroller 中单击 Facebook 活动项时,我的 Facebook sdk 代码应该会运行,但我无法这样做。
第二种方法
我们尝试了 uiactivityviewcontroller 中的自定义活动项,并成功制作了自定义图标并对作为类 uiactivity(- (void) performActivity) 的默认方法的操作进行了操作,但我想在我的视图控制器类中使用此方法我们通过 segue 传递文章链接、标题和图像。
这是我的代码:
#import "ysAPActivityIcon.h"
#import <Social/Social.h>
#import <FacebookSDK/FacebookSDK.h>
@implementation ysAPActivityIcon
- (NSString *)activityType { return @"it.albertopasca.myApp"; }
- (NSString *)activityTitle { return @"Open Maps"; }
- (UIImage *) _activityImage { return [UIImage imageNamed:@"iPad_jobs.png"]; }
- (BOOL) canPerformWithActivityItems:(NSArray *)activityItems { return YES; }
- (void) prepareWithActivityItems:(NSArray *)activityItems { }
- (UIViewController *) activityViewController { return nil; }
- (void) performActivity {
}
// my view controller class:
#import "ysAPActivityProvider.h"
#import "ysAPActivityIcon.h"
#import "ysDetailViewController.h"
@interface ysDetailViewController() <UIWebViewDelegate,UIActivityItemSource>
- (void) performActivity {
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",
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"]];
// NSLog(@"result %@", result);
}
}
}];
}
}