问题
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?)
Thanks in advance!
回答1:
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")!)
回答2:
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)
}
}
回答3:
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 {
回答4:
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!)
}
回答5:
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")
}
}
回答6:
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)
}
回答7:
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)
}
}
}
回答8:
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.
回答9:
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)!)
回答10:
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)
}
}
}
回答11:
Swift 5
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)
}
}
回答12:
var url = NSURL(string: "itms-apps://itunes.apple.com/app/id1024941703")
if UIApplication.sharedApplication().canOpenURL(url!) == true {
UIApplication.sharedApplication().openURL(url!)
}
Xcode 6.4
回答13:
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)
来源:https://stackoverflow.com/questions/44499611/trigger-app-store-to-open-in-mobile-to-open-particular-app