NSURL(string: fullURL) returns error

青春壹個敷衍的年華 提交于 2019-12-13 07:14:52

问题


Hello I am passing some data to webservice. This is my url

http://www.website.com/mobileapp/user.php?reg_device_id=566&alert_details=[{\n    \"is_active\" = 1;\n    \"term_id\" = 55;\n}, {\n    \"is_active\" = 1;\n    \"term_id\" = 2;\n}, {\n    \"is_active\" = 1;\n    \"term_id\" = 111;\n}, {\n    \"is_active\" = 0;\n    \"term_id\" = 21;\n}, {\n    \"is_active\" = 0;\n    \"term_id\" = 28;\n}, {\n    \"is_active\" = 0;\n    \"term_id\" = 1;\n}, {\n    \"is_active\" = 0;\n    \"term_id\" = 5;\n}, {\n    \"is_active\" = 0;\n    \"term_id\" = 4;\n}]

This is my webservice calling methid

for i in 0..<dm.array.count {
        let id=dm.SettingsItems[i]["term_id"] as! String
        var is_active="0"

        if dm.array[i]
        {
            is_active="1"
        }

        let catRegDict:NSDictionary=["term_id":"\(id)","is_active":"\(is_active)"]
        self.registerAyrray.append(catRegDict)
        print("------REGISTER ARRAY------\(self.registerAyrray)")
    }
    let urlPath = "http://www.website.com/mobileapp/user.php?"
    let path="reg_device_id=\(dm.DeviceID)&alert_details=\(self.registerAyrray)"

    let fullURL = "\(urlPath)\(path)"
    print(fullURL)


    guard let endpoint = NSURL(string: fullURL) else {
        print("Error creating endpoint")
        return
    }

    let request = NSMutableURLRequest(URL:endpoint)
    NSURLSession.sharedSession().dataTaskWithRequest(request,completionHandler:  { (data, response, error) in
        do {

But seems this condition not satisfying. I can see its prenting this error message.

guard let endpoint = NSURL(string: fullURL) else {
    print("Error creating endpoint")
    return
}

But when I put the above url in browser its giving me the service response.What is the reason for this? Please help me. Thanks


回答1:


Try to encode you url and then create NSURL object like this.

let url = fullURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())

Now pass this url in NSURL initialization.

Hope this will help you.




回答2:


It's because your URL is not valid url. You have to encode url it before use.

urlString.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())



回答3:


URLs can't contain spaces, or "[", or various other characters. If you read the Xcode docs on URLWithString, they say:

The URL string with which to initialize the NSURL object. Must be a URL that conforms to RFC 2396. This method parses URLString according to RFCs 1738 and 1808.

Look up those RFCs online for more info.

@Arsen gives you the code to escape the URL correctly, but why are you trying to send a URL that contains runs of spaces, and how are you generating the query string for alert_details? The alert_details string looks a little like pretty-printed JSON. You should not use pretty-printed format for URL parameters.



来源:https://stackoverflow.com/questions/38037806/nsurlstring-fullurl-returns-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!