Peter Pik
Peter Pik

Reputation: 11193

Running a url using swift

I've created a script/api which is suppose to add a record to my database when running a specific url. However i'm not sure how to run this url. I do not expect anything back just to run this url? how can i do this?

    var identifier = UIDevice.currentDevice().identifierForVendor.UUIDString
    var addViewUrl = "http://url/addview.php?type=ios&identifier=\(identifier)&newsid=\(newsObject?.id)"

Upvotes: 0

Views: 33

Answers (1)

dehlen
dehlen

Reputation: 7389

Based on my comment: You should get a response and check for errors. Also there is always the possibility to call a URL asynchronously to avoid blocking the GUI if the request takes a long time.

This can be made using delegate patterns or with completions handlers like in Objective-C.

Example:

var url = NSURL.URLWithString(addViewUrl)// Creating URL
var request = NSURLRequest(URL: url)// Creating Http Request

var queue: NSOperationQueue = NSOperationQueue()

NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{(response:NSURLResponse!, responseData:NSData!, error: NSError!) -> Void in

        if error != nil
        {
            println(error.description)
        }
        else
        {
            var responseStr:NSString = NSString(data:responseData, encoding:NSUTF8StringEncoding)
            //Everything went fine
         }
    })

Upvotes: 3

Related Questions