我必须调用GET API,比如
需要。
http://ctesterst.net/api/Customers/homeData?lat=12.456&lng=76.786¤t_app_version=123¤t_device=%7B%22Test%22%3A%22123%22%2C%22name%22%3A%22something%22%2C%22tested%22%3Atrue%7D&access_token=Km3R2AbU0yzAcVkp9BCxmCmaoC5k20fBiQxfLhIBIAolwJGgYw5w5E8X0NZzlDh8
像这样的我都要调用。
输入参数怎么加?
我是这样做的。
我的json字符串是:请求模型
struct RequestData: Codable {
struct Devices: Codable {
let test: String
let name: String
let tested:Bool
}
let current_device: Devices
}
和我的网址是:
urlAppend = val + "&lat=\(lati)" + "&lng=\(long)" + "¤t_app_version=\(appVersion)" + "¤t_device=\(jsonString)"
它越来越崩溃......请给我解决方案。
请给我解决方案。
你可以使用URLComponents来实现,URLComponents是在Foundation中定义的。
var components = URLComponents()
components.scheme = "http"
components.host = "ctesterst.net"
components.path = "/api/Customers/homeData"
let queryItemLat = URLQueryItem(name: "lat", value: "12.456")
let queryItemLong = URLQueryItem(name: "lng", value: "76.786")
let queryItemAppVersion = URLQueryItem(name: "current_app_version", value: "123")
let queryItemDevice = URLQueryItem(name: "current_device", value: "something")
let queryItemToken = URLQueryItem(name: "access_token", value: "Km3R2AbU0yzAcVkp9BCxmCmaoC5k20fBiQxfLhIBIAolwJGgYw5w5E8X0NZzlDh8")
components.queryItems = [queryItemLat, queryItemLong,queryItemAppVersion,queryItemDevice,queryItemToken]
print(components.url)