21
votes

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?

9

9 Answers

25
votes

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

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

18
votes

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)
    }
}
6
votes

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

4
votes

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")
}
4
votes

As openURL is deprecated from iOS 10 use below code:

UIApplication.shared.open((URL(string: "itms://itunes.apple.com/app/" + appStoreAppID)!), options:[:], completionHandler: nil)
4
votes

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))
    }
}
3
votes

Swift 3 - XCode 8.2.1

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

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

0
votes

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)