我正在尝试创建一个基本的天气应用程序,但现在我在应用程序中初始化我的个人 API 时遇到了问题。问题可能出在 URL 上,因为如果我更改它,它就会起作用。但是,我目前拥有的 API 是唯一适用于我国天气的 API。
附言另一个有效的网址来 self 正在关注的在线类(class)
这是行不通的
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>**nea.gov.sg**</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
同时,这有效
<key>NSAppTransportSecurity</key>
<dict>
<key>NSExceptionDomains</key>
<dict>
<key>**openweathermap.org**</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>
那么是不是因为 nea.gov.sg 比 openweathermap.org 多了一个域名?因此,为了回答一些问题,两者都是 http://,我尝试打印出这两个 api,但只有 openweathermap.org 的 url 与这两个相同的代码 一起工作。
这是我使用的API调用代码
let url = URL(string: "http://api.nea.gov.sg/api/WebAPI/?dataset=2hr_nowcast&keyref=*[keyCode]*")!
let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
if error != nil {
print(error!)
} else {
if let urlContent = data {
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
print(jsonResult)
} catch {
print("JSON Processing Failed")
}
}
}
}
task.resume()
最佳答案
您是否尝试过将其替换为您的 plist?
<key>**api.nea.gov.sg**</key>
关于JSONSerialisation API 不同的 url 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46128520/