使用swift运行URL

时间:2015-04-19 14:58:31

标签: ios swift

我已经创建了一个脚本/ api,假设在运行特定网址时将记录添加到我的数据库中。但是我不知道如何运行这个网址。我不期待任何回报只是为了运行这个网址?我怎么能这样做?

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

1 个答案:

答案 0 :(得分:3)

根据我的评论: 您应该得到回复并检查错误。 此外,如果请求需要很长时间,总是可以调用URL asynchronously以避免阻止GUI。

这可以使用委托模式或与Objective-C中的完成处理程序一起使用。

示例:

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
         }
    })