ios - How to open external Url inside NavigationDestination in swiftUI - Stack Overflow

.navigationDestination(for: Element.self) { element inif let urlString = element.link?.externalUrlString

        .navigationDestination(for: Element.self) { element in
                        if let urlString = element.link?.externalUrlString, let url = URL(string: urlString) {
                              openURL(url)
    }
}

I get below error: 'buildExpression' is unavailable: this expression does not conform to 'View'

        .navigationDestination(for: Element.self) { element in
                        if let urlString = element.link?.externalUrlString, let url = URL(string: urlString) {
                              openURL(url)
    }
}

I get below error: 'buildExpression' is unavailable: this expression does not conform to 'View'

Share Improve this question asked Mar 20 at 15:08 AppleDeveloperAppleDeveloper 1,4593 gold badges21 silver badges52 bronze badges 2
  • 1 An external URL is not a "navigation destination". Do you want to push a SFSafariViewController or something? Otherwise, just use a button and call openURL in its action. – Sweeper Commented Mar 20 at 15:10
  • Please post this an an answer. – AppleDeveloper Commented Mar 20 at 15:58
Add a comment  | 

1 Answer 1

Reset to default 1

An external URL is not a "navigation destination". You should replace the NavigationLink

NavigationLink("Some Title", value: someElement)

with a Button:

Button("Some Title") {
    if let urlString = element.link?.externalUrlString, let url = URL(string: urlString) {
        openURL(url)
    }
}

If the NavigationLink was originally in a List, and you want to keep the trailing chevron, see SwiftUI List disclosure indicator without NavigationLink.


If you want to navigate to a SFSafariViewController that shows that URL, see How do I use SFSafariViewController with SwiftUI?.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744400817a4572376.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信