3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【SwiftUI】URLを開く

Posted at

はじめに

私は今までUIApplication.shared.openを使用してURLを開いていましたが、openURLという環境値が追加されていて、SwiftUIではこちらを使うべきなのでは??と思ったので記事にしておきます。

古い

import SwiftUI

struct ContentView: View {
    var body: some View {
        Button {
            UIApplication.shared.open(URL(string: "https://qiita.com/SNQ-2001")!)
        } label: {
            Text("URLを開く")
        }
    }
}

新しい

import SwiftUI

struct ContentView: View {
    @Environment(\.openURL) var openURL
    var body: some View {
        Button {
            openURL(URL(string: "https://qiita.com/SNQ-2001")!)
        } label: {
            Text("URLを開く")
        }
    }
}

おわり

この辺は面白いことができそうなのでよく調べておきたいです

Text("Visit [Example Company](https://www.example.com) for details.")
    .environment(\.openURL, OpenURLAction { url in
        handleURL(url) // Define this method to take appropriate action.
        return .handled
    })
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?