Swift - URLSession: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „== Links == Swift (Programmiersprache) == Einführung == Mit URLSession kannst du Anfragen an einen Webserver generieren und Antworten verarbeiten. == S…“)
 
Zeile 10: Zeile 10:
 
* Give URLSession a Task (give the browser a url to fetch)
 
* Give URLSession a Task (give the browser a url to fetch)
 
* Start the task
 
* Start the task
 +
 +
=== Beispiel ===
 +
<syntaxhighlight lang="swift">
 +
//
 +
//  WeatherManager.swift
 +
//  Clima
 +
//
 +
//  Created by Stephan Schlegel on 08.01.23.
 +
//  Copyright © 2023 App Brewery. All rights reserved.
 +
//
 +
 +
import Foundation
 +
 +
struct WeatherManager{
 +
   
 +
    let weatherURL = "https://api.openweathermap.org/data/2.5/weather?units=metric&appid=63981dbcfa548021dd77394d24f34674"
 +
   
 +
    func fetchWeather(cityName: String){
 +
        let urlString = "\(weatherURL)&q=\(cityName)"
 +
        performRequest(urlString: urlString)
 +
    }
 +
   
 +
    func performRequest(urlString: String){
 +
        // 1. create URL
 +
        if let url = URL(string: urlString) {
 +
            // 2. create URLSession
 +
            let session = URLSession(configuration: .default)
 +
            // 3. give session a task
 +
            let task = session.dataTask(with: url, completionHandler: handle(data:response:error:))
 +
            // 4. start the task
 +
            task.resume()
 +
        }
 +
    }
 +
   
 +
    func handle(data: Data?, response: URLResponse?, error: Error?){
 +
        if error != nil {
 +
            print(error!)
 +
            return
 +
        }
 +
        if let safeData = data {
 +
            let dataString = String(data: safeData, encoding: .utf8)
 +
            print(dataString!)
 +
        }
 +
    }
 +
}
 +
 +
</syntaxhighlight>

Version vom 17. Januar 2023, 22:04 Uhr

Links

Swift (Programmiersprache)

Einführung

Mit URLSession kannst du Anfragen an einen Webserver generieren und Antworten verarbeiten.

Schritte

  • Create a URL
  • Create a URLSession (kind of Browser Object)
  • Give URLSession a Task (give the browser a url to fetch)
  • Start the task

Beispiel

//
//  WeatherManager.swift
//  Clima
//
//  Created by Stephan Schlegel on 08.01.23.
//  Copyright © 2023 App Brewery. All rights reserved.
//

import Foundation

struct WeatherManager{
    
    let weatherURL = "https://api.openweathermap.org/data/2.5/weather?units=metric&appid=63981dbcfa548021dd77394d24f34674"
    
    func fetchWeather(cityName: String){
        let urlString = "\(weatherURL)&q=\(cityName)"
        performRequest(urlString: urlString)
    }
    
    func performRequest(urlString: String){
        // 1. create URL
        if let url = URL(string: urlString) {
            // 2. create URLSession
            let session = URLSession(configuration: .default)
            // 3. give session a task
            let task = session.dataTask(with: url, completionHandler: handle(data:response:error:))
            // 4. start the task
            task.resume()
        }
    }
    
    func handle(data: Data?, response: URLResponse?, error: Error?){
        if error != nil {
            print(error!)
            return
        }
        if let safeData = data {
            let dataString = String(data: safeData, encoding: .utf8)
            print(dataString!)
        }
    }
}