5
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 3 years have passed since last update.

Arsaga App Division 2021🎅🏻Advent Calendar 2021

Day 21

[SwiftUI] 意図せず外部ブラウザを開いてしまう問題。

Last updated at Posted at 2021-12-20

TextやButtonなど、URLを文字列で格納しているViewを押下した際に、外部ブラウザを開いてしまう問題。

以下の場合、Text("遷移した")に遷移せずに、外部ブラウザ(safari)でhttps://qiita.com/trend を開いてしまう

NG1.gif

VStack(spacing: 30) {
    NavigationLink(destination: Text("遷移した")) {
        Text("https://qiita.com/trend")
    }
}
.foregroundColor(Color.materialColor(colorCode: .amber))
.background(Color.materialColor(colorCode: .teal))

解決

遷移させたいURLを変数に格納して使うことで、外部ブラウザを開くのを回避することができる。

VStack(spacing: 30) {
    let URL = "https://qiita.com/trend"
    NavigationLink(destination: Text("遷移した")) {
        Text(URL)
    }   
}
.foregroundColor(Color.materialColor(colorCode: .amber))
.background(Color.materialColor(colorCode: .teal))

OK1.gif

5
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
5
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?