How to make an HTTP request in Swift
from: http://stackoverflow.com/questions/24016142/how-to-make-an-http-request-in-swift
You can use NSURL, NSURLRequest 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的更多相关文章
- WKWebView使用遇到的坑--加载本地html及JS交互
1. ios9以前版本读取本地HTML的问题 当使用loadRequest来读取本地的HTML时,WKWebView是无法读取成功的,后台会出现如下的提示:Could not create a san ...
- swift 基础小结01 --delegate、Optional、GCD的使用、request请求、网络加载图片并保存到沙箱、闭包以及桥接
本文主要记录swift中delegate的使用.“?!”Optional的概念.GCD的使用.request请求.网络加载图片并保存到沙箱.闭包以及桥接. 一.delegate的使用 swift中de ...
- iOS代码规范(OC和Swift)
下面说下iOS的代码规范问题,如果大家觉得还不错,可以直接用到项目中,有不同意见 可以在下面讨论下. 相信很多人工作中最烦的就是代码不规范,命名不规范,曾经见过一个VC里有3个按钮被命名为button ...
- iOS开发系列--Swift进阶
概述 上一篇文章<iOS开发系列--Swift语言>中对Swift的语法特点以及它和C.ObjC等其他语言的用法区别进行了介绍.当然,这只是Swift的入门基础,但是仅仅了解这些对于使用S ...
- Swift -- 对AFN框架的封装
Swift -- 对AFN框架的封装 一.封装AFN的目的 简单的说: 解耦 日常工作中,我们一般都不会去直接使用AFNetWorking来直接发送网络请求,因为耦合性太强,假设有多个控制器都使用AF ...
- 在xcode中用 swift 进行网络服务请求
xcode集成开发环境是运行于Mac苹果电脑上用于开发swift应用程序的工具,利用xcode可以很方便.直观的开发OS X和iOS系统所支持的应用程序. 1 开发环境: Mac OS 10.11 X ...
- 【Swift】Alamofile网络请求数据更新TableView的坑
写这篇BLOG前,有些话不得不提一下,就仅当发发恼骚吧... 今天下午为了一个Alamofire取得数据而更新TableView的问题,查了一下午的百度(360也是见鬼的一样),竟然没有一个简单明了的 ...
- 【swift学习笔记】四.swift使用Alamofire和swiftyJson
Alamofire是AFNetworking的swift版本,功能灰常强大. github:https://github.com/Alamofire/Alamofire SwiftyJSON是操作js ...
- 【iOS】在Swift中使用JSONModel
前言 首先所有的Model还是使用oc来写——看到这一句是不是想关网页了- - #,在swift里面直接写一直报错所以就将就用oc来写了,这里主要是分享一下搭配Alamofire使用的经验. 声明 欢 ...
随机推荐
- docker探索-CentOS7中配置Docker的yum源并升级安装docker1.13(十)
此处使用的是CentOS7,内核版本为 [root@localhost ~]# uname -r -.el7.x86_64 该版本下,配置了yum的源为阿里的镜像源,具体的配置方法可以参见阿里镜像源配 ...
- docker探索-docker私有仓库搭建(九)
前言 本文讲解搭建docker的私有仓库,和Mavan的管理一样,Dockers不仅提供了一个中央仓库,同时也允许我们使用registry搭建本地私有仓库 一.环境 ip 主机名 操作系统 角色 19 ...
- win7+php5.3.10下安装memcache (转)
因为升级到了5.3.10,所以之前的dll不能使用了,弄了好久,终于还是在老外的博客上找到了解决方法: (环境是win7+php5.3.10+win32) 1.解压附件memcached到某个目录,本 ...
- Spark算子---实战应用
Spark算子实战应用 数据集 :http://grouplens.org/datasets/movielens/ MovieLens 1M Datase 相关数据文件 : users.dat --- ...
- 【IntelliJ IDEA】idea上提交代码到GitHub,已经提交了 但是GitHub上却没有的解决办法
摘要: 今天提交idea上的代码到GitHub,提交过程已经完成, 在版本控制的Log中可以看到,已经将这一部分都提交更新了 在版本控制的Local Changes中已经看不到提交过的代码了 ...
- HTML select 选中触发事件
$(function () { $("#cityidchange").change(function (data) { var cityid = $("#cityidch ...
- Building Apache Thrift on CentOS 6.5
Building Apache Thrift on CentOS 6.5 Starting with a minimal installation, the following steps are r ...
- jquery 实现下拉菜单
Jquery 是一个轻量的框架,个人认为非常好用,今天就写一个非常简单的例子,实现下拉菜单功能: 首先肯定要在页面引用jquery.js 版本不限 : 接下来把=================== ...
- Sword protobuf学习四
#include <iostream> #include <string> #include <sys/types.h> /* See NOTES */ #incl ...
- Q-Learning
一.Q-Learning: 例子:https://www.zhihu.com/question/26408259/answer/123230350 http://ml.cs.tsinghua.edu. ...