简体   繁体   English

UIActivityViewController 处理取消操作 | 斯威夫特/Xcode

[英]UIActivityViewController handle Cancel action | Swift/Xcode

I can't find complete code in Swift of how to execute an action when the user presses the "x" in a UIActivityViewController (top right), cancelling the share.我在 Swift 中找不到完整的代码,说明当用户按下 UIActivityViewController(右上角)中的“x”并取消共享时如何执行操作。

This is what I currently have:这就是我目前拥有的:

do {
                try pdfData.write(to: temporaryFileURL)
                let vc = UIActivityViewController(activityItems: [temporaryFileURL], applicationActivities: [])
                self.present(vc, animated: true, completion: nil)
            } catch {
                print(error)
            }

I found this Objective-C code that may work.我发现这个 Objective-C 代码可能有效。 Can someone convert it to Swift for me and show me how I could implement it with what I currently have?有人可以帮我把它转换成 Swift 并告诉我如何用我目前拥有的实现它吗?

vc.completionWithItemsHandler = ^(NSString *activityType,
                                                          BOOL completed,
                                                          NSArray *returnedItems,
                                                          NSError *error){
                    // react to the completion
                    if (completed) {
                        
                        // user shared an item
                        NSLog(@"We used activity type%@", activityType);
                        
                    } else {
                        
                        // user cancelled
                        NSLog(@"We didn't want to share anything after all.");
                    }
                    
                    if (error) {
                        NSLog(@"An Error occured: %@, %@", error.localizedDescription, error.localizedFailureReason);
                    }
                };

Thank you!谢谢!

You can do it this way.你可以这样做。

vc.completionWithItemsHandler = { activityType, completed, returnedItems, 
error in
    // Your logic here
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM