Launching App Store from App in Swift
Asked Answered
F

9

23

I am creating an app, and I have a banner which promotes my other app. This is my code:

var barsButton : UIButton = UIButton(frame: CGRectMake((self.view.bounds.width / 2) - 51, self.view.bounds.height - 100, 102, 30))
barsButton.setImage(UIImage(named: "Bars Icon 2.png"), forState: .Normal)
barsButton.addTarget(self, action: "openBarsLink", forControlEvents: UIControlEvents.TouchUpInside)

func openBarsLink() {
    var barsLink : String = "itms-apps:https://itunes.apple.com/app/bars/id706081574?mt=8"
    UIApplication.sharedApplication().openURL(NSURL.URLWithString(barsLink))
}

However, when the user presses the button, it just takes them to the App Store, and not the specific page for my app. What am I doing wrong?

Fail answered 19/9, 2014 at 18:19 Comment(0)
E
26

You have too many protocols in your URL. Get rid of https: so the URL reads

itms-apps://itunes.apple.com/app/bars/id706081574

Egress answered 19/9, 2014 at 18:30 Comment(0)
J
21

Just by following older answers I couldn't make it work, so here I post my complete solution:

if let url  = NSURL(string: "itms-apps://itunes.apple.com/app/id1234567890"), 
   UIApplication.shared.canOpenURL(url) {
    
  UIApplication.shared.openURL(url)
    }
}
Jodijodie answered 5/2, 2015 at 16:29 Comment(0)
N
6

Use just the short "itms://".

For Swift 3 this is the snippet:

UIApplication.shared.openURL(URL(string: "itms://itunes.apple.com/app/id" + appStoreAppID)!)

I hope this helps someone.

Cheers.

P.S. @Eric Aya was ahead of the time :)

Newhall answered 29/7, 2016 at 14:46 Comment(2)
@Eric D, thanks. However I am using the XCode 8, beta 3 and it goes only with "shared()"Newhall
Ah, sorry, my bad. I was pretty sure this one changed too. I guess they're not finished modifying the APIs. :)Keturahkeung
E
4

I had this problem but this code just works on the phone not simulator. So check this code:

if let url = URL(string: "itms-apps://itunes.apple.com/app/id" + APP_ID ),
    UIApplication.shared.canOpenURL(url){
    UIApplication.shared.openURL(url)
}else{
    //Just check it on phone not simulator!
    print("Can not open")
}
Evanston answered 14/6, 2017 at 8:29 Comment(1)
nice call for "open it on a real device", I thought I was going crazy.Botticelli
C
4

As openURL is deprecated from iOS 10 use below code:

UIApplication.shared.open((URL(string: "itms://itunes.apple.com/app/" + appStoreAppID)!), options:[:], completionHandler: nil)
Christopher answered 29/9, 2017 at 17:19 Comment(0)
S
4

Simply you can use these functions in a utility struct to goto app page in app store also you can goto rate app view directly:

static func gotoApp(appID: String, completion: ((_ success: Bool)->())? = nil) {
    let appUrl = "itms-apps://itunes.apple.com/app/id\(appID)"

    gotoURL(string: appUrl, completion: completion)
}

static func rateApp(appId: String, completion: ((_ success: Bool)->())? = nil) {
    //let appUrl = "itms-apps://itunes.apple.com/app/" + appId
    let appUrl = "https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=\(appId)&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"
    //TODO: use &action=write-review for opening review directly
    print("app review URL: ", appUrl)

    gotoURL(string: appUrl, completion: completion)
}

static func gotoURL(string: String, completion: ((_ success: Bool)->())? = nil) {
    print("gotoURL: ", string)
    guard let url = URL(string: string) else {
        print("gotoURL: invalid url", string)
        completion?(false)
        return
    }
    if #available(iOS 10, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: completion)
    } else {
        completion?(UIApplication.shared.openURL(url))
    }
}
Standard answered 3/4, 2018 at 6:51 Comment(0)
F
3

Swift 3 - XCode 8.2.1

UIApplication.shared.openURL(URL(string: "itms-apps://itunes.apple.com/app/id" + appStoreAppID)!)
Follow answered 10/2, 2017 at 8:46 Comment(0)
T
1

Link you are trying to open is not valid - remove https: schema from it (or itms: - but I suggest first option, to avoid redirects)

Trainee answered 19/9, 2014 at 18:33 Comment(0)
T
1

I use this and it works.

let locale: String = Locale.current.regionCode ?? "US"
UIApplication.shared.open(URL(string: "https://apps.apple.com/\(locale)/developer/{developer-name}/{idXXXXXXXXXX}")!, options: [:], completionHandler: nil)
Turkish answered 2/7, 2020 at 16:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.