func makeLogon() {
let url:URL = URL(string: "https://xxx/xxx/API?xxx")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "GET"
let task = session.dataTask(with: request as URLRequest) {(
data, response, error) in
guard let data = data, let _:URLResponse = response, error == nil else {
print("error")
return
let dataString = String(data: data, encoding: String.Encoding.utf8)
let dict = self.getDictionaryFromJSONString(jsonString: dataString!)
print(dict)
task.resume()
func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{
let jsonData:Data = jsonString.data(using: .utf8)!
let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
if dict != nil {
return dict as! NSDictionary
return NSDictionary()
func makeUpdateCountSheet() {
let jsonString = "{\"Data\":{\"xxx\":\"834\",\"xxx\":[{\"xxx\":[{\"xxx\":\"031019\",\"xxx\":\"ADD\",\"xxx\":\"9\"},{\"xxx\":\"5651G-06920ADBAA\",\"xxx\":\"ADD\",\"xxx\":\"6\"}],\"xxx\":\"xxx\",\"Counted\":true,\"xxx\":true,\"LineNum\":\"1\",\"xxx\":\"235\",\"Quantity\":\"15\"}],\"xxx\":\"\",\"Initials\":\"we\",\"xxx\":true},\"xxx\":\"\"}"
let dict = self.getDictionaryFromJSONString(jsonString: jsonString)
print(dict)
var jsonData = NSData()
do {
jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) as NSData
} catch {
print(error.localizedDescription)
let url:URL = URL(string: "https://xxx/xxx/API?xxx")!
let session = URLSession.shared
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("\(jsonData.length)", forHTTPHeaderField: "Content-Length")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData as Data
let task = session.dataTask(with: request as URLRequest) {(
data, response, error) in
guard let data = data, let _:URLResponse = response, error == nil else {
print("error")
return
let dataString = String(data: data, encoding: String.Encoding.utf8)
let dict = self.getDictionaryFromJSONString(jsonString: dataString!)
print(dict)
task.resume()
表象上GET请求体中没有数据,参数都在URL中,而POST请求将数据放置到请求体中。本质上GET和POST是没什么区别的,所以上面两个例子其实是一样一样的。
对于swift4.2的网络请求的封装。包含链式,类AFN及对moya的封装。封装的功能包含:1.一次性处理请求指示器HUD
2.一次性处理请求状态码及错误弹窗
3.登录过期自动跳转到登录页等。
4.可以处理所有接口都要使用的公共参数。
import UIKit
class ViewController: UIViewController,UITableViewDataSource,MJRefreshBaseViewDelegate {
//表格属性
var table:UITableView?
var t...
最近因为新手自学iOS Swift,然后写了一套聚合SDK,对网络请求那块第三方框架暂时不是很懂,也没有去深入研究他们(alfire跟moya),所以后面就选用了原生的URLSession,所以对他封装了成为一个方法,直接调用使用!
1. /// MARK:- 聚合SDK GET请求(带参数)
class func antUEGET(path: String,paras: Dictionary<String,Any>?,success: @escaping ((_ result: Any) -&
import UIKit import AFNetworking //请求方法 /// - GET: get /// - POST: post enum RequsetMethod:String { case GET = "GET" case POST = "POST"
只要属于移动开发的范畴,网络请求一定是其中的重头戏,Object-C的网络请求大家都不陌生,不过感觉很熟悉的貌似也不多,因为第三方的库用起来更加方便,比如我们所熟知的AFNetworking,在Swift中,也存在一些封装的比较好的网络请求库,不过我今天说的不是那种第三方的,而是原生的请求方法,下面看代码:import UIKitclass ViewController: UIViewControl