我有这些协议方法,
activityViewControllerPlaceholderItem:
和activityViewController:itemForActivityType:
但他们从来没有被叫过。我如何告诉UIActivityViewController
他们打电话给他们?
我也有一个UIActivityItemProvider
子类,但是我对谁调用这两种方法感到困惑。我真的很感激一些示例代码,因为我在网上找不到任何东西。:)
您可以在任何地方实现协议,即使您的视图控制器也可以。只需使用initWithActivityItems:@[self]
.
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";
}
根据文档。您传递给的活动项
-initWithActivityItems:applicationActivities:
数组可以是数据对象数组,如字符串或图像,也可以是实现UIActivityItemSource
协议的对象数组。
如果您传递一个实现UIActivityItemSource
协议的对象数组,那么您的实例UIActivityViewController
将在您的活动项目上调用这些方法。这些对象不一定是UIActivityItemProvider
. UIActivityItemProvider
只是一个符合这个协议的类。
借鉴JotWee和Sihad Begovic提供的内容,这里是Swift 5.0版本,让您ViewController
采用该UIActivityItemSource
协议并使用share
barButtonItem 触发与您的 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网站上的这篇网络文章非常有见地。
快乐编码!