0

I have this code:

func openPDFFromInternet(www: String){
        let serwerUrl = ApiConstans.fullPath + "" + www
        let url = NSURL(string: www)
        print("the url = \(url)")
        UIApplication.shared.open(URL(string : www)!)
    }

www has value: "

/Users/imac/Library/Developer/CoreSimulator/Devices/A2B19B93-A0AD-46DF-923F-E18DD76EAC96/data/Containers/Data/Application/A37F7B0E-3B67-42E8-B616-5C3066F5653F/Documents/eng/PDF/MyFile RostiBites2015-ENG new_OK-prev.pdf" 

Print return me nil.

When I run this code I have error:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Does anyone know repair it? I have file in this path. I can't rename my filename

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
triff
  • 157
  • 3
  • 12
  • Possible duplicate of [What does "fatal error: unexpectedly found nil while unwrapping an Optional value" mean?](https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu) – Sharad Chauhan Jun 12 '18 at 08:58
  • The issue here is due to the fact that the filename has spaces included.. Removing the spaces in the filename will return a value for url – Skywalker Jun 12 '18 at 09:02
  • Its also true that there are invalid escape sequences used specifically '\g' just before '/PDF' – Skywalker Jun 12 '18 at 09:04
  • On a different note, are you not getting warnings that you are not using the constant serwerUrl? Also use URL instead of NSURL in second line – Skywalker Jun 12 '18 at 09:06
  • if you must have the space in the file name you can url encode the filename string and then use that – Skywalker Jun 12 '18 at 09:07
  • Then you must url encode the filename and use that string – Skywalker Jun 12 '18 at 09:13
  • edit the question.. dont post it in comments – Skywalker Jun 12 '18 at 09:14
  • @triff don't add irrelevant tags, especially after they've been removed through edit for a reason. Swift version tags should only be used when asking about a change/issue specific to a Swift version, however your question is not related to any changes between Swift3 and 4, so you shouldn't add those tags. – Dávid Pásztor Jun 12 '18 at 09:17
  • Ok, sorry. Thank you for you info – triff Jun 12 '18 at 09:28

2 Answers2

1

The issue is caused by the URL having invalid characters, namely spaces in it. You need to encode the URL to remove the invalid characters.

let localUrlString = "/Users/imac/Library/Developer/CoreSimulator/Devices/A2B19B93-A0AD-46DF-923F-E18DD76EAC96/data/Containers/Data/Application/A37F7B0E-3B67-42E8-B616-5C3066F5653F/Documents/enb\\g/PDF/MyFile RostiBites2015-ENG new_OK-prev.pdf"
if let localEncodedUrlString = localUrlString.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) {
    let localUrl = URL(fileURLWithPath: localEncodedUrlString)
}

Your function name is quite misleading, since the value of www in your question is clearly a local file path, so either change the function name or if it really is supposed to open files from remote URLs, change URL(fileURLWithPath: localEncodedUrlString) to URL(string: localEncodedUrlString and use optional binding to safely unwrap the failable initializer.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
1

The URL is clearly a file system URL – indicated by a leading slash – so the API URL(string: is wrong. You have to use URL(fileURLWithPath which percent escapes space characters implicitly.

func openPDFFromInternet(www: String){
    let serwerUrl = ApiConstans.fullPath + "" + www
    let url = URL(fileURLWithPath : www)
    print("the url = \(url)")
    UIApplication.shared.open(url)
}

Or if the URL can be local or remote a more robust way

func openPDFFromInternet(www: String){
    let serwerUrl = ApiConstans.fullPath + "" + www
    if www.hasPrefix("/") {
        let url = URL(fileURLWithPath : www)
        UIApplication.shared.open(url)
    } else {
        guard let escapedString = www.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed),
        let url = URL(string : escapedString) else { return }
        UIApplication.shared.open(url)
    }
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • this code starts correctly, but does not open the pdf file in the default system program – triff Jun 12 '18 at 09:42
  • Actually that's another question Is the file URL valid? – vadian Jun 12 '18 at 09:47
  • Please check if the URL is valid at **runtime**. – vadian Jun 12 '18 at 09:55
  • yes, I have this path for example: file:///Users/imac/Library/Developer/CoreSimulator/Devices/A2B19B93-A0AD-46DF-923F-E18DD76EAC96/data/Containers/Data/Application/E25C2467-1047-4C49-B8C4-75373D99ED1F/Documents/en/PDF/Folder%20A4-R2015-ENG%20new_OK-prev.pdf - when I copy this and paste to Firefox - it opens correctly without any errors – triff Jun 12 '18 at 10:00