Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?)

Thanks in advance!

like image 781
LinusGeffarth Avatar asked Sep 17 '14 05:09

LinusGeffarth


People also ask

How do I navigate to the App Store?

It's the blue button in the bottom-right corner of your iPhone's keyboard. Find an app you like. You may have to scroll through the apps listed on this page or re-enter a new search query to do so. You can also go back to one of the tabs visited previously and tap on an app you like.

Where is iOS App Store URL?

Navigate to My Apps > [the app whose link you want], then under More, you'll see "View on App Store". Right click that and copy the link.


Video Answer


3 Answers

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)
}
like image 62
Atomix Avatar answered Oct 11 '22 10:10

Atomix


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)
    }
}
like image 20
Stuart Casarotto Avatar answered Oct 11 '22 08:10

Stuart Casarotto


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 {
like image 20
djdance Avatar answered Oct 11 '22 09:10

djdance