16

我有这些协议方法,

activityViewControllerPlaceholderItem:activityViewController:itemForActivityType:

但他们从来没有被叫过。我如何告诉UIActivityViewController他们打电话给他们?

我也有一个UIActivityItemProvider子类,但是我对谁调用这两种方法感到困惑。我真的很感激一些示例代码,因为我在网上找不到任何东西。:)

4

4 回答 4

30

您可以在任何地方实现协议,即使您的视图控制器也可以。只需使用initWithActivityItems:@[self].

于 2012-10-06T21:59:23.977 回答
20

JotWee的回答帮助了我。

不需要子类化,UIActivityItemSource协议方法可以在实现分享按钮的视图控制器中实现。

添加self活动项目数组非常重要,如下所示(正如 JotWee 建议的那样):

NSArray *activityItems = [NSArray arrayWithObjects:self, url, image, nil];

这是我的最终实现:

视图控制器.h

@interface ViewController : UIViewController <UIActivityItemSource>

视图控制器.m

- (void)shareBarButtonItemClick:(UIBarButtonItem *)sender
{
    NSURL *url = [NSURL URLWithString:@"http://example.com"];
    NSURL *imageUrl = [NSURL URLWithString:@"http://example.com/images/1.jpg"];
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];

    NSArray *activityItems = [NSArray arrayWithObjects:self, url, image, nil];

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];

    [self presentViewController:activityViewController animated:YES completion:nil];
}

- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(NSString *)activityType
{
    return @"Summary Text";
}

- (id)activityViewControllerPlaceholderItem:(UIActivityViewController *)activityViewController
{
    return @"";
}

- (NSString *)activityViewController:(UIActivityViewController *)activityViewController subjectForActivityType:(NSString *)activityType
{
    return @"Subject";
}
于 2015-01-28T08:00:37.153 回答
17

根据文档。您传递给的活动项
-initWithActivityItems:applicationActivities:数组可以是数据对象数组,如字符串或图像,也可以是实现UIActivityItemSource协议的对象数组。

如果您传递一个实现UIActivityItemSource协议的对象数组,那么您的实例UIActivityViewController将在您的活动项目上调用这些方法。这些对象不一定是UIActivityItemProvider. UIActivityItemProvider只是一个符合这个协议的类。

于 2012-10-04T10:27:58.787 回答
0

借鉴JotWeeSihad Begovic提供的内容,这里是Swift 5.0版本,让您ViewController采用该UIActivityItemSource协议并使用sharebarButtonItem 触发与您的 iPhone(s)和/或 iPad(s)上的其他应用程序共享对象:

viewDidLoad您的类的方法中ViewController,实现以下内容:

 navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareBarButtonItemClicked(_:)))

在你的viewDidLoad方法之外的某个地方,但在你的ViewController类中,让我们实现如下shareBarButtonItemClicked方法:

@objc func shareBarButtonItemClicked(_ sender: UIBarButtonItem) {
    //Here, iOS requires that you use self for your array of items 
    let items = [self]
    let activityVC = UIActivityViewController(activityItems: items, applicationActivities: nil)

    //App activities to be excluded from sharing to
    activityVC.excludedActivityTypes = [
        UIActivityType.airDrop,
        UIActivityType.print,
        UIActivityType.saveToCameraRoll,
        UIActivityType.addToReadingList
    ]

    if UIDevice.current.userInterfaceIdiom == .pad {
        if activityVC.responds(to: #selector(getter: UIViewController.popoverPresentationController)) {
            activityVC.popoverPresentationController?.barButtonItem = sender
        }
    }

    self.present(activityVC, animated: true, completion: nil)
}

现在事情变得有趣了。我们需要使您的ViewController类符合UIActivityItemSource协议并实现其所需的方法,如下所示:

extension ReferAFriendViewController: UIActivityItemSource {
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
    return String(describing: "Sharing my awesome app")
}

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
    return String(describing: "Sharing my awesome app")
}

// Now you can modify and use the same above method if you want different messages/info to be presented for different apps user selects to share with. 
// The Secret is utilizing the activityType parameter. 
// You just have to uncomment below method and swap it with above method.

/*
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
    if activityType == .postToTwitter {
    return "Download #MyAwesomeApp via @vickSwift."
    } else if activityType == .mail {
        return String(describing: "My awesome app is lit. So download it")
    } else {
        return String(describing: "Download my awesome app")
    }
}
*/

// Now note that when you share your app via a mail app, you might need to provide `subject` line. 
// In fact, the `UIActivityItemSource` protocol provide us an optional delegate method to accomplish just that; implemented below: 
func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivityType?) -> String {
    return String(describing: "My Awesome app email's subject title")
}
}

这就是您采用类以符合 UIActivityItemSource 协议并实现其方法的方式。要阅读更多内容,我发现HackingWithSwift网站上的这篇网络文章非常有见地。

快乐编码!

于 2019-01-10T22:58:52.837 回答