我试图用UIActivityViewControllerCompletionHandler
,但我不完全得到怎样..
我想要当用户结束或驳回该视图控制器 (检测UIActivityViewController
)。
任何人都知道怎么样?
我试图用UIActivityViewControllerCompletionHandler
,但我不完全得到怎样..
我想要当用户结束或驳回该视图控制器 (检测UIActivityViewController
)。
任何人都知道怎么样?
typedef void (^UIActivityViewControllerCompletionHandler)
(NSString *activityType, BOOL completed);
完成处理的第二个参数告诉你用户是否驳回了控制器。 如果他们驳回了控制器, completed
将被设置为NO
。
还有更多的细节完成处理的文档 。
下面是如何显示共享对话框,设置它的完成处理
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
};
}
斯威夫特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)")
}
对于斯威夫特,这个工作对我来说...
// Configure UIActivityViewController let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil) activityViewController.excludedActivityTypes = [UIActivityTypeAirDrop, UIActivityTypeAddToReadingList, UIActivityTypeAssignToContact, UIActivityTypePrint, UIActivityTypeCopyToPasteboard] // Show UIActivityViewController presentViewController(activityViewController, animated: true, completion: nil) // Define completion handler activityViewController.completionWithItemsHandler = doneSharingHandler ... func doneSharingHandler(activityType: String!, completed: Bool, returnedItems: [AnyObject]!, error: NSError!) { // Return if cancelled if (!completed) { return } // If here, log which activity occurred println("Shared video activity: \(activityType)") }