Search code examples
iosswiftswift3swift2apple-developer

NSData gives me an error in version 9.0


Given below is my function written in swift this code works fine in 10.0 version of iPhone but gives an error in 9.0 saying

CFURLCopyResourcePropertyForKey failed because it was passed an URL which has no scheme Failed to load: The file “Recording2017-03-06_11.08.53000.mp3” couldn’t be opened.

partFileURL will be having

/private/var/mobile/Containers/Data/Application/B8F9055D-D816-4E27-BA2A-B13F0EE97709/tmp/Recording2017-03-06_11.08.53000.mp3

This is my below function-:

//function
func putPartUplaod(partFileURL:URL , partFileNumber:Int)
{
        var partfileData:Data?
        var md5hash:Any?
        var sha256hash:Any?
        //  var error: NSError?   
        let uri:URL = NSURL(fileURLWithPath: partFileURL.absoluteString) as URL

        //Get MD5 Digest
        do{
            print(partFileURL)
            partfileData = try NSData(contentsOf: uri, options: NSData.ReadingOptions.dataReadingMapped) as Data    
            print("hello" , partfileData);
            md5hash = partfileData?.md5().base64EncodedString()  
        }catch let error as NSError {
            print("Failed to load: \(error.localizedDescription)")
        }
}

Solution

  • You can use something like NSData *data = [[NSFileManager defaultManager] contentsAtPath:path]; The string that you're passing in is not a valid URL, it's just a file path. For making it URL, you would need to add the scheme which would be file://.