【问题标题】:How can we use uiactivity class method - (void) performActivity in UIViewController class?我们如何在 UIViewController 类中使用 uiactivity 类方法 - (void) performActivity?
【发布时间】:2014-08-20 17:38:08
【问题描述】:

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

                                          }];

           }



      }

【问题讨论】:

    标签: ios objective-c share uiactivityviewcontroller uiactivity


    【解决方案1】:

    在你的自定义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 等进行分享的能力...因为您必须进行自定义活动

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多