user1960169
user1960169

Reputation: 3653

NSURL(string: fullURL) returns error

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

Upvotes: 0

Views: 150

Answers (3)

Nirav D
Nirav D

Reputation: 72420

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.

Upvotes: 0

Duncan C
Duncan C

Reputation: 131426

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.

Upvotes: 1

Arsen
Arsen

Reputation: 10951

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

urlString.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())

Upvotes: 1

Related Questions