2019. 4. 25. 16:51ㆍiOS
let strUrl : String = "https://jsonplaceholder.typicode.com"
func requestHttp(_ txtResponse: UITextView) {
let api = strUrl+"/users/10"
let encoding = api.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
let url = URL(string: encoding!)
print("url : \(String(describing: url))")
if let _url = url {
var request = URLRequest(url: _url)
request.httpMethod = "get" //get : Get 방식, post : Post 방식
//header 설정
// request.setValue("", forHTTPHeaderField: "")
//post body 설정
// var requestBody : String = ""
// request.httpBody = requestBody.data(using: .utf8)
let session = URLSession.shared
let task = session.dataTask(with: request, completionHandler: { (data: Data?, response: URLResponse?, error: Error?) in
guard error == nil && data != nil else {
if let err = error {
print(err.localizedDescription)
}
return
}
if let _data = data {
if let strData = NSString(data: _data, encoding: String.Encoding.utf8.rawValue) {
let str = String(strData)
print(str)
DispatchQueue.main.async {
txtResponse.text = str
}
}
}else{
print("data nil")
}
})
task.resume()
}
}
'iOS' 카테고리의 다른 글
Segue (0) | 2019.12.31 |
---|---|
Protocol (0) | 2019.12.30 |
(SWIFT) JSON Parse (0) | 2019.04.25 |
(SWIFT) Toast (0) | 2019.04.25 |
(SWIFT) 화면 전환 (0) | 2019.04.25 |