我正在尝试使用UIActivityViewControllerCompletionHandler
,但我不太明白如何。
我想检测用户何时完成或关闭此视图控制器 ( UIActivityViewController
)。
有人知道怎么做吗?
下面介绍如何显示共享对话框并设置其完成处理程序
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];
在 iOS 8 中,completionHandler
已被弃用,因此您需要提供替代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
};
}
typedef void (^UIActivityViewControllerCompletionHandler)
(NSString *activityType, BOOL completed);
完成处理程序的第二个参数告诉您用户是否关闭了控制器。如果他们关闭控制器,completed
将设置为 NO
。
完成处理程序的文档中提供了更多详细信息。
Swift 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)")
}
completionWithItemsHandler
这是Objective-C中的代码。请相应地将其翻译成 Swift。
UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:@[text] applicationActivities:nil];
activityVC.completionWithItemsHandler = ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *error){
if (completed) {
NSLog(@"Shared by activity %@", activityType);
} else {
NSLog(@"ShareSheet was closed");
}
if (error) {
NSLog(@"An Error Occured: %@, %@", error.localizedDescription, error.localizedFailureReason);
}
};
对于 Swift 来说,这个对我有用......
配置 UIActivityViewController let activityViewController = UIAactivityViewController(activityItems: objectsToShare, applicationActivities: nil) activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard] 显示 UIActivityViewController presentViewController(activityViewController, animated: true, complete: nil) 定义完成处理程序 activityViewController.completionWithItemsHandler = doneSharingHandler...func doneSharingHandler(activityType: String!, complete: Bool, returnItems: [AnyObject]!, error: NSError!){ 取消退货 如果 (!已完成) { 返回 } 如果在此处,请记录发生的活动 println("共享视频活动: \(活动类型)")}