gpt4 book ai didi

ios - 当使用 swift3.0 执行按钮操作时,如何在 ios 移动设备中打开文档文件,例如(.pdf、.doc、.docx)?

转载 作者:可可西里 更新时间:2023-11-01 03:04:48 25 4
gpt4 key购买 nike

我需要打开 UIDocumentPickerViewController 并且它应该允许用户选择所有类型的文件。 ie.(.pdf,.doc)files 我使用了 UIDocumentPickerViewController 方法。我的代码:

UIDocumentPickerDelegate, UIDocumentMenuDelegate 在我的类声明中

//上传文件

func OpenFile(){

let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePNG),String(kUTTypeImage)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .fullScreen
self.present(importMenu, animated: true, completion: nil)

}
@available(iOS 8.0, *)
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let cico = url as URL
print("The Url is : \(cico)")

do {
let weatherData = try NSData(contentsOf: cico, options: NSData.ReadingOptions())
print(weatherData)
let activityItems = [weatherData]
let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
if UI_USER_INTERFACE_IDIOM() == .phone {
self.present(activityController, animated: true, completion: { _ in })
}
else {
let popup = UIPopoverController(contentViewController: activityController)
popup.present(from: CGRect(x: CGFloat(self.view.frame.size.width / 2), y: CGFloat(self.view.frame.size.height / 4), width: CGFloat(0), height: CGFloat(0)), in: self.view, permittedArrowDirections: .any, animated: true)
}

} catch {
print(error)
}

//optional, case PDF -> render
//displayPDFweb.loadRequest(NSURLRequest(url: cico) as URLRequest)


}

@available(iOS 8.0, *)
public func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {

documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}


func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {

print(" cancelled by user")

dismiss(animated: true, completion: nil)

}

在此代码中,应用程序将崩溃。原因是 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“除了 initWithDocumentTypes:inMode: 和 initWithURL:inMode: 初始值设定项之外,您无法初始化 UIDocumentPickerViewController。”

我不知道如何初始化 initWithDocumentTypes:inMode。 我是 iOS 新手,谁能帮帮我???你能帮帮我吗..

最佳答案

swift 3*、4*、

要打开文档并选择任何文档,您正在使用 UIDocumentPickerViewController 然后,如果用户设备连接了 Google Drive,则将显示 iCloud、文件和 Google Drive 中显示的所有文档。然后需要将选定的文档下载到您的应用程序中,然后您可以从那里在 WKWebView 中显示它,

   @IBAction func uploadNewResumeAction(_ sender: Any) {

/* let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","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"], in: .import) */

let documentPicker = UIDocumentPickerViewController(documentTypes: ["public.text", "com.apple.iwork.pages.pages", "public.data"], in: .import)

documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}

extension YourViewController: UIDocumentPickerDelegate{


func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {

let cico = url as URL
print(cico)
print(url)

print(url.lastPathComponent)

print(url.pathExtension)

}
}

注意:如果您打算选择所有文件,则必须使用以下代码:

  let documentPicker = UIDocumentPickerViewController(documentTypes: ["com.apple.iwork.pages.pages", "com.apple.iwork.numbers.numbers", "com.apple.iwork.keynote.key","public.image", "com.apple.application", "public.item","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"], in: .import) 

在您的操作方法中。

关于ios - 当使用 swift3.0 执行按钮操作时,如何在 ios 移动设备中打开文档文件,例如(.pdf、.doc、.docx)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47865646/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com