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?

9 Answers 9

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

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

2
  • @Eric D, thanks. However I am using the XCode 8, beta 3 and it goes only with "shared()"
    – Sasho
    Jul 29, 2016 at 14:54
  • Ah, sorry, my bad. I was pretty sure this one changed too. I guess they're not finished modifying the APIs. :)
    – Eric Aya
    Jul 29, 2016 at 14:56
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")
}
1
  • nice call for "open it on a real device", I thought I was going crazy.
    – thexande
    Sep 19, 2017 at 22:54
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)
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))
    }
}
3

Swift 3 - XCode 8.2.1

UIApplication.shared.openURL(URL(string: "itms-apps://itunes.apple.com/app/id" + appStoreAppID)!)
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)

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.