实现UIActivityItemSource协议

时间:2012-10-04 09:45:47

标签: ios ios6

我有这些协议方法,

activityViewControllerPlaceholderItem:activityViewController:itemForActivityType:

但他们永远不会被召唤。如何告诉UIActivityViewController给他们打电话?

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

4 个答案:

答案 0 :(得分:26)

您可以在任何地方实现协议,甚至您的viewcontroller都可以。只需使用initWithActivityItems:@[self]实例化activityViewController。

答案 1 :(得分:18)

JotWee的回答帮助了我。

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

在活动项目数组中添加self非常重要,就像这样(如JotWee建议的那样):

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

这是我的最终实施:

ViewController.h

@interface ViewController : UIViewController <UIActivityItemSource>

ViewController.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";
}

答案 2 :(得分:15)

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

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

答案 3 :(得分:-1)

JotWee Sihad Begovic 所提供的内容,现在是 Swift 5.0 版本,使您的ViewController采用UIActivityItemSource协议,并使用share barButtonItem触发与iPhone和/或iPad上的其他应用共享对象:

在您的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协议并实现其方法的方式。有关更多阅读,我发现这个web article from HackingWithSwift网站非常有见地。

编码愉快!