我想在我的iOS应用程序中选择任何类型的文件(.pdf,.docs,.xlsx,.jpeg,.txt,.rtf等).点击上传按钮,我希望我的应用程序打开一个目录并选择文件(DocumentsPicker)
@IBAction pickDocument(sender: UIButton) {
//Open Document Picker
}
Run Code Online (Sandbox Code Playgroud)
这样做的方法是什么Swift?
我正在使用documentPicker获取任何文档的url路径,然后上传到数据库.我选择文件(pdf,txt ..),上传工作但我想限制文件的大小.
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
self.file = url //url
self.path = String(describing: self.file!) // url to string
self.upload = true //set upload to true
self.attachBtn.setImage(UIImage(named: "attachFilled"), for: .normal)//set image
self.attachBtn.tintColor = UIColor.black //set color tint
sendbtn.tintColor = UIColor.white //
do
{
let fileDictionary = try FileManager.default.attributesOfItem(atPath: self.path!)
let fileSize = fileDictionary[FileAttributeKey.size]
print ("\(fileSize)")
}
catch{
print("Error: \(error)")
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误消息,此文件不存在,文档选择器保存文件以及如何获取其属性.
我检查了最新的Dropbox和Excel for iOS.在Dropbox中,我们得到一个编辑按钮.单击它会打开Excel的扩展,您可以在其中编辑文件.
保存后,更改也会反映在Dropbox文件中.
我想添加这样一个按钮.此外,我想在图像中添加这样一个按钮,以便在可用的"照片编辑"应用程序中打开它们.
如何检查文件(图像,xls,doc或任何其他)是否可以打开进行编辑?
代码到目前为止:
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithURL:url inMode:UIDocumentPickerModeExportToService];
documentPicker.delegate = self;
documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:documentPicker animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
还尝试将模式改为UIDocumentPickerModeMoveToService......

根据apple docs移动本地文档.用户选择外部目的地.文件选择器移动文件; 但是,您仍然可以将文档作为外部文档访问,让用户可以编辑文档.
但我尝试了所有四种模式.没有显示excel选项.
UIDocumentPickerModeImport,
UIDocumentPickerModeOpen,
UIDocumentPickerModeExportToService,
UIDocumentPickerModeMoveToService
Run Code Online (Sandbox Code Playgroud) 我想打开UIDocumentPickerViewController,它应该允许用户选择所有类型的文件.我试图在UIDocumentPickerViewController初始化方法中提及所有UTI仍无法找到某些文件的有效UTI,如rar,Visio文件,mpp,mpt
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:[MingleUtils allowedUTIs] inMode:UIDocumentPickerModeImport];
Run Code Online (Sandbox Code Playgroud)
和
+(NSArray*)allowedUTIs{
return @[@"public.data",@"public.content",@"public.audiovisual-content",@"public.movie",@"public.audiovisual-content",@"public.video",@"public.audio",@"public.text",@"public.data",@"public.zip-archive",@"com.pkware.zip-archive",@"public.composite-content",@"public.text"];
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用文件应用程序一次导入/选择多个文件UIDocumentPickerViewController.
尝试设置,allowsMultipleSelection = true但在呈现选择器时仍然没有" 选择 "选项.
代码段:
UIDocumentPickerViewController *dvc = [[UIDocumentPickerViewController alloc]initWithDocumentTypes:arrContents inMode:UIDocumentPickerModeImport];
dvc.delegate = self;
dvc.allowsMultipleSelection = true;
[self presentViewController:dvc animated:true completion:nil];
Run Code Online (Sandbox Code Playgroud)
我正在研究iOS App上的文件共享,我是新手UIDocumentPickerViewController.我不知道为什么我的应用程序崩溃了.
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.image"] inMode:UIDocumentPickerModeImport];
documentPicker.delegate = self;
documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:documentPicker animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
应用程序在突出显示的行上崩溃.
有没有人以前做过这个?我想像下面的附件一样做
问题:
当从iCloud中选择文档时,应用程序会随机崩溃,大多数时候以下代码都会起作用,但在极少数情况下会失败.
我在应用程序中启用了iCloud权利,似乎无法找到间歇性失败的原因.有检查我错过了吗?
它也会偶尔悬挂5秒钟左右(通常在碰撞中)
码:
#pragma mark - iCloud =======================================================================================================
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
BOOL fileUrlAuthozied = [url startAccessingSecurityScopedResource];
NSURL *ubiquityURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
NSLog(@"ubiquityURL - %@",ubiquityURL);
if(fileUrlAuthozied){
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] init];
NSError *error;
[fileCoordinator coordinateReadingItemAtURL:url options:0 error:&error byAccessor:^(NSURL *newURL) {
NSData *data = [NSData dataWithContentsOfURL:newURL];
//Do something with data
selectedDocumentToUpload = [[UploadDocumentObj alloc] initWithiCloudDocument:data];
[self performSegueWithIdentifier:@"goToRename" sender:nil];
}];
[url stopAccessingSecurityScopedResource];
}else{
//Error handling
[Lib showErrorMessageWithTitle:@"Alert" message:@"E-Sign could not retrive the document!\nPlease try again." delegate:self]; …Run Code Online (Sandbox Code Playgroud) 我已经尝试了两天多来写一个文件到iCloud驱动器.我尝试直接编写一个简单的文本文件,然后在本地移动它,使用UIDocumentMenuViewController等.我没有得到任何错误的代码和单步执行调试器,它看起来很成功,但当我检查文件是否存在或至少iCloud目录,那里什么都没有.我尝试了模拟器和我的iPhone,触发了iCloud同步,以及我能想到的其他一切.
我的主要目标是简单地将文本文件写入iCloud驱动器,后来将成为"数字"文件
我已经设置了我的plist文件和我的权利:
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.com.paul.c.$(PRODUCT_NAME:rfc1034identifier)</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>myCloudTest</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
Run Code Online (Sandbox Code Playgroud)
我还提到了捆绑版本,如下所述:将iOS 8文档保存到iCloud Drive
我已经尝试了几十个没有运气的教程.我的最新代码基于此示例:https://medium.com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1
这是我的代码:
@IBAction func ExportFile(sender: AnyObject) {
var error:NSError?
let iCloudDocumentsURL = NSFileManager.defaultManager().URLForUbiquityContainerIdentifier(nil)?.URLByAppendingPathComponent("myCloudTest")
//is iCloud working?
if iCloudDocumentsURL != nil {
//Create the Directory if it doesn't exist
if (!NSFileManager.defaultManager().fileExistsAtPath(iCloudDocumentsURL!.path!, isDirectory: nil)) {
//This gets skipped after initial run saying directory exists, but still don't see it on iCloud
NSFileManager.defaultManager().createDirectoryAtURL(iCloudDocumentsURL!, withIntermediateDirectories: true, attributes: nil, error: nil)
}
} …Run Code Online (Sandbox Code Playgroud) 我想将DocumentPicker的导航栏背景颜色设置为与我的应用程序的其余部分相同.
let filePicker = UIDocumentPickerViewController(documentTypes: ["public.content"], in: .import)
filePicker.delegate = self
filePicker.navigationController?.navigationBar.barTintColor = self.theme.navigationBarColor
self.present(filePicker, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
不起作用.
我试过的其他事情:
使用UINavigationBar.appearance().backgroundColor = self.theme.navigationBarColor- 不起作用,看起来太像一个变通方法而不是一个正确的方法,如果它的工作原理.
编辑:现在我们的应用程序被重新设计为使用主要颜色作为导航栏文本颜色,并具有与DocumentPicker相同的背景颜色.答案仍将受到赞赏.
我UIDocumentPickerViewController用来让用户从iCloud Drive中选择一个文件上传到后端.
大多数时候,它工作正常.但是,有时(特别是当互联网连接不稳定时)documentPicker:didPickDocumentAtURL:会给出一个文件系统上实际不存在的URL,并且任何使用它的尝试都会返回NSError"No such file or directory".
处理这个问题的正确方法是什么?我正在考虑使用NSFileManager fileExistsAtPath:并告诉用户如果不存在则再试一次.但这听起来不是非常用户友好.有没有办法从iCloud Drive获得真正的错误原因,也许告诉iCloud Drive再试一次?
代码的相关部分:
@IBAction func add(sender: UIBarButtonItem) {
let documentMenu = UIDocumentMenuViewController(
documentTypes: [kUTTypeImage as String],
inMode: .Import)
documentMenu.delegate = self
documentMenu.popoverPresentationController?.barButtonItem = sender
presentViewController(documentMenu, animated: true, completion: nil)
}
func documentMenu(documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
documentPicker.popoverPresentationController?.sourceView = self.view
presentViewController(documentPicker, animated: true, completion: nil)
}
func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) {
print("original URL", url)
url.startAccessingSecurityScopedResource()
var error: NSError?
NSFileCoordinator().coordinateReadingItemAtURL(
url, …Run Code Online (Sandbox Code Playgroud) ios ×9
icloud ×5
swift ×4
icloud-drive ×3
objective-c ×3
uidocument ×2
xcode ×2
excel ×1
filesize ×1