1

I have a project that has a ViewController that loads a saved NSURL from memory. This NSURL is saved using NSCoding. When I run my app initially, my print log says:

Saved URL File: file:///private/var/mobile/Containers/Data/Application/7939335F-C909-479E-A309-5AC833069A7B/Documents/Inbox/Pizza-2.pdf

Webview started Loading

Webview did finish load

It displays the PDF file just fine. When I run my app again a few minutes later, it won't display the PDF and it says:

Saved URL File: file:///private/var/mobile/Containers/Data/Application/7939335F-C909-479E-A309-5AC833069A7B/Documents/Inbox/Pizza-2.pdf

Webview started Loading Webview fail with error Optional(Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSUnderlyingError=0x14883f210 {Error Domain=kCFErrorDomainCFNetwork Code=-1100 "The requested URL was not found on this server." UserInfo={NSErrorFailingURLStringKey=file:///private/var/mobile/Containers/Data/Application/7939335F-C909-479E-A309-5AC833069A7B/Documents/Inbox/Pizza-2.pdf, NSLocalizedDescription=The requested URL was not found on this server., 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=The requested URL was not found on this server.})

My code for the ViewController is:

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' is an array of PDF Files. The NSURL is saved from an incoming PDF file the user can view from mail. It looks like it might not be saving? But why is it showing the file name if it's not saving? Thank you.

Update:

In my AppDelegate I have this code:

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
}

I've created a class called 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)

}

}

When I view the incoming PDF file from mail, it loads in a separate UIWebView as such:

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)

    }

}

My save button points to an unwind on another View Controller as:

@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")

    }

}
ChallengerGuy
  • 2,385
  • 6
  • 27
  • 43
  • The second url is different from the first one (Pizza vs. Burrito) – l'L'l Dec 17 '15 at 15:12
  • I understand that, they were examples. If I were to click on the 'pizza' one, it would give the same error. Please see edit. – ChallengerGuy Dec 17 '15 at 15:26
  • Have you checked to see if the pdf file is actually at the path, and what happens if you try the one that didn't work a few more minutes later? – l'L'l Dec 17 '15 at 15:51
  • Same error on the one I tried a few minutes prior. It works flawlessly until I reload my all. The. None of them work unless I import a new PDF file from mail. Not sure how to see if PDF file as actually at that path. – ChallengerGuy Dec 17 '15 at 16:27
  • I don't see where you are actually saving it; the place you have logged which says "Saved URL File" is just loading it I think from a temporary path after the request and not saving it to permanent storage. Maybe this isn't what you are trying to do, so maybe some clarification on what you are expecting might help. Where are you defining the incoming file url? – l'L'l Dec 17 '15 at 16:37
  • I updated it with an edit. I think my problem is in the saving. I'm taking the NSURL and saving it as an NSURL in a class using NSCoding as the save. It's not actually saving it as a PDF. My original WebView is loading a saved NSURL and not a PDF in a URL. I'm still new with this programming and may be wrong however. I think my main issue now is trying to saving an incoming PDF from an email as a PDF in my app and then referencing it later in a WebView. – ChallengerGuy Dec 17 '15 at 17:00

0 Answers0