目前,我们可能无法通过在activityViewController中添加excludedActivityTypes来禁用“保存到文件”或“添加到共享相册”。但是,当用户按下两个活动类型时,我们可以防止执行操作,而是显示警报。
首先,我们创建自定义的UIActivityItemProvider。
class ActivityItemProvider: UIActivityItemProvider {
override func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
if(activityType.rawValue.contains("com.apple.CloudDocsUI.AddToiCloudDrive") || (activityType.rawValue.contains("com.apple.mobileslideshow.StreamShareService") {
activityViewController.dismiss(animated: true, completion: nil)
return nil
}
return self.placeholderItem
}
在我们调用
UIActivityController
的地方,让我们使用
let item = ActivityItemProvider.init(placeholderItem: {your item})
let activityViewController = UIActivityViewController.init(activityItems: item, applicationActivities: nil)
ObjectiveC版本
#import "BRActivityItemProvider.h"
#import "UIViewController+Additions.h"
@implementation BRActivityItemProvider
- (id)activityViewController:(UIActivityViewController *)activityViewController itemForActivityType:(UIActivityType)activityType {
[super activityViewController:activityViewController itemForActivityType:activityType];
NSLog(@"itemForActivityType %@", activityType);
if([activityType containsString:@"com.apple.CloudDocsUI.AddToiCloudDrive"] || [activityType containsString:@"com.apple.mobileslideshow.StreamShareService"]) {
[activityViewController dismissViewControllerAnimated:true completion:^{
UIViewController *rootController = [UIApplication sharedApplication].keyWindow.rootViewController;
if(rootController != nil) {
NSString *str = [activityType containsString:@"com.apple.CloudDocsUI.AddToiCloudDrive"]
? @"Cannot save file to iCloud Driver"
: @"Cannot save file to Shared Album";
dispatch_async(dispatch_get_main_queue(), ^{
[rootController showAlertController:str action:^(UIAlertAction * _Nonnull action) {
}];
});
}
}];
return nil;
}
return self.placeholderItem;
}
在哪里调用
UIActivityViewController
?
BRActivityItemProvider *itemProvider = [[BRActivityItemProvider alloc]initWithPlaceholderItem:item];
UIActivityViewController *activityController = [[UIActivityViewController alloc]initWithActivityItems:itemProvider applicationActivities:nil];
activityController.popoverPresentationController.sourceView = controller.view;
// your logic code
// ....