在iOS中獲取URL我有添加查詢參數在Swift3
https://api.asiancar.com/api/applicationsettings
的URL它基本上是一個GET URL,所以我需要傳遞一個boolean
「isMobile」和timestamp
作爲查詢參數。如何實現這一目標作爲傳遞查詢後,最終的URL看起來像這樣:
https://api.asiancar.com/api/applicationsettings?timestamp=111122244556789879&isMobile=true
let queryItems = [
NSURLQueryItem(timestamp: "1234568878788998989", isMobile: true),
NSURLQueryItem(timestamp: "1234568878788998989", isMobile: true)
]
let urlComps = NSURLComponents(string: "www.api.asiancar.com/api/applicationsettings")!
urlComps.queryItems = queryItems
let URL = urlComps.URL!
我做正確的或任何其他修改?請告訴。
您可以先創建網址的字符串,然後使用'讓URL = URL(字符串:「」)'創建最終的URL。 – Akhilrajtr
請發佈答案 –
儘管接受的答案在技術上會起作用,但我強烈建議您查看我的答案並繼續使用url組件。一旦習慣了它們,它們更加靈活並且不易出錯 – BJHStudios