0

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

                                          }];

           }



      }
4

1 回答 1

0

在自定义 UIActivity 的 .h 文件中,创建标题、链接、图像所需的变量...在 ViewController 的实现文件中导入 UIActivity 的头文件,并在按钮的操作方法中创建一个实例班上。假设 UIActivity 头文件名为 customAct:

customAct *act = [[customAct alloc] init];

然后用它给头文件中的变量添加数据:

act.titleString = @"Text you want to add"

然后在 performActivity 方法中使用这些变量。

但如果我理解得很好,您正在尝试分享到 Facebook。默认情况下,UIActivityController 使您能够在 facebook、twitter、邮件、消息上共享。永远要让 facebook 分享按钮出现,你必须在设置中登录到你的 facebook 帐户,对于 twitter 也是如此。另一方面,UIActivtyController 不提供通过 whatsapp、instagram 共享的能力……因为您必须进行自定义活动

于 2015-04-10T12:39:38.987 回答