【发布时间】:2018-02-18 02:11:00
【问题描述】:
我正在尝试创建一个基本的天气应用程序,但现在在我的应用程序中初始化我的个人 api 时遇到了问题。问题可能在于 URL,因为如果我更改它,它就可以工作。但是,我目前拥有的 api 是唯一适用于我国天气的。
附注另一个有效的网址来自我正在关注的在线课程
这个不行
<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>
那么,与 openweathermap.org 相比,是不是因为 nea.gov.sg 多了一个域名?因此,为了回答一些问题,两者都是 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()
【问题讨论】:
-
两个网址都是 http 还是一个 https?另外,当您说“它不起作用”时,您是什么意思,您是否遇到错误?
-
当我打印出api时,只有openweather.org的那个有效,当两个代码完全相同时,只有info.plist的这个源代码不同
-
你可以在这里添加你的api调用代码吗?
-
此链接可能对您有所帮助:nea.gov.sg/docs/default-source/api/developer%27s-guide.pdf。您是否按照此处列出的步骤进行操作?
-
@PranavKasetti 是的,我做了,我也检查过了,是的,我把 api 调用代码放在最上面
标签: json swift weather-api openweathermap app-transport-security