Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLConnection finished with error - code -1002 when trying to encode URL in iOS

Tags:

url

ios

swift

I was trying to encode a URL. The code works fine when I encode files in my bundle. But when I tried to encode the files written to Documents and Cache, the program fails to encode.

Here is my encoder:

private class func EncodeURL(_ url:URL,encoding:UInt) ->String {
    do{
        return try NSString(contentsOf: url, encoding: encoding) as String
    }catch{}

    return ""
}

I'm using the following three:

content = EncodeURL(url, encoding: String.Encoding.utf8.rawValue)
content = EncodeURL(url, encoding: 0x80000632)
content = EncodeURL(url, encoding: 0x80000631)

And none of them work.

Here is the code I use to generate files. I'm putting them in the Documents Folder.

func writeFile(fileName:String,data:NSData)->Bool{

    guard let filePath = createFilePath(fileName: fileName) else{
        return false
    }
    return data.write(toFile:filePath,atomically:true)
}

func createFilePath(fileName:String)->String?{
    let dir = getCachePath() 
    if(!dirExists(dir: dir) && !createDir(dir: dir)){
        return nil
    }
    let filePath = dir + fileName
    if(fileExists(path: filePath)){
        do{
            try getFileManager().removeItem(atPath: filePath)
        }catch{
            return nil
        }
    }
    return filePath
}

func getCachePath()->String{
    var cacheDir = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, .userDomainMask, true).first!
    if(!cacheDir.hasSuffix("/")){
        cacheDir += "/"
    }
    cacheDir += CACHEPATH + "/" //CACHEPATH is just NSHomeDirectory()
    return cacheDir
}

writeFile(fileName: String(timeInterval)+"_"+somestring+".txt", data: data! as NSData)

Above is how I generate the files.

And how I passed URL is:

url = URL(string:getCachePath()+bookname+".txt")

passing this to

EncodeURL(url:URL,encoding:UInt) 

My URL is:

/Users/houki/Library/Developer/CoreSimulator/Devices/67C921C8-18A3-4A3F-81FF-C3AF04E88049/data/Containers/Data/Application/85633861-90E6-4DB8-95B0-86C359C74C6B/Documents//Users/houki/Library/Developer/CoreSimulator/Devices/67C921C8-18A3-4A3F-81FF-C3AF04E88049/data/Containers/Data/Application/85633861-90E6-4DB8-95B0-86C359C74C6B/1511757881.83107_bigbrother.txt

Does this look weird? I'm testing it on a simulator though.

But actually this worked just fine when I tried to read files through the path. The following code is working.

let contentsofPath = try FileManager.default.contentsOfDirectory(atPath: getCachePath())
like image 757
Nancy Z Avatar asked Nov 27 '17 05:11

Nancy Z


2 Answers

You are not creating your URL correctly. You are passing a path to the string argument. You need to use the URL(fileURLWithPath:) initializer.

let url = URL(fileURLWithPath: path)

Only use the URL(string:) initializer if the string you pass is a valid URL beginning with a URL scheme.

like image 185
rmaddy Avatar answered Oct 05 '22 14:10

rmaddy


For anyone still looking for a solution on Objective-C,

If you are trying to access the bundle file, to create NSURL you'll have to use

fileURLWithPath

instead

URLWithString

. Usually, -1002 error suggests, the URI is invalid.

For Objective-C, below can be the code -

NSURL *url = [NSURL fileURLWithPath:resourceURL];

later, you can utilize this URI in your code.

like image 36
Pankaj Gaikar Avatar answered Oct 05 '22 13:10

Pankaj Gaikar