首页 > 解决方案 > 'Link' is only available in macOS 11.0 or newer

问题描述

I have a text link that opens a web page externally with safari from my macOS app written with SwiftUI.

var body: some View {          
   Link("Visit Apple", destination: URL(string: "https://www.apple.com")!) 
}

This only works if my deployment target is for macOS 11.0 and above but i want it to work for 10.15 which shows these errors:

'Link' is only available in macOS 11.0 or newer 
 
'init(_:destination:)' is only available in macOS 11.0 or newer

Is there an alternative way to open external links for 10.15?

标签: swiftswiftui

解决方案


You can use open(_:options:completionHandler:).

Try the following:

var body: some View {
    Button("Visit Apple") {
        UIApplication.shared.open(URL(string: "https://www.apple.com")!)
    }
}

To open the URL on macOS:

NSWorkspace.shared.open(URL(string: "https://www.apple.com")!)

推荐阅读