安装cocoapods

  • Post author:
  • Post category:其他


sudo gem install cocoapods

发生错误安装错误

rudy版本低 安装失败 指定cocoapods 版本安装

查看是否安装成功

安装依赖。

CocoaPods.org

export https_proxy=http://127.0.0.1:7890

安装第三方功能包

双击


import

Alamofire

AF.request("https://devapi.qweather.com/v7/weather/now?location=\(lon),\(lat)&key=ae7874c28151478b9e8d9b161c23834d").responseJSON { response in
            if let data = response.value{
                print(data)
            }
        }


利用SwiftyJSon解析json类型的数据

//
//  ViewController.swift
//  Weather
//
//  Created by Mac on 2023/4/1.
//

import UIKit
// 第一步 导入包
import CoreLocation
import Alamofire
import SwiftyJSON
// dekegate -- 代理/委托
// protocol协议
class ViewController: UIViewController ,CLLocationManagerDelegate{
    // 温度
    @IBOutlet weak var tempLabel: UILabel!
    @IBOutlet weak var iconImageView:UIImageView!
    
    @IBOutlet weak var cityLable: UILabel!
    // 位置管理器
    let locationManager = CLLocationManager()
    // 实例化类
    let weather = Weather()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        // 请求用户授权获取当前位置
        // 当用户使用app时允许获取当前位置
        // 在info.plist 添加 Priacy-Location Where in Usage Description
        locationManager.requestWhenInUseAuthorization()
        locationManager.delegate = self
        
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
        locationManager.requestLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // 在delegate方法中获取经纬度 配置模拟器的位置
        let lon = locations[0].coordinate.longitude
        let lat = locations[0].coordinate.latitude
        // 模拟器 features - Location Custom Location 设置模拟器的经纬度
        print(lon)
        print(lat)
        // 网络请求
        AF.request("https://devapi.qweather.com/v7/weather/now?location=\(lon),\(lat)&key=5df7d38f0afd459c912068f290510581").responseJSON { response in
            if let data = response.value{
                //print(data)
                // 转化为真正的json
                let weatherJSON = JSON(data)
               // print(weatherJSON["now"]["temp"])
                //print(weatherJSON["refer"]["sources"][0])
                // °
                self.weather.temp = "\(weatherJSON["now","temp"].stringValue)°"
                //self.weather.icon = weatherJSON["now","icon"].stringValue
                self.tempLabel.text = self.weather.temp
                //self.iconImageView.image = UIImage(named: weatherJSON["now","icon"].stringValue)
            }
        }
        
        AF.request("https://geoapi.qweather.com/v2/city/lookup?location=\(lon),\(lat)&key=5df7d38f0afd459c912068f290510581").responseJSON { response in
            if let data = response.value{
                let cityJSON = JSON(data)
                self.weather.city = cityJSON["location",0,"name"].stringValue
                print(cityJSON["location","name"])
                print(self.weather.city)
            }
        }
        
    }
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        print(error)
    }


}



版权声明:本文为weixin_38107457原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。