UIDocumentInteractionController打开菜单取消回调

我目前正在开发一个专门针对iOS7的应用程序,它利用菜单中打开的UIDocumentInteractionController,需要一个方法,当用户取消并且不选择可用选项时通知我。

UIDocumentInteractionControllerDelegate提供:

- (void)documentInteractionControllerDidDismissOptionsMenu:(UIDocumentInteractionController *) controller 

但这并未指定用户是否点按了其中一个可用选项或取消。

有任何想法吗?

注意:这不再适用于iOS 8,仅适用于iOS7及更早版本

要确定用户是否已取消菜单或选择了选项,您必须使用以下委托方法:

1-

 - (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application { //get called only when the user selected an option and then the delegate method bellow get called // Set flag here _isOptionSelected = YES; _isOptionSelected = YES; } 

2-

 - (void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller { //called whether the user has selected option or not // check your flag here if(_isOptionSelected == NO) { //the user has canceled the menu } _isOptionSelected = NO; } 

iOS 8

对于iOS 8及更高版本,请使用此方法而不是步骤2中的方法:

 - (void)documentInteractionController:(UIDocumentInteractionController *)controller didEndSendingToApplication:(NSString *)application 

这适用于iOS7和iOS8

 BOOL didSelectOptionFromDocumentController = NO;//**set this to "NO" every time you present your documentInteractionController too -(void)documentInteractionController:(UIDocumentInteractionController *)controller willBeginSendingToApplication:(NSString *)application { didSelectOptionFromDocumentController = YES; } -(void)documentInteractionControllerDidDismissOpenInMenu:(UIDocumentInteractionController *)controller { if (didSelectOptionFromDocumentController == NO) {//user cancelled. } } 

这适用于iOS8和iOS9,适用于第三方应用和系统应用!

它不漂亮,但它的工作原理。

谁能告诉我这是否会通过App Review? 不确定,因为我指的是不可公开访问的类名(_UIDocumentActivityViewController)。 这是Swift 2.2!

NSObject Extension获取类名的字符串:

 extension NSObject { var theClassName: String { return NSStringFromClass(self.dynamicType) } } 

您从以下位置调用UIDocumentInteractionController的Viewcontroller:

 var appOpened = false var presentedVCMonitoringTimer: NSTimer! var docController: UIDocumentInteractionController! func openDocController() { docController = UIDocumentInteractionController(URL: yourURL!) docController.UTI = "your.UTI" docController.delegate = self docController.presentOptionsMenuFromRect(CGRectZero, inView: self.view, animated: true) // Check the class of the presentedViewController every 2 seconds presentedVCMonitoringTimer = NSTimer.scheduledTimerWithTimeInterval(2, target: self, selector: #selector(self.checkPresentedVC), userInfo: nil, repeats: true) } func checkPresentedVC() { if let navVC = UIApplication.sharedApplication().keyWindow?.rootViewController as? UINavigationController { print(navVC.presentedViewController?.theClassName) if navVC.presentedViewController != nil && (navVC.presentedViewController?.theClassName)! != "_UIDocumentActivityViewController" && (navVC.presentedViewController?.theClassName)! != self.theClassName { // A system App was chosen from the 'Open In' dialog // The presented ViewController is not the DocumentInteractionController (anymore) and it's not this viewcontroller anymore (could be for example the MFMailComposeViewController if the user chose the mail app) appOpened = true presentedVCMonitoringTimer?.invalidate() presentedVCMonitoringTimer = nil } } } func documentInteractionControllerDidDismissOptionsMenu(controller: UIDocumentInteractionController) { print("dismissedOptionsMenu") presentedVCMonitoringTimer?.invalidate() presentedVCMonitoringTimer = nil if appOpened { // Do your thing. The cancel button was not pressed appOpened = false } else { // Do your thing. The cancel button was pressed } } func documentInteractionController(controller: UIDocumentInteractionController, willBeginSendingToApplication application: String?) { // A third party app was chosen from the 'Open In' menu. appOpened = true presentedVCMonitoringTimer?.invalidate() presentedVCMonitoringTimer = nil } 

对于Swift 4,请使用:

  func documentInteractionControllerDidDismissOpenInMenu(_ controller: UIDocumentInteractionController) { // this function get called when users finish their work, // either for sharing thing within the same app or exit to other app will do } 

用户在Facebook和Instagram上共享图像后使用它。