SafariのSwiftOpen Link



Swift Open Link Safari



解決:

'Swiftに焼き付けられた'ではありませんが、標準を使用できますそれを行うためのUIKitメソッド。 UIApplicationのを見てくださいopenUrl(_ :) (非推奨)open(_:options:completionHandler :)。

Swift 4 + Swift 5(iOS 10以降)



ガードleturl = URL(string: 'https://stackoverflow.com')else {return} UIApplication.shared.open(url)

Swift 3(iOS 9以下)

ガードleturl = URL(string: 'https://stackoverflow.com')else {return} UIApplication.shared.openURL(url)

スイフト2.2



ガードleturl = URL(string: 'https://stackoverflow.com')else {return} UIApplication.sharedApplication()。openURL(url) 

iOS 9以降の新機能として、ユーザーにSFSafariViewController(ここのドキュメントを参照)。基本的に、ユーザーをアプリから離れさせることなく、ユーザーをSafariに送ることのすべてのメリットを享受できます。新しいSFSafariViewControllerを使用するには、次のようにします。

SafariServicesをインポートします

そして、イベントハンドラーのどこかで、次のようなサファリビューコントローラーをユーザーに提示します。

let svc = SFSafariViewController(url:url)present(svc、animated:true、completion:nil)

サファリビューは次のようになります。



ここに画像の説明を入力してください


Swift 4の更新:(Marco Weberのクレジット)

if let requestUrl = NSURL(string: 'http://www.iSecurityPlus.com'){UIApplication.shared.openURL(requestUrl as URL)}

またはを使用してより迅速なスタイルで行くガード:

guard let requestUrl = NSURL(string: 'http://www.iSecurityPlus.com')else {return} UIApplication.shared.openURL(requestUrl as URL)

スウィフト3:

NSURLは、次の方法で暗黙的にオプションとして確認できます。

if let requestUrl = NSURL(string: 'http://www.iSecurityPlus.com'){UIApplication.sharedApplication()。openURL(requestUrl)}