简体   繁体   中英

Swift - Can not get Almofire.request

I'm currently coding a weather app, I'm copying it from a Udemy tutorial on swift.

I am currently in the process of getting the weather data, so I would like a request from Almofire as shown in the video.

as a beginner this is a bit difficult to explain so I just show the code:

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) }
}

Swift 5

Try this code, will work with you perfectly. Once we set location data, we will call the API directly.

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)
        }
    }
}

Note : Do not forget to enable HTTP mode by adding the following keys in your Info.plist (open it as source code)

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

For further reading: iOS - Transport security has blocked a cleartext HTTP

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM