Skip to content

Instantly share code, notes, and snippets.

@benigumocom
Last active May 27, 2024 07:56
Show Gist options
  • Save benigumocom/747d321bf35d4a354905145a42d3972c to your computer and use it in GitHub Desktop.
Save benigumocom/747d321bf35d4a354905145a42d3972c to your computer and use it in GitHub Desktop.
【SwiftUI】シンプルに HTTPリクエスト でお天気情報取得 👉 https://android.benigumo.com/20240109/url-lines/
import SwiftUI
struct WeatherButton: View {
@State private var text = "GET"
private let url = URL(string: "https://wttr.in/?format=3")!
var body: some View {
Button(text) {
text = "Loading ..."
Task {
for try await line in url.lines {
text = line
}
// let (data, _) = try await URLSession.shared.data(from: url)
// text = String(data: data, encoding: .utf8)?.trim(.whitespacesAndNewlines) ?? "ERROR"
}
}
.buttonStyle(.borderedProminent)
}
}
#Preview {
WeatherButton()
}
@benigumocom
Copy link
Author

sc 2024-05-26 at 21 20 48

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment