问题描述
我有一个iOS应用程序,该应用程序创建PDF,并使用UIActivityViewController使用默认共享功能进行共享.我不得不阻止一些共享功能,例如Vimeo,Facebook,Twillio等,而且文件也不应存储在设备上.
I have an iOS app which creates a PDF and is shared using the UIActivityViewController using the default share functionality. I had to block a few sharing facilities like Vimeo, Facebook, Twillio, etc. and also the file should not be stored on the device.
在新的iOS 11版本中,Apple提供了具有默认共享功能的SaveToFiles选项.我尝试使用excludeActivityTypes并阻止了"com.apple.CloudDocsUI.AddToiCloudDrive"选项,但没有成功.
With the new iOS 11 version, Apple has provided SaveToFiles option with the default share functionality. I tried using excludedActivityTypes, and with blocking the "com.apple.CloudDocsUI.AddToiCloudDrive" option, but no success.
有人可以帮助我通过Swift禁用SaveToFile选项吗?我正在使用Xcode 9.3和Swift4.
Can anyone help me to disable the SaveToFile option though Swift? I am using Xcode 9.3 and Swift 4.
推荐答案
当前,我们可能无法通过添加来禁用
.但是我们可以防止当用户确实按下两个活动类型时不执行操作,而是显示警报.保存到文件
或添加到共享相册
activityViewController
中的excludeActivityTypes
Currently, we probably cannot disable Save to Files
or Add to Shared Album
by add excludedActivityTypes
in activityViewController
. But we can prevent when user did press two activity types will do not perform action instead we'll showing alert.
首先,我们创建自定义 UIActivityItemProvider
First we create custom 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
的地方,让我们使用
In where we call UIActivityController
, let's using
let item = ActivityItemProvider.init(placeholderItem: {your item})
let activityViewController = UIActivityViewController.init(activityItems: item, applicationActivities: nil)
ObjectiveC版本
ObjectiveC version
#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
// ....
这篇关于如何禁用“保存到文件"在iOS 11中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!