25

我正在尝试使用UIActivityViewControllerCompletionHandler ,但我不太明白如何..

我想检测用户何时完成或关闭此视图控制器UIActivityViewController)。

有谁知道怎么做?

4

5 回答 5

70

这是显示共享对话框并设置其完成处理程序的方法

NSArray *imageArray = [NSArray arrayWithObject:shareImage];

UIActivityViewController *sharing = [[UIActivityViewController alloc] initWithActivityItems:imageArray applicationActivities:nil];

[sharing setCompletionHandler:^(NSString *activityType, BOOL completed) {
    NSLog(@"completed dialog - activity: %@ - finished flag: %d", activityType, completed);
}];

[self presentViewController:sharing animated:YES completion:nil];
于 2012-10-02T01:21:09.553 回答
25

completionHandler推荐使用 iOS 8 ,因此您需要提供替代方案completionWithItemsHandler。它还允许处理您设置的扩展修改数据(例如照片扩展)。查看文档

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[text, URL, image] applicationActivities:nil];
    activityViewController.excludedActivityTypes = @[UIActivityTypePrint, UIActivityTypeCopyToPasteboard, UIActivityTypeAssignToContact, UIActivityTypeAirDrop, UIActivityTypeAddToReadingList];

    // check if new API supported
    if ([activityViewController respondsToSelector:@selector(completionWithItemsHandler)]) {
        activityViewController.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) {
            // When completed flag is YES, user performed specific activity
        };
    } else {
        activityViewController.completionHandler = ^(NSString *activityType, BOOL completed) {
            // When completed flag is YES, user performed specific activity
        };
    }
于 2015-02-13T14:03:35.543 回答
19
typedef void (^UIActivityViewControllerCompletionHandler)
    (NSString *activityType, BOOL completed);

完成处理程序的第二个参数告诉您用户是否关闭了控制器。如果他们解雇了控制器,completed将设置为NO

完成处理程序的文档中有更多详细信息。

于 2012-09-23T18:08:08.990 回答
1

斯威夫特 5:

func showShareOf(url: URL, title: String) {
    let shareItems: [Any] = ["\(title)\n", url]
    let activityVC = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    activityVC.completionWithItemsHandler = doneSharingHandler
    self.present(activityVC, animated: true, completion: nil)
}

@objc func doneSharingHandler(activityType: UIActivity.ActivityType?, completed: Bool, _ returnedItems: [Any]?, error: Error?) {
    if (!completed) { return }
    // If here, log which activity occurred
    print("Shared activity: \(activityType)")
}
于 2019-05-16T19:42:48.183 回答
-1

对于 Swift,这个对我有用……

    // 配置 UIActivityViewController
    让 activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop,
        UIActivityTypeAddToReadingList,
        UIActivityTypeAssignToContact,
        UIActivityTypePrint,
        UIActivityTypeCopyToPasteboard]

    // 显示 UIActivityViewController
    presentViewController(activityViewController,动画:真,完成:无)

    // 定义完成处理程序
    activityViewController.completionWithItemsHandler = doneSharingHandler

...

func doneSharingHandler(activityType:String!,完成:Bool,returnItems:[AnyObject]!,错误:NSError!){

    // 如果取消则返回
    如果(!完成){
        返回
    }
    // 如果在这里,记录发生了哪个活动
    println("共享视频活动:\(activityType)")
}
于 2015-09-06T04:27:27.533 回答