Swift Open Link in Safari - webview

I am currently opening the link in my app in a WebView, but I'm looking for an option to open the link in Safari instead.

It's not "baked in to Swift", but you can use standard UIKit methods to do it. Take a look at UIApplication's openUrl(_:) (deprecated) and open(_:options:completionHandler:).
Swift 4 + Swift 5 (iOS 10 and above)
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.open(url)
Swift 3 (iOS 9 and below)
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.shared.openURL(url)
Swift 2.2
guard let url = URL(string: "https://stackoverflow.com") else { return }
UIApplication.sharedApplication().openURL(url)

New with iOS 9 and higher you can present the user with a SFSafariViewController (see documentation here). Basically you get all the benefits of sending the user to Safari without making them leave your app. To use the new SFSafariViewController just:
import SafariServices
and somewhere in an event handler present the user with the safari view controller like this:
let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)
The safari view will look something like this:

UPDATED for Swift 4: (credit to Marco Weber)
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
UIApplication.shared.openURL(requestUrl as URL)
}
OR go with more of swift style using guard:
guard let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") else {
return
}
UIApplication.shared.openURL(requestUrl as URL)
Swift 3:
You can check NSURL as optional implicitly by:
if let requestUrl = NSURL(string: "http://www.iSecurityPlus.com") {
UIApplication.sharedApplication().openURL(requestUrl)
}

Swift 5
Swift 5: Check using canOpneURL if valid then it's open.
guard let url = URL(string: "https://iosdevcenters.blogspot.com/") else {
return
}
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

Swift 3 & IOS 10.2
UIApplication.shared.open(URL(string: "http://www.stackoverflow.com")!, options: [:], completionHandler: nil)
Swift 3 & IOS 10.2

since iOS 10 you should use:
guard let url = URL(string: linkUrlString) else {
return
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}

In Swift 1.2:
#IBAction func openLink {
let pth = "http://www.google.com"
if let url = NSURL(string: pth){
UIApplication.sharedApplication().openURL(url)
}

In Swift 2.0:
UIApplication.sharedApplication().openURL(NSURL(string: "http://stackoverflow.com")!)

Swift 5
if let url = URL(string: "https://www.google.com") {
UIApplication.shared.open(url)
}

if your using SwiftUI:
Link("Stack Overflow", destination: URL(string: "https://www.stackoverflow.com/")!)

IOS 11.2 Swift 3.1- 4
let webView = WKWebView()
override func viewDidLoad() {
super.viewDidLoad()
guard let url = URL(string: "https://www.google.com") else { return }
webView.frame = view.bounds
webView.navigationDelegate = self
webView.load(URLRequest(url: url))
webView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
view.addSubview(webView)
}
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: #escaping (WKNavigationActionPolicy) -> Void) {
if navigationAction.navigationType == .linkActivated {
if let url = navigationAction.request.url,
let host = url.host, !host.hasPrefix("www.google.com"),
UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
print(url)
print("Redirected to browser. No need to open it locally")
decisionHandler(.cancel)
} else {
print("Open it locally")
decisionHandler(.allow)
}
} else {
print("not a user click")
decisionHandler(.allow)
}
}

Related

openURL and open in IOS 9 / 10

-UIApplication.shared.openURL(url)
-UIApplicartion.shared.open(url,options: [:],completionHandler: nil)
Can I use these two options in iOS9 and iOS10?
Is UIApplication.shared.openURL(url) supported in iOS9 and/or iOS10?
You can try this
guard let url = URL(string: "http://www.google.com") else {
return //be safe
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
Yes you have to use both by putting them in condition like this,
func open(scheme: String) {
if let url = URL(string: scheme) {
if #available(iOS 10, *) { // For ios 10 and greater
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(scheme): \(success)")
})
} else { // for below ios 10.
let success = UIApplication.shared.openURL(url)
print("Open \(scheme): \(success)")
}
}
}
guard let url = URL(string: "https://www.google.com/") else { return }
if #available(iOS 10.0, *) {
UIApplication.shared.open(url)
} else {
// Fallback on earlier versions
let svc = SFSafariViewController(url: url)
present(svc, animated: true, completion: nil)
}
I used this when I want to Open a URL in Safari browser in iOS 9 and above.

trigger app store to open in mobile to open particular app [duplicate]

Could you guys help me to translate the following code into Swift?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4"]];
(or do I have to take this link: itms://itunes.apple.com/app/id839686104?)
Here. But I highly suggest you learn the basics of Swift!
UIApplication.sharedApplication().openURL(NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")!)
If you wanna open the AppStore in Swift 5:
if let url = URL(string: "itms-apps://itunes.apple.com/app/id1629135515") {
UIApplication.shared.open(url)
}
Swift 3 Syntax and improved with an 'if let'
if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.openURL(url)
}
UPDATE 7/5/17 (Thank you Oscar for pointing this out):
if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
UIApplication.shared.canOpenURL(url)
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
I use this combination, its better for rate/shopping.
(partially from here)
#IBAction func rateMe(sender: AnyObject) {
if #available(iOS 8.0, *) {
openStoreProductWithiTunesItemIdentifier("107698237252");
} else {
var url = NSURL(string: "itms://itunes.apple.com/us/app/xxxxxxxxxxx/id107698237252?ls=1&mt=8")
if UIApplication.sharedApplication().canOpenURL(url!) == true {
UIApplication.sharedApplication().openURL(url!)
}
}
}
func openStoreProductWithiTunesItemIdentifier(identifier: String) {
let storeViewController = SKStoreProductViewController()
storeViewController.delegate = self
let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
storeViewController.loadProductWithParameters(parameters) { [weak self] (loaded, error) -> Void in
if loaded {
// Parent class of self is UIViewContorller
self?.presentViewController(storeViewController, animated: true, completion: nil)
}
}
}
func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
viewController.dismissViewControllerAnimated(true, completion: nil)
}
don't forget to import and delegate:
import StoreKit
class RateMeViewController: UIViewController, SKStoreProductViewControllerDelegate {
Swift 4 with completion handler:
Make sure to update your id in the appStoreUrlPath
func openAppStore() {
if let url = URL(string: "itms-apps://itunes.apple.com/app/id..."),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.open(url, options: [:]) { (opened) in
if(opened){
print("App Store Opened")
}
}
} else {
print("Can't Open URL on Simulator")
}
}
For Swift 5 (tested code) to open App Store link
if let url = URL(string: "https://itunes.apple.com/in/app/your-appName/id123456?mt=8")
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
else {
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
}
}
Since other answers didn't work for me (Swift, Xcode 6.1.1) here I post my solution:
var url = NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")
if UIApplication.sharedApplication().canOpenURL(url!) {
UIApplication.sharedApplication().openURL(url!)
}
Check newer app update available on iTunes in Swift 3
let currentAppVersion = Bundle.main.infoDictionary
Alamofire.request("http://itunes.apple.com/jp/lookup/?id=548615", method: .get, parameters: nil, headers: nil).responseJSON { response in
if let value = response.result.value as? [String: AnyObject] {
let versionNum = value["results"]?.value(forKey: "version") as? NSArray
if versionNum?[0] as! String != currentAppVersion?["CFBundleShortVersionString"] as! String {
self.alertForUpdateApp()
}
}
}
func alertForUpdateApp() {
let alertController = UIAlertController(title: "Update Available", message: "There is a newer version of this app available", preferredStyle: .alert)
let alertActionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let alertActionUpdate = UIAlertAction(title: "Update", style: .default, handler: { _ in
if let url = URL(string: Constants.API_REDIRECT_TO_ITUNES),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
})
alertController.addAction(alertActionCancel)
alertController.addAction(alertActionUpdate)
let pushedViewControllers = (self.window?.rootViewController as! UINavigationController).viewControllers
let presentedViewController = pushedViewControllers[pushedViewControllers.count - 1]
presentedViewController.present(alertController, animated: true, completion: nil)
}
Swift 5.0:
import StoreKit
extension YourViewController: SKStoreProductViewControllerDelegate {
func openStoreProductWithiTunesItemIdentifier(_ identifier: String) {
let storeViewController = SKStoreProductViewController()
storeViewController.delegate = self
let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
storeViewController.loadProduct(withParameters: parameters) { [weak self] (loaded, error) -> Void in
if loaded {
self?.present(storeViewController, animated: true, completion: nil)
}
}
}
private func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
viewController.dismiss(animated: true, completion: nil)
}
}
// How to use
openStoreProductWithiTunesItemIdentifier("12345")
If you want to open in app store use
let appstoreUrl = "https://itunes.apple.com/in/app/myapp-test/id11111111?mt=8"
UIApplication.shared.openURL(URL(string: appstoreUrl)!)
if you want in itune store use
let ituneUrl = "itms-apps://itunes.apple.com/in/app/myapp-test/id11111111?mt=8"
UIApplication.shared.openURL(URL(string: ituneUrl)!)
In Swift 4.2 and Xcode 10.2
You have two ways to open App Store or iTunes Store
If you want to open App Store use https or if you want to open iTunes Store use itms
Ex: https://itunes.apple.com/in/app/yourAppName/id14****4?mt=8 //For App Store
itms://itunes.apple.com/in/app/yourAppName/id14****4?mt=8 //For iTunes Store
Method 1: This is simple direct and old approach
let url = NSURL(string: "https://itunes.apple.com/in/app/smsdaddy/id1450172544?mt=8")//itms https
if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.openURL(url! as URL)
}
Method 2: New approach
if let url = URL(string: "https://itunes.apple.com/in/app/smsdaddy/id1450172544?ls=1&mt=8") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
// Earlier versions
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
}
}
For the new AppStore, simply open your app's link on AppStore and replace the https scheme to itms-apps scheme.
Example on Swift 4:
if let url = URL(string: "itms-apps://itunes.apple.com/us/app/my-app/id12345678?ls=1&mt=8") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
You can find your app's link on the App Information page.
Swift 5
let url = "your app url"
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
else {
// Earlier versions
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
}
SwiftUI
import StoreKit
struct StoreView: UIViewControllerRepresentable {
let appID: String
func makeUIViewController(context: UIViewControllerRepresentableContext<StoreView>) -> SKStoreProductViewController {
let sKStoreProductViewController = SKStoreProductViewController()
let parameters = [ SKStoreProductParameterITunesItemIdentifier : appID]
sKStoreProductViewController.loadProduct(withParameters: parameters)
return sKStoreProductViewController
}
func updateUIViewController(_ uiViewController: SKStoreProductViewController, context: UIViewControllerRepresentableContext<StoreView>) {
}
}
//how to use
.sheet(isPresented: $showAppAtStore){
StoreView(appID: "12345678")
}
var url = NSURL(string: "itms-apps://itunes.apple.com/app/id1024941703")
if UIApplication.sharedApplication().canOpenURL(url!) == true {
UIApplication.sharedApplication().openURL(url!)
}
Xcode 6.4
For me didn't worked one of this answers. (Swift 4) The app always opened the iTunes Store not the AppStore.
I had to change the url to "http://appstore.com/%Appname%" as described at this apple Q&A: https://developer.apple.com/library/archive/qa/qa1633/_index.html
for example like this
private let APPSTORE_URL = "https://appstore.com/keynote"
UIApplication.shared.openURL(URL(string: self.APPSTORE_URL)!)
(remove spaces from the app-name)
For those looking for the updated working solution
Most of the given solutions here are outdated & deprecated, like "canOpenURL", "openURL" etc.
let appStoreLink = "https://apps.apple.com/app/{app-name}/{app-id}"
guard let url = URL(string: appStoreLink) else { return }
UIApplication.shared.open(url)

How to open iCloud Settings through app

I am trying to open the user's iCloud settings through my iOS App. Currently, I have this:
#IBAction func openSettings(_ sender: AnyObject) {
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.openURL(settingsUrl)
}
}
However, this opens the app's. How can I open the user's iCloud settings? Thanks!
Try this:
let settingsCloudKitURL = URL(string: "App-Prefs:root=CASTLE")
if let url = settingsCloudKitURL, UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}

How do I open Instagram app from my iOS app?

I am trying to open the Instagram application from my iOS app through UIButton action but it's not working. It shows nothing.
Here is my code in Swift 3:
#IBAction func Instagram(_ sender: AnyObject) {
let instagramURL = URL(string: "instagram://app")!
if UIApplication.shared.canOpenURL(instagramURL) {
UIApplication.shared.openURL(instagramURL)
}
For swift 4.2+ and ios 9+
This code launch Instagram, if it's not installed, launch the Instagram profile page in a web browser.
let screenName = "mehdico" // CHANGE THIS
let appURL = URL(string: "instagram://user?username=\(screenName)")
let webURL = URL(string: "https://instagram.com/\(screenName)")
if UIApplication.shared.canOpenURL(appURL) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(appURL)
}
} else {
//redirect to safari because the user doesn't have Instagram
if #available(iOS 10.0, *) {
UIApplication.shared.open(webURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(webURL)
}
}
The method openUrl which you have posted in comments in deprecated too in iOS 10
Use this
#IBAction func openInstagramButtonPressed(_ sender: Any) {
let instagram = URL(string: "instagram://app")!
if UIApplication.shared.canOpenURL(instagram) {
UIApplication.shared.open(instagram, options: ["":""], completionHandler: nil)
} else {
print("Instagram not installed")
}
}
For the first time it will prompt you to open or cancel.
Also make sure to do the LSApplicationQueriesSchemes entry for instagram which you had already done.

Open AppStore through button

Could you guys help me to translate the following code into Swift?
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4"]];
(or do I have to take this link: itms://itunes.apple.com/app/id839686104?)
Here. But I highly suggest you learn the basics of Swift!
UIApplication.sharedApplication().openURL(NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")!)
If you wanna open the AppStore in Swift 5:
if let url = URL(string: "itms-apps://itunes.apple.com/app/id1629135515") {
UIApplication.shared.open(url)
}
Swift 3 Syntax and improved with an 'if let'
if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.openURL(url)
}
UPDATE 7/5/17 (Thank you Oscar for pointing this out):
if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
UIApplication.shared.canOpenURL(url)
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
}
I use this combination, its better for rate/shopping.
(partially from here)
#IBAction func rateMe(sender: AnyObject) {
if #available(iOS 8.0, *) {
openStoreProductWithiTunesItemIdentifier("107698237252");
} else {
var url = NSURL(string: "itms://itunes.apple.com/us/app/xxxxxxxxxxx/id107698237252?ls=1&mt=8")
if UIApplication.sharedApplication().canOpenURL(url!) == true {
UIApplication.sharedApplication().openURL(url!)
}
}
}
func openStoreProductWithiTunesItemIdentifier(identifier: String) {
let storeViewController = SKStoreProductViewController()
storeViewController.delegate = self
let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
storeViewController.loadProductWithParameters(parameters) { [weak self] (loaded, error) -> Void in
if loaded {
// Parent class of self is UIViewContorller
self?.presentViewController(storeViewController, animated: true, completion: nil)
}
}
}
func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
viewController.dismissViewControllerAnimated(true, completion: nil)
}
don't forget to import and delegate:
import StoreKit
class RateMeViewController: UIViewController, SKStoreProductViewControllerDelegate {
Swift 4 with completion handler:
Make sure to update your id in the appStoreUrlPath
func openAppStore() {
if let url = URL(string: "itms-apps://itunes.apple.com/app/id..."),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.open(url, options: [:]) { (opened) in
if(opened){
print("App Store Opened")
}
}
} else {
print("Can't Open URL on Simulator")
}
}
For Swift 5 (tested code) to open App Store link
if let url = URL(string: "https://itunes.apple.com/in/app/your-appName/id123456?mt=8")
{
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
else {
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
}
}
Since other answers didn't work for me (Swift, Xcode 6.1.1) here I post my solution:
var url = NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")
if UIApplication.sharedApplication().canOpenURL(url!) {
UIApplication.sharedApplication().openURL(url!)
}
Check newer app update available on iTunes in Swift 3
let currentAppVersion = Bundle.main.infoDictionary
Alamofire.request("http://itunes.apple.com/jp/lookup/?id=548615", method: .get, parameters: nil, headers: nil).responseJSON { response in
if let value = response.result.value as? [String: AnyObject] {
let versionNum = value["results"]?.value(forKey: "version") as? NSArray
if versionNum?[0] as! String != currentAppVersion?["CFBundleShortVersionString"] as! String {
self.alertForUpdateApp()
}
}
}
func alertForUpdateApp() {
let alertController = UIAlertController(title: "Update Available", message: "There is a newer version of this app available", preferredStyle: .alert)
let alertActionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let alertActionUpdate = UIAlertAction(title: "Update", style: .default, handler: { _ in
if let url = URL(string: Constants.API_REDIRECT_TO_ITUNES),
UIApplication.shared.canOpenURL(url){
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
})
alertController.addAction(alertActionCancel)
alertController.addAction(alertActionUpdate)
let pushedViewControllers = (self.window?.rootViewController as! UINavigationController).viewControllers
let presentedViewController = pushedViewControllers[pushedViewControllers.count - 1]
presentedViewController.present(alertController, animated: true, completion: nil)
}
Swift 5.0:
import StoreKit
extension YourViewController: SKStoreProductViewControllerDelegate {
func openStoreProductWithiTunesItemIdentifier(_ identifier: String) {
let storeViewController = SKStoreProductViewController()
storeViewController.delegate = self
let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
storeViewController.loadProduct(withParameters: parameters) { [weak self] (loaded, error) -> Void in
if loaded {
self?.present(storeViewController, animated: true, completion: nil)
}
}
}
private func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
viewController.dismiss(animated: true, completion: nil)
}
}
// How to use
openStoreProductWithiTunesItemIdentifier("12345")
If you want to open in app store use
let appstoreUrl = "https://itunes.apple.com/in/app/myapp-test/id11111111?mt=8"
UIApplication.shared.openURL(URL(string: appstoreUrl)!)
if you want in itune store use
let ituneUrl = "itms-apps://itunes.apple.com/in/app/myapp-test/id11111111?mt=8"
UIApplication.shared.openURL(URL(string: ituneUrl)!)
In Swift 4.2 and Xcode 10.2
You have two ways to open App Store or iTunes Store
If you want to open App Store use https or if you want to open iTunes Store use itms
Ex: https://itunes.apple.com/in/app/yourAppName/id14****4?mt=8 //For App Store
itms://itunes.apple.com/in/app/yourAppName/id14****4?mt=8 //For iTunes Store
Method 1: This is simple direct and old approach
let url = NSURL(string: "https://itunes.apple.com/in/app/smsdaddy/id1450172544?mt=8")//itms https
if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.openURL(url! as URL)
}
Method 2: New approach
if let url = URL(string: "https://itunes.apple.com/in/app/smsdaddy/id1450172544?ls=1&mt=8") {
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
// Earlier versions
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
}
}
For the new AppStore, simply open your app's link on AppStore and replace the https scheme to itms-apps scheme.
Example on Swift 4:
if let url = URL(string: "itms-apps://itunes.apple.com/us/app/my-app/id12345678?ls=1&mt=8") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
You can find your app's link on the App Information page.
Swift 5
let url = "your app url"
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
else {
// Earlier versions
if UIApplication.shared.canOpenURL(url as URL) {
UIApplication.shared.openURL(url as URL)
}
}
SwiftUI
import StoreKit
struct StoreView: UIViewControllerRepresentable {
let appID: String
func makeUIViewController(context: UIViewControllerRepresentableContext<StoreView>) -> SKStoreProductViewController {
let sKStoreProductViewController = SKStoreProductViewController()
let parameters = [ SKStoreProductParameterITunesItemIdentifier : appID]
sKStoreProductViewController.loadProduct(withParameters: parameters)
return sKStoreProductViewController
}
func updateUIViewController(_ uiViewController: SKStoreProductViewController, context: UIViewControllerRepresentableContext<StoreView>) {
}
}
//how to use
.sheet(isPresented: $showAppAtStore){
StoreView(appID: "12345678")
}
var url = NSURL(string: "itms-apps://itunes.apple.com/app/id1024941703")
if UIApplication.sharedApplication().canOpenURL(url!) == true {
UIApplication.sharedApplication().openURL(url!)
}
Xcode 6.4
For me didn't worked one of this answers. (Swift 4) The app always opened the iTunes Store not the AppStore.
I had to change the url to "http://appstore.com/%Appname%" as described at this apple Q&A: https://developer.apple.com/library/archive/qa/qa1633/_index.html
for example like this
private let APPSTORE_URL = "https://appstore.com/keynote"
UIApplication.shared.openURL(URL(string: self.APPSTORE_URL)!)
(remove spaces from the app-name)
For those looking for the updated working solution
Most of the given solutions here are outdated & deprecated, like "canOpenURL", "openURL" etc.
let appStoreLink = "https://apps.apple.com/app/{app-name}/{app-id}"
guard let url = URL(string: appStoreLink) else { return }
UIApplication.shared.open(url)

Resources