【问题标题】:Swift - Can not get Almofire.requestSwift - 无法获取 Alamofire.request
【发布时间】:2020-09-26 12:57:50
【问题描述】:

我目前正在编写一个天气应用程序,我正在从 swift 上的 Udemy 教程中复制它。

我目前正在获取天气数据,所以我想要视频中所示的 Almofire 的请求。

作为初学者,这有点难以解释,所以我只显示代码:

import UIKit
import CoreLocation
import Alamofire

class WetterViewController: UIViewController, CLLocationManagerDelegate {

// MARK: Outlet
@IBOutlet weak var WetterImage: UIImageView!
@IBOutlet weak var temperatureLabel: UILabel!
@IBOutlet weak var StatusLabel: UILabel!

let WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather"
let APP_ID = "3084f92d04fb27b6dd25f1a3a8afd221"

let locationManager = CLLocationManager()

override func viewDidLoad()
{
    super.viewDidLoad()

    // Latitude = Breitegrad
    // longitude = Längengrad
    //altitude = höhe
    // accuray = genauigkeit
    
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters //Genauigkeit einstellen
    locationManager.requestWhenInUseAuthorization() // Fragen ob position verwendet werden darf
    locationManager.startUpdatingLocation() //Fängt an GPS location zu empfangen 
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations[locations.count - 1]
    
    print("Längengrad: \(location.coordinate.latitude)")
    print("breitengrad: \(location.coordinate.longitude)")
    
    let latitude = String(location.coordinate.latitude)
    let longitude = String(location.coordinate.longitude)
    
    let data : [String : String] = ["lat" : latitude , "lon" : longitude , "appid" : APP_ID]
}

func getWeatherData (url: String , data: [String: String]) {
    Alamofire.request(url, method: .get, parameters: data) }
}

【问题讨论】:

  • 最后一行说:Almofire.request 但是请求不被识别,我只能做Alomfire.Request,但这不是我需要的
  • 在新的 alamo fire 版本中使用 AF.request
  • @marcuchka 你看过下面的答案了吗?我已经测试过了。
  • 感谢@EssamMohamedFahmi 对其进行测试´!
  • 现在我想写 func getWeatherData (url: String , data: [String: String]) { AF.request(url, method: .get, parameters: data).responseJSON { (response)在如果响应。结果。 (isSuccess 但它为什么不起作用?)

标签: ios swift alamofire


【解决方案1】:

斯威夫特 5

试试这个代码,它将完美地与你合作。一旦我们设置了位置数据,我们将直接调用 API。

import UIKit
import CoreLocation
import Alamofire

class WetterViewController: UIViewController, CLLocationManagerDelegate
{
    // MARK: Outlet

    @IBOutlet weak var WetterImage: UIImageView!
    @IBOutlet weak var temperatureLabel: UILabel!
    @IBOutlet weak var StatusLabel: UILabel!

    // MARK: Properties

    let weatherURL = "http://api.openweathermap.org/data/2.5/weather"
    let appID = "3084f92d04fb27b6dd25f1a3a8afd221"

    var data: [String: String] = [:]
    {
        didSet
        {
            getWeatherData(from: weatherURL, with: data)
        }
    }

    let locationManager = CLLocationManager()

    // MARK: View Controller Life Cycle

    override func viewDidLoad()
    {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

    // MARK: Methods

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations:     [CLLocation])
    {
        let location = locations[locations.count - 1]

        let latitude = String(location.coordinate.latitude)
        let longitude = String(location.coordinate.longitude)

        data = ["lat": latitude,
                "lon" : longitude,
                "appid": appID]
    }

    func getWeatherData(from url: String, with parameters: [String: String])
    {
        AF.request(url, method: .get, parameters: parameters).response { response in

            // parsing your response
            debugPrint(response)
        }
    }
}

注意:不要忘记通过在Info.plist 中添加以下键来启用HTTP 模式(作为源代码打开)

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

进一步阅读: iOS - Transport security has blocked a cleartext HTTP

【讨论】:

    猜你喜欢
    • 2017-03-18
    • 2015-11-08
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多