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?

user2397282
  • 3,798
  • 15
  • 48
  • 94

9 Answers9

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

Chris Droukas
  • 3,196
  • 1
  • 23
  • 26
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)
    }
}
Okhan Okbay
  • 1,374
  • 12
  • 26
Andrej
  • 7,266
  • 4
  • 38
  • 57
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 :)

Sasho
  • 3,532
  • 1
  • 31
  • 30
  • @Eric D, thanks. However I am using the XCode 8, beta 3 and it goes only with "shared()" – Sasho Jul 29 '16 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 '16 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")
}
reza_khalafi
  • 6,230
  • 7
  • 56
  • 82
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)
iVarun
  • 6,496
  • 2
  • 26
  • 34
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))
    }
}
Amr Lotfy
  • 2,937
  • 5
  • 36
  • 56
3

Swift 3 - XCode 8.2.1

UIApplication.shared.openURL(URL(string: "itms-apps://itunes.apple.com/app/id" + appStoreAppID)!)
ergunkocak
  • 3,334
  • 1
  • 32
  • 31
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)

mlepicki
  • 391
  • 2
  • 11
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)
Timchang Wuyep
  • 629
  • 7
  • 10