Swift

Swift NSURLSession

1
import Foundation
2
3
let headers = [
4
"content-type": "application/json"
5
]
6
let parameters = [
7
"token": "c2a0b6221c5dd55ceb09ae1f74e46521756d",
8
"priority ": 0,
9
"application": "2",
10
"sleep": 0,
11
"globalmessage": "",
12
"globalmedia": "",
13
"data": [
14
[
15
"number": "1212",
16
"message": "Test"
17
]
18
]
19
] as [String : Any]
20
21
let postData = JSONSerialization.data(withJSONObject: parameters, options: [])
22
23
let request = NSMutableURLRequest(url: NSURL(string: "https://pickyassist.com/app/api/v2/push")! as URL,
24
cachePolicy: .useProtocolCachePolicy,
25
timeoutInterval: 10.0)
26
request.httpMethod = "POST"
27
request.allHTTPHeaderFields = headers
28
request.httpBody = postData as Data
29
30
let session = URLSession.shared
31
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
32
if (error != nil) {
33
print(error)
34
} else {
35
let httpResponse = response as? HTTPURLResponse
36
print(httpResponse)
37
}
38
})
39
40
dataTask.resume()
Copied!
Last modified 2yr ago
Copy link