UIActivityViewController 处理取消操作 | Swift/Xcode

UIActivityViewController handle Cancel action | Swift/Xcode

我在 Swift 中找不到完整的代码,说明当用户按下 UIActivityViewController(右上角)中的“x”并取消共享时如何执行操作。

这是我目前拥有的:

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

我发现这个 Objective-C 代码可能有效。有人可以帮我把它转换成 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);
                    }
                };

谢谢!

你可以这样做。

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