5

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

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

谁能帮助我通过 Swift 禁用 SaveToFile 选项?我正在使用 Xcode 9.3 和 Swift 4。

4

1 回答 1

0

目前,我们可能无法禁用Save to FilesAdd 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
// ....
于 2020-02-17T10:47:07.747 回答