加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift + AFNetworking获取天气信息

发布时间:2020-12-14 06:14:26 所属栏目:百科 来源:网络整理
导读:课程地址 http://www.imooc.com/video/2475 如何实现 xcode8.3,swift3.0+ 环境需要做如下步骤才可以看到信息 ios app访问需要https,修改Info.plist 添加几个属性 就可以使用http访问网络 方法参考 http://www.jb51.cc/article/p-qhwfhcxo-b.html 其中的xcod

课程地址 http://www.imooc.com/video/2475

如何实现

xcode8.3,swift3.0+ 环境需要做如下步骤才可以看到信息

  • ios app访问需要https,修改Info.plist 添加几个属性 就可以使用http访问网络
    方法参考 http://www.52php.cn/article/p-qhwfhcxo-b.html 其中的xcode7.1 设置

  • 参考 @白天不懂天黑黑 方法
    http://api.openweathermap.org/data/2.5/weather 需要appid, 不想自己申请点话,
    使用@白天不懂天黑黑 方法 提供的appid 如何使用?

self.updateWeatherInfo(latitude: location.coordinate.latitude,longitude: location.coordinate.longitude,appid: "4f4be8fe7031dddd5dec789e01c1b3ac")
  • updateWeatherInfo 具体实现
func updateWeatherInfo(latitude: CLLocationDegrees,longitude: CLLocationDegrees,appid: String) {

        let manager = AFHTTPSessionManager()
        let url = "http://api.openweathermap.org/data/2.5/weather"

        let params = ["lat": latitude,"lon": longitude,"appid": appid,"cnt": 0] as [String : Any]

        manager.get(url,parameters: params,progress: {(progress: Progress) in print("progress")},success: {(operation:URLSessionDataTask!,responSEObject: Any!) 
        in print("JSON: " + (responSEObject as AnyObject).description)},failure: {(operation:URLSessionDataTask?,error: Error!)  
        in print("Error: " + error.localizedDescription)})

    }
  • 完成以上,就可以打印出 经纬度信息 以及weather json数据, 但是仔细发现 有可能经纬度信息是旧金山,
    如果需要打印本地经纬度的话,需要在ios 模拟器中手动设置一次本地经纬度信息
    方法参考:http://blog.csdn.net/qq_35502977/article/details/52823419?locationNum=8&fps=1
    如何查找本地经纬度, 参考 http://www.gpsspg.com/maps.htm

  • 要是有更好的方式,请多多的留言。 祝?, 学习愉快

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读