如何禁用"保存到文件"在iOS 11中?

时间:2018-05-02 16:00:18

标签: swift ios11 uiactivityviewcontroller xcode9.3

我有一个iOS应用程序可以创建PDF并使用默认共享功能使用UIActivityViewController进行共享。我不得不阻止一些共享设施,如Vimeo,Facebook,Twillio等,而且文件也不应存储在设备上。

使用新的iOS 11版本,Apple提供了具有默认共享功能的SaveToFiles选项。我尝试使用excludedActivityTypes,并阻止" com.apple.CloudDocsUI.AddToiCloudDrive"选项,但没有成功。

任何人都可以帮我禁用Swift的SaveToFile选项吗?我正在使用Xcode 9.3和Swift 4.

1 个答案:

答案 0 :(得分:0)

当前,我们可能无法通过在Save to Files中添加Add to Shared Album来禁用excludedActivityTypesactivityViewController。但是我们可以防止当用户确实按下两种活动类型时不执行操作,而是显示警报。

首先,我们创建自定义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
// ....