如果用户按下取消,UIActivityViewController完成处理程序仍然调用操作

在我的UIActivityViewController中,我使用完成处理程序来执行“成功共享”通知。 它的工作原理,但我唯一的问题是,它仍然显示通知,如果用户按取消。

这是我的完成处理程序代码,

[controller setCompletionHandler:^(NSString *activityType, BOOL completed) { CWStatusBarNotification *notification = [CWStatusBarNotification new]; [notification displayNotificationWithMessage:@"✓ Successfully Shared Centre!" forDuration:3.0f]; notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f]; notification.notificationLabelTextColor = [UIColor whiteColor]; }]; 

谢谢您的帮助!

这是completed论点是:

 [controller setCompletionHandler:^(NSString *activityType, BOOL completed) { if (!completed) return; CWStatusBarNotification *notification = [CWStatusBarNotification new]; [notification displayNotificationWithMessage:@"✓ Successfully Shared Centre!" forDuration:3.0f]; notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f]; notification.notificationLabelTextColor = [UIColor whiteColor]; }]; 

注意:iOS8中不推荐使用completionHandler属性,因此不可能知道共享操作的结果。 https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622010-completionhandler

更新:就像adruzh说,在iOS8上有一个新的完成处理程序,苹果忘记在文档中提到:

 [activityController setCompletionWithItemsHandler: ^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError) { }]; 

https://developer.apple.com/documentation/uikit/uiactivityviewcontroller/1622022-completionwithitemshandler

对于Swift来说,这对我们是有效的:

  ... // 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)") } 

对于那里的Swifties,你将如何在Swift中编写代码以及一些共享服务检测:

 activityViewController.completionHandler = {(activityType, completed:Bool) in if !completed { //cancelled return } //shared successfully //below is how you would detect for different sharing services var activity:String = "other" if activityType == UIActivityTypePostToTwitter { activity = "twitter" } if activityType == UIActivityTypeMail { activity = "mail" } //more code here if you like } 

completed参数将是用户取消的NO

 [controller setCompletionHandler:^(NSString *activityType, BOOL completed) { if (completed) { CWStatusBarNotification *notification = [CWStatusBarNotification new]; [notification displayNotificationWithMessage:@"✓ Successfully Shared Centre!" forDuration:3.0f]; notification.notificationLabelBackgroundColor = [UIColor colorWithRed:38.0f/255.0f green:81.0f/255.0f blue:123.0f/255.0f alpha:1.0f]; notification.notificationLabelTextColor = [UIColor whiteColor]; } }]; 

SWIFT 2.0,iOS 8.0> ,你应该使用这样的完成处理程序:

 self.presentViewController(activityVC, animated: true, completion: nil) activityVC.completionWithItemsHandler = {(activityType, completed:Bool, returnedItems:[AnyObject]?, error: NSError?) in //do some action } 

看到我的答案在这里: https : //stackoverflow.com/a/34581940/1109892

Swift 3

  func completionHandler(activityType: UIActivityType?, shared: Bool, items: [Any]?, error: Error?) { if (shared) { print("Cool user shared some stuff") } else { print("Bad user canceled sharing :(") } } activityController.completionWithItemsHandler = completionHandler