如何在Swift语言中创建http请求

前端之家收集整理的这篇文章主要介绍了如何在Swift语言中创建http请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
概述:本文通过实例从同步和异步两种方式上回答了”如何在Swift语言中创建http请求“的问题。 @H_403_6@
@H_403_6@

如果你对Objective-C比较了解的话,对于如何创建http请求你一定驾轻就熟了,而新语言Swift与其相比只有语法上的区别。但是,对才接触到这个崭新平台的初学者来说,他们仍然想知道“如何在Swift语言中创建http请求?”。

在这里,我将作出一些建议来回答上述问题。常见的创建http请求的方式主要有两种:同步和异步。在这里,我们将对这两种方式作一一讨论。

在objective C,可以使用NSURL、NSURLRequest和NSURLConnection来创建http请求,而在Swift,它们同样的适用。让我们从同步调用开始吧。

同步调用

? @H_403_6@
1 @H_403_6@
2 @H_403_6@
3 @H_403_6@
4 @H_403_6@
5 @H_403_6@
6 @H_403_6@
7 @H_403_6@
8 @H_403_6@
9 @H_403_6@
10 @H_403_6@
11 @H_403_6@
12 @H_403_6@
13 @H_403_6@
14 @H_403_6@
15 @H_403_6@
16 @H_403_6@
var urlString = "http://api.shephertz.com" // Your Normal URL String @H_403_6@
var url = NSURL.URLWithString(urlString) // Creating URL @H_403_6@
var request = NSURLRequest(URL: url) // Creating Http Request @H_403_6@
var response:AutoreleasingUnsafePointer<NSURLResponse?> = nil; @H_403_6@
var error: AutoreleasingUnsafePointer<NSErrorPointer?> = nil; @H_403_6@
// Sending Synchronous request using NSURLConnection @H_403_6@
var responseData = NSURLConnection.sendSynchronousRequest(request,returningResponse: response,error:nil) as NSData @H_403_6@
if error != nil @H_403_6@
{ @H_403_6@
// You can handle error response here @H_403_6@
} @H_403_6@
else @H_403_6@
{ @H_403_6@
//Converting data to String @H_403_6@
var responseStr:NSString = NSString(data:responseData,encoding:NSUTF8StringEncoding) @H_403_6@
} @H_403_6@ @H_403_6@
@H_403_6@ @H_403_6@

如果响应的是JSON格式,你可以直接把它解析到NSArray / NSDictionary格式:

? @H_403_6@
1 @H_403_6@
var responseDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData,options: NSJSONReadingOptions.MutableContainers,error:nil) as NSDictionary @H_403_6@ @H_403_6@
@H_403_6@ @H_403_6@

异步调用

异步调用可以使用委托模式或者使用objective-c中的completion handler。

使用completion handler:

? @H_403_6@
1 @H_403_6@
2 @H_403_6@
3 @H_403_6@
4 @H_403_6@
5 @H_403_6@
6 @H_403_6@
7 @H_403_6@
8 @H_403_6@
9 @H_403_6@
10 @H_403_6@
11 @H_403_6@
12 @H_403_6@
13 @H_403_6@
14 @H_403_6@
15 @H_403_6@
16 @H_403_6@
17 @H_403_6@
18 @H_403_6@
19 @H_403_6@
20 @H_403_6@
21 @H_403_6@
// Creating Http Request @H_403_6@
@H_403_6@
// Creating NSOperationQueue to which the handler block is dispatched when the request completes or Failed @H_403_6@
var queue: NSOperationQueue = NSOperationQueue() @H_403_6@
@H_403_6@
// Sending Asynchronous request using NSURLConnection @H_403_6@
NSURLConnection.sendAsynchronousRequest(request,queue: queue,completionHandler:{(response:NSURLResponse!,responseData:NSData!,error: NSError!) ->Void in @H_403_6@
@H_403_6@
error != nil @H_403_6@
{ @H_403_6@
println(error.description) @H_403_6@
self.removeActivityIndicator() @H_403_6@
} @H_403_6@
else @H_403_6@
{ @H_403_6@
//Converting data to String @H_403_6@
403_6@
} @H_403_6@
}) @H_403_6@ @H_403_6@
@H_403_6@ @H_403_6@

使用委托模式:

? @H_403_6@
1 @H_403_6@
2 @H_403_6@
3 @H_403_6@
4 @H_403_6@
5 @H_403_6@
// Creating Http Request @H_403_6@
//Making request @H_403_6@
var connection = NSURLConnection(request: request,delegate: self,startImmediately: true ) @H_403_6@ @H_403_6@
@H_403_6@ @H_403_6@

在这里,如果要让connection一开始就立刻加载数据,可把startImmediately的值设定为true,否则就设定为false。如果你使用的是false,则connection将会被设定为不在run loop下运行。你可以在之后通过调用scheduleInRunLoop(_aRunLoop:NSRunLoop!,forModemode:String!)来将connection设定为进入run loop运行和你所选择的模式。

现在你可以创建请求,但是你需要定义在这种情况下的作为响应的委托。

在上面的代码中,我们传递委托来作为self,因此包含的类应当与NSURLConnectionDataDelegate一致,并且类中也应当定义以下委托函数

1 @H_403_6@
2 @H_403_6@
3 @H_403_6@
4 @H_403_6@
func connection(connection: NSURLConnection!,didReceiveResponse response: NSURLResponse!) @H_403_6@
{ //It says the response started coming @H_403_6@
NSLog( "didReceiveResponse" ) @H_403_6@
} @H_403_6@ @H_403_6@ @H_403_6@ @H_403_6@

你还应当定义一个全局的NSMutableData变量:

1 @H_403_6@
var data = NSMutableData() @H_403_6@ @H_403_6@ @H_403_6@ @H_403_6@

这里还有一种可能性是数据可能以块的形式存在。如果数据是以分批的形式接受到的,那么以下的委托函数将会把这些数据收集成块并添加到已定义的全局数据变量。

? @H_403_6@
1 @H_403_6@
2 @H_403_6@
3 @H_403_6@
4 @H_403_6@
5 @H_403_6@
6 @H_403_6@
7 @H_403_6@
8 @H_403_6@
9 @H_403_6@
10 @H_403_6@
11 @H_403_6@
12 @H_403_6@
13 @H_403_6@
403_6@
//This will be called again and again until you get the full response @H_403_6@
"didReceiveData" ) @H_403_6@
// Appending data @H_403_6@
self.data.appendData(_data) @H_403_6@
} @H_403_6@
@H_403_6@
func connectionDidFinishLoading(connection: NSURLConnection!) @H_403_6@
// This will be called when the data loading is finished i.e. there is no data left to be received and now you can process the data. @H_403_6@
"connectionDidFinishLoading" ) @H_403_6@
var responseStr:NSString = NSString(data:self.data,encoding:NSUTF8StringEncoding) @H_403_6@
} @H_403_6@ @H_403_6@
@H_403_6@ @H_403_6@

本文作者将整个代码封装在了一个示例项目里,你可以在my github repo上进行下载。

@H_403_6@ @H_403_6@

猜你在找的Swift相关文章