目前,我们可能无法禁用Save to Files
或Add to Shared Album
添加excludedActivityTypes
. activityViewController
但是我们可以防止当用户按下两个活动类型时不会执行操作,而是显示警报。
首先我们创建自定义UIActivityItemProvider
class ActivityItemProvider: UIActivityItemProvider {
override func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
// in here we'll check activityType = "com.apple.CloudDocsUI.AddToiCloudDrive" (Save to Files),
// activityType = "com.apple.mobileslideshow.StreamShareService" (Shared Album)
if(activityType.rawValue.contains("com.apple.CloudDocsUI.AddToiCloudDrive") || (activityType.rawValue.contains("com.apple.mobileslideshow.StreamShareService") {
// dismiss activityViewController first
activityViewController.dismiss(animated: true, completion: nil)
// show alert controller, we can using UIApplication.shared.keyWindow?.rootViewController to present alert
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(), ^{
// this's my custom show alert controller, you can change your own
[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
// ....