可以通过SPM(Swift Package Manager)导入您的项目
https://github.com/zhuiyizhiqiu/SimpleNetwork.git
首先要引入框架
import SimpleNetwork
sn.request(url: "https://www.sojson.com/api/qqmusic/8446666/json") { (result,dic) in
switch result{
case .failure(let error,_):
print(error)
case .success:
print(dic!)
}
}
sn.request(url: "http://api.qingyunke.com/api.php?key=free&appid=0&msg=北京天气") { (result,dic) in
switch result{
case .failure(let error,_):
print("error =",error)
case .success:
print("返回键值对",dic!)
}
}
struct data: Codable {
var status = 0
var msg = ""
}
sn.request(url: "要访问的url") { (result, response: data?) in
switch result{
case .failure(let str,_):
print(str)
case .success:
print(response!.status,response!.msg)
}
}
let head = [
"token" : "eyJhbGciOiJIUzI1NiJ9.eyJjcmVhdGVUaW1lIjoxNTk4NTkyOTgxNTY0LCJ0b2tlblZlcnNpb24iOjYsInBvd2VyIjoxMCwidXNlcklkIjoyfQ.v7j7K4PNwLyTpwUcc_UmGYdrUTU16il_orECivCzWR4"
]
sn.request(url: "http://www.youapi.com",head: head) { (result, dic) in
switch result{
case .failure(let error, _):
print("error =",error)
case .success:
print(dic!)
}
}
let paraments = [
"user" : "youname",
"pasword": "12345"
]
sn.request(url: "http://www.youapi.com",paraments: paraments,httpMethod: .post) { (result, dic) in
switch result{
case .failure(let error, _):
print("error =",error)
case .success:
print(dic!)
}
}
let image = UIImage(named: "yourImageName")
let imagesString = image!.jpegData(compressionQuality: 0.1)!.base64EncodedString()
let paraments = [
"image" : imagesString
]
sn.request(url: "http://www.youapi.com",paraments: paraments,httpMethod: .post) { (result, dic) in
switch result{
case .failure(let error, _):
print("error =",error)
case .success:
print(dic!)
}
}