If you know Objective-C, you must be familiar with the way of making http requests. The process is quite similar in Swift, however, having just a syntax change. Anyone who is new to this platform and is trying to start with Swift might be wondering ‘how to make http request in swift?’.もしあなたがObjective-Cを理解しているならば、HTTPリクエストとSwiftの作成方法をすぐに理解できるに違いないでしょう。比較するとシンタックスの変更のみで作成可能です。しかしこのプラットフォームを使うのが初めてで、Swiftを始めようとしている方からすれば、SwiftにおけるHTTPリクエストの作成方法とはなんぞやとなることでしょう。
Here, I will propose a few tips to answer the above question. Normally, there are two ways of making http calls, Synchronous and Asynchronous and we will discuss both, one-by-one.
In Objective-C, the http calls can be made using NSURL, NSURLRequest and NSURLConnection and same can be used in this case as well. So let’s start with Synchronous calls.
Synchronous Calls(Updated for Swift 2.0):
let urlString = "http://api.shephertz.com" // Your Normal URL String
let url = NSURL(string: urlString)// Creating URL
let request = NSURLRequest(URL: url!) // Creating Http Request
let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil
// Sending Synchronous request using NSURLConnection
do {
let responseData = try NSURLConnection.sendSynchronousRequest(request, returningResponse: response) //Converting data to String
let responseStr:NSString = NSString(data:responseData, encoding:NSUTF8StringEncoding)!
} catch (let e) {
print(e)
// You can handle error response here
}
If the response is in JSON format, you can parse it directly to NSArray / NSDictionary format as:
var responseDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData,options: NSJSONReadingOptions.MutableContainers, error:nil) as NSDictionary
Asynchronous Calls:
Asynchronous calls can be made using delegate pattern or using completion handler as in Objective-C.
Using Completion Handler:
let urlString = "http://api.shephertz.com" // Your Normal URL String
let url = NSURL(string: urlString)// Creating URL
let request = NSURLRequest(URL:url!)// Creating Http Request
// Creating NSOperationQueue to which the handler block is dispatched when the request completes or failed
let queue: NSOperationQueue = NSOperationQueue()
// Sending Asynchronous request using NSURLConnection
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{(response:NSURLResponse?, responseData:NSData?, error: NSError?) ->Void in
if error != nil
{
print(error!.description)
}
else
{
let responseStr:NSString = NSString(data:responseData!, encoding:NSUTF8StringEncoding)!
}
})
Using Delegate Pattern:
let urlString = "http://api.shephertz.com" // Your Normal URL String
let url = NSURL(string: urlString)// Creating URL
let request = NSURLRequest(URL: url!)// Creating Http Request
//Making request
NSURLConnection(request: request, delegate: self, startImmediately: true)
Here, keep startImmediately as true if, the connection has to begin with loading the data immediately, otherwise state it as false. If you pass false, the connection will not be scheduled with a run loop. You can then schedule the connection in the run loop and mode of your choice by calling scheduleInRunLoop(_ aRunLoop: NSRunLoop!,forMode mode: String!).
Now, you have made the request but you need to define delegates as well to get the response in this case.
In the above code, we passed delegate as self, so the containing class should confirm to the NSURLConnectionDataDelegate, and class should also define the following delegate functions:
func connection(connection: NSURLConnection, didReceiveResponse response: NSURLResponse)
{
//Will be called when
NSLog("didReceiveResponse")
}
You should always define a global NSMutableData variable as:
var data = NSMutableData()
Well, there is a possibility that the data may come in chunks. If the data is being received in parts, then the chunks of data can be collected in the following delegate function and appended to the globally defined data variable.
func connection(connection: NSURLConnection, didReceiveData _data: NSData)
{
NSLog("didReceiveData")
// Appending data
self.data.appendData(_data)
}
func connectionDidFinishLoading(connection: NSURLConnection)
{
NSLog("connectionDidFinishLoading")
let responseStr:NSString = NSString(data:self.data, encoding:NSUTF8StringEncoding)!
}
I have encapsulated the entire code in a sample project that can be downloaded from my github repo.
If you have any queries or need further assistance, please feel free to reach us at support@shephertz.com.
ここで私が上記の質問に回答するためにいくつかの説明を提示いたしましょう。一般的にHTTPコールの作成方法には2種類、同期式と非同期式がありますので1つずつ解説していきましょう。
Objective-Cでは、HTTPコールがNSURLやNSURL Request, NSURL Connectionを使用して行われています。そして同じものが今回のケースでも使用されます。それでは同期式コールの説明を始めましょう。
同期式コール
var urlString = "http://api.shephertz.com" // Your Normal URL String
var url = NSURL.URLWithString(urlString)// Creating URL
var request = NSURLRequest(URL: url) // Creating Http Request
var response:AutoreleasingUnsafePointer = nil;
var error: AutoreleasingUnsafePointer = nil;
// Sending Synchronous request using NSURLConnection
var responseData = NSURLConnection.sendSynchronousRequest(request,returningResponse: response, error:nil) as NSData
if error != nil
{
// You can handle error response here
}
else
{
//Converting data to String
var responseStr:NSString = NSString(data:responseData, encoding:NSUTF8StringEncoding)
}
もしレスポンスがJSONのフォーマットならば、あなたはNS Array / NS Dictionaryフォーマットで直接それを分析することができます。
var responseDict: NSDictionary = NSJSONSerialization.JSONObjectWithData(responseData,options: NSJSONReadingOptions.MutableContainers, error:nil) as NSDictionary
非同期式コール
非同期式コールはデリゲートパターンやコンプリーションハンドラーを使用して、Objective-Cで作られています。
コンプリーションハンドラーを使用
var urlString = "http://api.shephertz.com" // Your Normal URL String
var url = NSURL.URLWithString(urlString)// Creating URL
var request = NSURLRequest(URL: url)// Creating Http Request
// Creating NSOperationQueue to which the handler block is dispatched when the request completes or failed
var queue: NSOperationQueue = NSOperationQueue()
// Sending Asynchronous request using NSURLConnection
NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{(response:NSURLResponse!, responseData:NSData!, error: NSError!) ->Void in
if error != nil
{
println(error.description)
self.removeActivityIndicator()
}
else
{
//Converting data to String
var responseStr:NSString = NSString(data:responseData, encoding:NSUTF8StringEncoding)
}
})
デリゲートパターンを使用
var urlString = "http://api.shephertz.com" // Your Normal URL String
var url = NSURL.URLWithString(urlString)// Creating URL
var request = NSURLRequest(URL: url)// Creating Http Request
//Making request
var connection = NSURLConnection(request: request, delegate: self, startImmediately: true)
ここで、もしコネクションがデータをすぐにロードする必要があるならば、startImmediatelyをtrue としてキープしましょう。さもないとfalseが表示されてしまいます。もしfalseになってしまえば、Run Loopでスケジュールされないでしょう。あなたはschedule In Run Loop(_ a Run Loop: NS Run Loop!,for Mode mode: String!)にコールすることで、Run Loopでのコネクションや選択モードをスケジュールできます。
あなたはリクエスト作ってきましたが、このケースではレスポンスを獲得するためにデリゲートを定義する必要があります。
上記のコードで私達はselfとしてデリゲートをパスしましたので、含まれるクラスはNS RLConnectionDataDelegateに確認するべきですし、クラスは次のデリゲートファンクションを定義するべきです。
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!)
{ //It says the response started coming
NSLog("didReceiveResponse")
}
あなたは常にグローバルNS Mutable Data変数を定義する必要があります。
var data = NSMutableData()
データがチャンクの中に入る可能性があります。もしデータがパーツに受け取られるならば、そのデータのチャンクは次のデリゲートファンクションに収集され、グローバルに定義されたデータ変数に追加されうるでしょう。
func connection(connection: NSURLConnection!, didReceiveData _data: NSData!)
{ //This will be called again and again until you get the full response
NSLog("didReceiveData")
// Appending data
self.data.appendData(_data)
}
func connectionDidFinishLoading(connection: NSURLConnection!)
{
// 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.
NSLog("connectionDidFinishLoading")
var responseStr:NSString = NSString(data:self.data, encoding:NSUTF8StringEncoding)
}
私はGithub Repoからダウンロードできるサンプルプロジェクトで全体のコードをカプセル化してきました。
もし質問がございましたら、ご自由にsupport@shephertz.com.へご連絡ください。
Leave A Reply