iOS开发Swift-10-位置授权, cocoapods,API,天气获取,城市获取-和风天气App首页代码

1.获取用户当前所在的位置

在infi中点击加号,选择权限:当用户使用app的时候获取位置权限.

填写使用位置权限的目的.

2.获取用户的经纬度.

ViewController:

复制代码
import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {    //遵循CLLocationManagerDelegate协议
    //位置管理器
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        locationManager.requestWhenInUseAuthorization()    //请求当用户正在使用app的时候允许后台得到用户位置.只会弹出来一次
        locationManager.delegate = self    //位置管理器代理人是view controller对象.希望view controller能通过实现CLLocationManagerDelegate协议中的方法,获取到当前用户位置信息
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers    //设置需要的位置精度(三公里误差精度)
        locationManager.requestLocation()    //请求用户位置
        
        
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  //requesLocation请求到了的话会执行这个方法
        let lon = locations[0].coordinate.longitude   //精度.因为location可能实时变化(地图app),所以是一个数组.我们只需要使用第一个获取到的位置.
        let lat = locations[0].coordinate.latitude    //纬度
        print(lon)
        print(lat)
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {    //requesLocation请求失败的话会执行这个方法
        print(error)
    }


}

3.通过第三方服务获取当前天气

(1)安装cocoapods

https://dev.qweather.com/

在网站中可以找到,当向 https://devapi.qweather.com/v7/weather/now?\[请求参数\] 发送请求时,就可以得到当前天气.

通过依赖管理工具cocoapods命令行工具进行依赖管理.在终端里对它进行下载并安装,

在Mac的启动台上找到「终端」应用,或者在触控板上通过四指聚合来打开「启动台---终端」。

在命令行输入

复制代码
sudo gem install cocoapods

(以超级管理员的身份,安装cocoapods这个应用.)

输入电脑开机密码.

等待安装成功.

这里可能出现Error installing cocoapods:

The last version of activesupport (>= 5.0, < 8) to support your Ruby & RubyGems was 6.1.7.6. Try installing it with `gem install activesupport -v 6.1.7.6` and then running the current command again

activesupport requires Ruby version >= 2.7.0. The current ruby version is 2.6.10.210.

这个bug,解决方式请参照博客:https://www.cnblogs.com/lysboke/p/17678896.html

(2)安装功能包

在cocoapods官网https://cocoapods.org/的搜索栏搜索Alamofire.点击进入.

用cocoapods安装Alamofire.

在终端中输入cd空格,将项目文件夹Weather拖入到cd后面.点击回车.

终端输入

复制代码
pod init

等待.完成之后打开Weather文件夹,发现成功创建Podfile文件.

将Podfile拖入Xcode,Podfile自动弹出.

加入代码:

复制代码
  pod 'Alamofire'

保存并关闭Podfile.

在终端输入命令安装功能包:

复制代码
pod install

关闭Xcode,从Weather文件中打开Weather.xcworkspace,可以看到项目结构如下.

4.利用和风API获取当前位置的天气信息

注册和风天气账号.在和风天气中创建一个免费的新项目.

https://console.qweather.com/#/apps/create-app/create

得到key.

编写代码得到当前经纬度下的天气数据信息.

复制代码
import UIKit
import CoreLocation
import Alamofire   //引入和风API包

class ViewController: UIViewController, CLLocationManagerDelegate {    //遵循CLLocationManagerDelegate协议
    //位置管理器
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        locationManager.requestWhenInUseAuthorization()    //请求当用户正在使用app的时候允许后台得到用户位置.只会弹出来一次
        locationManager.delegate = self    //位置管理器代理人是view controller对象.希望view controller能通过实现CLLocationManagerDelegate协议中的方法,获取到当前用户位置信息
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers    //设置需要的位置精度(三公里误差精度)
        locationManager.requestLocation()    //请求用户位置
        
        
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  //requesLocation请求到了的话会执行这个方法
        let lon = locations[0].coordinate.longitude   //精度.因为location可能实时变化(地图app),所以是一个数组.我们只需要使用第一个获取到的位置.
        let lat = locations[0].coordinate.latitude    //纬度
        //print(lon)
        //print(lat)
        AF.request("https://devapi.qweather.com/v7/weather/now?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                print(data)
            }
        }      //请求和风API的网址,得到当前位置的天气
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {    //requesLocation请求失败的话会执行这个方法
        print(error)
    }
}

为了在测试中使用http传输,在Info中新增一个key和Value:App Transport Security Settings,在它里边再添加一个Allow Arbitrary Loads,Value设置为Yes.

5.解析和风API返回的JSON数据

在Xcode中的Podfile中添加如下:

复制代码
pod 'SwiftyJSON', '~> 4.0'

保存之后在终端输入:

复制代码
pod install

下载之后引入JSON解析包,写入代码,测试.

复制代码
import UIKit
import CoreLocation
import Alamofire   //引入和风API包
import SwiftyJSON   //引入解析JSON数据的包

class ViewController: UIViewController, CLLocationManagerDelegate {    //遵循CLLocationManagerDelegate协议
    //位置管理器
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        locationManager.requestWhenInUseAuthorization()    //请求当用户正在使用app的时候允许后台得到用户位置.只会弹出来一次
        locationManager.delegate = self    //位置管理器代理人是view controller对象.希望view controller能通过实现CLLocationManagerDelegate协议中的方法,获取到当前用户位置信息
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers    //设置需要的位置精度(三公里误差精度)
        locationManager.requestLocation()    //请求用户位置
        
        
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  //requesLocation请求到了的话会执行这个方法
        let lon = locations[0].coordinate.longitude   //精度.因为location可能实时变化(地图app),所以是一个数组.我们只需要使用第一个获取到的位置.
        let lat = locations[0].coordinate.latitude    //纬度
        //print(lon)
        //print(lat)
        AF.request("https://devapi.qweather.com/v7/weather/now?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let weatherJSON = JSON(data)
                print(weatherJSON)
            }
        }      //请求和风API的网址,得到当前位置的天气
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {    //requesLocation请求失败的话会执行这个方法
        print(error)
    }
}

启动运行:

6.拿到天气数据并展示在界面上

把温度拖拽到ViewController中.

同理,将天气图标和用户当前所在的城市也拖拽到代码区.

新建一个swift文件:

Weather.swift:

复制代码
import Foundation

class Weather{
    var temp = ""
    var icon = ""
    var city = ""
}

ViewController:

复制代码
import UIKit
import CoreLocation
import Alamofire   //引入和风API包
import SwiftyJSON   //引入解析JSON数据的包

class ViewController: UIViewController, CLLocationManagerDelegate {    //遵循CLLocationManagerDelegate协议
    @IBOutlet weak var tempLable: UILabel!
    
    @IBOutlet weak var iconImageView: UIImageView!
    
    @IBOutlet weak var cityLable: UILabel!
    
    //位置管理器
    let locationManager = CLLocationManager()
    //Weather.swift实例化
    let weather = Weather()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        locationManager.requestWhenInUseAuthorization()    //请求当用户正在使用app的时候允许后台得到用户位置.只会弹出来一次
        locationManager.delegate = self    //位置管理器代理人是view controller对象.希望view controller能通过实现CLLocationManagerDelegate协议中的方法,获取到当前用户位置信息
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers    //设置需要的位置精度(三公里误差精度)
        locationManager.requestLocation()    //请求用户位置
        
        
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  //requesLocation请求到了的话会执行这个方法
        let lon = locations[0].coordinate.longitude   //精度.因为location可能实时变化(地图app),所以是一个数组.我们只需要使用第一个获取到的位置.
        let lat = locations[0].coordinate.latitude    //纬度
        //print(lon)
        //print(lat)
        AF.request("https://devapi.qweather.com/v7/weather/now?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let weatherJSON = JSON(data)
                //print(weatherJSON["now"]["temp"])    //或weatherJSON["now", "temp"]
                //print(weatherJSON["refer"]["sources"][0])     //weatherJSON["refer", "sources", 0]
                
                //MVC结构
                self.weather.temp = "\(weatherJSON["now"]["temp"].stringValue)˚"
                self.weather.icon = weatherJSON["now"]["icon"].stringValue
                
                self.tempLable.text = self.weather.temp
                self.iconImageView.image = UIImage(named: self.weather.icon)
            }
        }      //请求和风API的网址,得到当前位置的天气
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {    //requesLocation请求失败的话会执行这个方法
        print(error)
    }
}

启动测试:

7.获取用户当前所在的城市

ViewController:

复制代码
import UIKit
import CoreLocation
import Alamofire   //引入和风API包
import SwiftyJSON   //引入解析JSON数据的包

class ViewController: UIViewController, CLLocationManagerDelegate {    //遵循CLLocationManagerDelegate协议
    @IBOutlet weak var tempLable: UILabel!
    
    @IBOutlet weak var iconImageView: UIImageView!
    
    @IBOutlet weak var cityLable: UILabel!
    
    //位置管理器
    let locationManager = CLLocationManager()
    //Weather.swift实例化
    let weather = Weather()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        locationManager.requestWhenInUseAuthorization()    //请求当用户正在使用app的时候允许后台得到用户位置.只会弹出来一次
        locationManager.delegate = self    //位置管理器代理人是view controller对象.希望view controller能通过实现CLLocationManagerDelegate协议中的方法,获取到当前用户位置信息
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers    //设置需要的位置精度(三公里误差精度)
        locationManager.requestLocation()    //请求用户位置
        
        
    }
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {  //requesLocation请求到了的话会执行这个方法
        let lon = locations[0].coordinate.longitude   //精度.因为location可能实时变化(地图app),所以是一个数组.我们只需要使用第一个获取到的位置.
        let lat = locations[0].coordinate.latitude    //纬度
        //print(lon)
        //print(lat)
        AF.request("https://devapi.qweather.com/v7/weather/now?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let weatherJSON = JSON(data)
                //print(weatherJSON["now"]["temp"])    //或weatherJSON["now", "temp"]
                //print(weatherJSON["refer"]["sources"][0])     //weatherJSON["refer", "sources", 0]
                
                //MVC结构
                self.weather.temp = "\(weatherJSON["now"]["temp"].stringValue)˚"
                self.weather.icon = weatherJSON["now"]["icon"].stringValue
                
                self.tempLable.text = self.weather.temp
                self.iconImageView.image = UIImage(named: self.weather.icon)
                
                
            }
        }      //请求和风API的网址,得到当前位置的天气
        
        AF.request("https://geoapi.qweather.com/v2/city/lookup?location=\(lon),\(lat)&key=a91848aaab484a3599a703b139dfe87b").responseJSON { response in
            if let data = response.value{
                let cityJSON = JSON(data)
                //处理数据
                self.weather.city = cityJSON["location", 0, "name"].stringValue
                //处理AI
                self.cityLable.text = self.weather.city
            }
        }
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {    //requesLocation请求失败的话会执行这个方法

cityLable.text = "获取用户城市失败"

复制代码
    }


}

启动测试:

相关推荐
code2cat24 分钟前
Java高级篇之Socket:从建立连接到完整收发一条消息
java·开发语言
凯瑟琳.奥古斯特32 分钟前
OCR文字检测算法对比解析
开发语言·python·职场和发展
用户693717500138433 分钟前
当你向领导申请 AI 编程工具预算时,如果领导追问“能提效多少,有量化数据吗”,你该怎么回答?
android·后端·ios
Layer37 分钟前
一些脱离 Mac 依赖的 Agent 操作 iPhone 实现
ios·ai编程·测试
Misnice38 分钟前
Python 内置函数大全
开发语言·windows·python
美狐美颜SDK开放平台39 分钟前
直播APP集成美颜SDK,如何梳理第三方组件的数据权限?
开发语言·人工智能·深度学习·音视频·第三方美颜sdk
怪我冷i1 小时前
Rust 核心语法与内存安全机制
开发语言·安全·rust
开开心心_Every1 小时前
免费压缩软件WinRAR无弹窗、广告干扰
java·开发语言·前端·spring·智能手机·word·winrar
江沉晚呤时1 小时前
零依赖的代价与自由:我用 C# 自研了一套 Office 读写库
开发语言·c#
想做小南娘,发现自己是女生喵2 小时前
QT项目实战第一周(嵌入式)
开发语言·qt