from: http://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift

You can use NSURLNSURLRequest and NSURLSession or NSURLConnection as you'd normally do in Objective-C. Note that for iOS 7.0 and later, NSURLSession is preferred.

Using NSURLSession

Initialize an NSURL object and an NSURLSessionDataTask from NSURLSession. Then run the task with resume().

var url = NSURL(string: "http://www.stackoverflow.com")

let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
} task.resume()

Using NSURLConnection

First, initialize an NSURL and an NSURLRequest:

var url = NSURL(string: "http://www.stackoverflow.com")
var request = NSURLRequest(URL: url)

Then, you can load the request asynchronously with:

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {(response, data, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}

Or you can initialize an NSURLConnection:

var connection = NSURLConnection(request: request, delegate:nil, startImmediately: true)

Just make sure to set your delegate to something other than nil and check the delegate methods to work with the response and data received.

For more detail, check the documentation for the NSURLConnectionDataDelegate protocol

Testing on an Xcode playground

If you want to try this code on a Xcode playground, add import XCPlayground to your playground, as well as the following call:

XCPSetExecutionShouldContinueIndefinitely()

This will allow you to use asynchronous code in playgrounds.

How to make an HTTP request in Swift的更多相关文章

  1. WKWebView使用遇到的坑--加载本地html及JS交互

    1. ios9以前版本读取本地HTML的问题 当使用loadRequest来读取本地的HTML时,WKWebView是无法读取成功的,后台会出现如下的提示:Could not create a san ...

  2. swift 基础小结01 --delegate、Optional、GCD的使用、request请求、网络加载图片并保存到沙箱、闭包以及桥接

    本文主要记录swift中delegate的使用.“?!”Optional的概念.GCD的使用.request请求.网络加载图片并保存到沙箱.闭包以及桥接. 一.delegate的使用 swift中de ...

  3. iOS代码规范(OC和Swift)

    下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...

  4. iOS开发系列--Swift进阶

    概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...

  5. Swift -- 对AFN框架的封装

    Swift -- 对AFN框架的封装 一.封装AFN的目的 简单的说: 解耦 日常工作中,我们一般都不会去直接使用AFNetWorking来直接发送网络请求,因为耦合性太强,假设有多个控制器都使用AF ...

  6. 在xcode中用 swift 进行网络服务请求

    xcode集成开发环境是运行于Mac苹果电脑上用于开发swift应用程序的工具,利用xcode可以很方便.直观的开发OS X和iOS系统所支持的应用程序. 1 开发环境: Mac OS 10.11 X ...

  7. 【Swift】Alamofile网络请求数据更新TableView的坑

    写这篇BLOG前,有些话不得不提一下,就仅当发发恼骚吧... 今天下午为了一个Alamofire取得数据而更新TableView的问题,查了一下午的百度(360也是见鬼的一样),竟然没有一个简单明了的 ...

  8. 【swift学习笔记】四.swift使用Alamofire和swiftyJson

    Alamofire是AFNetworking的swift版本,功能灰常强大. github:https://github.com/Alamofire/Alamofire SwiftyJSON是操作js ...

  9. 【iOS】在Swift中使用JSONModel

    前言 首先所有的Model还是使用oc来写——看到这一句是不是想关网页了- - #,在swift里面直接写一直报错所以就将就用oc来写了,这里主要是分享一下搭配Alamofire使用的经验. 声明 欢 ...

随机推荐

  1. css按钮固定

  2. 基于css3 transform实现散乱的照片排列

    分享一款基于css3 transform实现散乱的照片排列.这是一款简单零散的css3相册排列特效下载.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div class= ...

  3. Linux使用redis

    在linux遇到这种情况. 注意,这里本redis 用的端口是6389 通过 ps -aux 看到redis 启动了: root ? Ssl Jun14 : redis-server *: root ...

  4. [转] [Java] 知乎下巴第5集:使用HttpClient工具包和宽度爬虫

    原文地址:http://blog.csdn.net/pleasecallmewhy/article/details/18010015 下载地址:https://code.csdn.net/wxg694 ...

  5. Python MQTT 最简单例程搭建

    MQTT 不是普通的 client server 模型,他还加了一个 代理者. 根据剑锋的提示,先下载了 paho-mqtt 模块, ubuntu 14.04 上下载方法如下: sudo apt-ge ...

  6. 【oneday_onepage】—— 日常用语

    what do you do for living? 一般用在问对方的工作.如果直接说“what is your job?”会显得有点生硬了. i was wondering if you can t ...

  7. 安装redis出现cc adlist.o /bin/sh:1:cc:not found

    安装redis时 提示执行make命令时, 提示 CC adlist.o /bin/sh: cc: 未找到命令 问题原因:这是由于系统没有安装gcc环境,因此在进行编译时才会出现上面提示,当安装好gc ...

  8. LibreOffice openoffice 区别

    LibreOffice的初始版本号码被设置为与OpenOffice.org一致,故初始发布(2010年)即为第三版,并不存在第二版.第一版. 后来,甲骨文宣布停止OpenOffice.org的商业支持 ...

  9. Sword STL之map效率问题

    #include <iostream> #include <map> #include <vector> using namespace std; /* STL容器 ...

  10. [Qt] 利用QtWebKit完成JavaScript访问C++对象

    一. 介绍 在浏览器扩展或者WebApp的项目经常用的脚本语言javascript有很多局限性,比如,javascript语言不能够夸窗口访问js对象,不能直接读写磁盘文件(这个也正是发明人设计的安全 ...