我有一个项目,它有一个从内存ViewController
中加载的项目。NSURL
这NSURL
是使用保存的NSCoding
。当我最初运行我的应用程序时,我的print
日志显示:
保存的 URL 文件:file:///private/var/mobile/Containers/Data/Application/7939335F-C909-479E-A309-5AC833069A7B/Documents/Inbox/Pizza-2.pdf
Webview 开始加载
Webview 确实完成了加载
它可以很好地显示 PDF 文件。几分钟后,当我再次运行我的应用程序时,它不会显示 PDF 并显示:
保存的 URL 文件:file:///private/var/mobile/Containers/Data/Application/7939335F-C909-479E-A309-5AC833069A7B/Documents/Inbox/Pizza-2.pdf
Webview 开始加载 Webview 失败并出现错误 Optional(Error Domain=NSURLErrorDomain Code=-1100 "在此服务器上找不到请求的 URL。" UserInfo={NSUnderlyingError=0x14883f210 {Error Domain=kCFErrorDomainCFNetwork Code=-1100 "请求的 URL 不是在此服务器上找到。” UserInfo={NSErrorFailingURLStringKey=file:///private/var/mobile/Containers/Data/Application/7939335F-C909-479E-A309-5AC833069A7B/Documents/Inbox/Pizza-2.pdf, NSLocalizedDescription=在此服务器上找不到请求的 URL。NSErrorFailingURLKey=file:///private/var/mobile/Containers/Data/Application/7939335F-C909-479E-A309-5AC833069A7B/Documents/Inbox/Pizza-2.pdf} }, NSErrorFailingURLStringKey=file:///private/var/mobile/Containers/Data/Application/7939335F-C909-479E-A309-5AC833069A7B/Documents/Inbox/Pizza-2.pdf,NSErrorFailingURLKey=file:///private/var/mobile/Containers/Data/Application/7939335F-C909-479E-A309-5AC833069A7B/Documents/Inbox/Pizza-2.pdf, NSLocalizedDescription=在此服务器上找不到请求的 URL .})
我的代码ViewController
是:
class PDFItemViewController: UIViewController, UIWebViewDelegate {
// MARK: Properties
var file: PDFFile?
var incomingURL: NSURL!
@IBOutlet weak var background: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//Set theme pic to back always
view.sendSubviewToBack(background)
let myWebView:UIWebView = UIWebView(frame: CGRectMake(0, 44, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height))
self.view.addSubview(myWebView)
myWebView.delegate = self
if let file = file {
incomingURL = file.url
print("Saved URL File: \(incomingURL)")
let request = NSURLRequest(URL: incomingURL!)
myWebView.loadRequest(request)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: UIWebView Delegate
func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
print("Webview fail with error \(error)");
}
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return true
}
func webViewDidStartLoad(webView: UIWebView) {
print("Webview started Loading")
}
func webViewDidFinishLoad(webView: UIWebView) {
print("Webview did finish load")
}
}
'PDFFile' 是一个 PDF 文件数组。NSURL
保存在用户可以从邮件中查看的传入 PDF 文件中。好像没救了?但是,如果不保存,为什么会显示文件名?谢谢你。
更新:
在我的AppDelegate
我有这个代码:
func application(app: UIApplication, openURL url: NSURL, options: [String : AnyObject]) -> Bool {
// Transfer incoming file to global variable to be read
if url != "" {
// Load from Mail App
incomingFileTransfer = url
incomingStatus = "Incoming"
}
return true
}
我创建了一个class
名为 PDFFile.swift 的文件:
// Class for the saved PDF File, in this case a NSURL
class PDFFile: NSObject, NSCoding {
// MARK: Properties
var name: String
var url: NSURL
// MARK: Archiving Path
static let DocumentsDirectory = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("files")
// MARK: Types
struct PropertyKey {
static let nameKey = "name"
static let urlKey = "url"
}
// MARK: Initialization
init?(name: String, url: NSURL) {
self.name = name
self.url = url
super.init()
if name.isEmpty {
return nil
}
}
// MARK: NSCoding
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(name, forKey: PropertyKey.nameKey)
aCoder.encodeObject(url, forKey: PropertyKey.urlKey)
}
required convenience init?(coder aDecoder: NSCoder) {
let name = aDecoder.decodeObjectForKey(PropertyKey.nameKey) as! String
let url = aDecoder.decodeObjectForKey(PropertyKey.urlKey) as! NSURL
self.init(name: name, url: url)
}
}
当我从邮件中查看传入的 PDF 文件时,它会单独加载UIWebView
:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Incoming Emailed PDF from the 'Open-in' feature
if incomingFileTransfer != nil {
// Show incoming file
let request = NSURLRequest(URL: incomingFileTransfer!)
incomingView.loadRequest(request)
}
}
我的保存按钮指向unwind
另一个View Controller
:
@IBAction func unwindToMainMenu(sender: UIStoryboardSegue) {
if let sourceViewController = sender.sourceViewController as? IncomingFileViewController, file = sourceViewController.file {
if let selectedIndexPath = fileTableNotVisible.indexPathForSelectedRow {
// Update an existing recipe.
pdfFiles[selectedIndexPath.row] = file
fileTableNotVisible.reloadRowsAtIndexPaths([selectedIndexPath], withRowAnimation: .None)
}
else {
// Add a new file
let newIndexPath = NSIndexPath(forRow: pdfFiles.count, inSection: 0)
// Add to pdf file array
pdfFiles.append(file)
// Adds new file to bottom of table
fileTableNotVisible.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Bottom)
}
saveFiles()
}
}
// MARK: NSCoding
func saveFiles() {
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(pdfFiles, toFile: PDFFile.ArchiveURL.path!)
if !isSuccessfulSave {
print("Failed to save PDF file")
}
}