0

I like to forward app to iTunes or Apple Music (like done for Instagram or FV) when user clicks a button, below code gives found nil error. Is there a way to do that? Or even better play previews in my app.

 var url  = NSURL(string: "itms://itunes.apple.com/us/album/ne-olacak-dj-funky-c-vs-ogün-dalka-single/id1202943921")

    if UIApplication.shared.canOpenURL(url! as URL) {
        UIApplication.shared.openURL(url! as URL)
    }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Emre Önder
  • 2,408
  • 2
  • 23
  • 73

1 Answers1

1

This method expects URLString to contain only characters that are allowed in a properly formed URL. All other characters must be properly percent escaped. Any percent-escaped characters are interpreted using UTF-8 encoding.

and you need to encode it before passing it to NSURL, you can do this via stringByAddingPercentEncodingWithAllowedCharacters

 let url  = URL(string: "itms://itunes.apple.com/us/album/ne-olacak-dj-funky-c-vs-ogün-dalka-single/id1202943921".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)
     if UIApplication.shared.canOpenURL(url! as URL) {
        UIApplication.shared.open(url!, options: [:], completionHandler: nil)
    }

for that error you need to follow this

This is a new enforced security measure that apple has implemented on any app that is build in iOS 9.

The only solution so far is to add an entry in the info.plist file with the Key LSApplicationQueriesSchemes and add "itms" and any other url scheme that your app will be linking to in this array.

LSApplicationQueriesSchemes

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
  • I'm getting this error2017-03-26 22:23:43.285 Direct Link Test[18493:514076] -canOpenURL: failed for URL: "itms://itunes.apple.com/us/album/ne-olacak-dj-funky-c-vs-og%C3%BCn-dalka-single/id1202943921" - error: "This app is not allowed to query for scheme itms" @Anbu.Karthik – Emre Önder Mar 26 '17 at 19:24
  • 2017-03-27 12:40:40.879 Direct Link Test[1095:17687] -canOpenURL: failed for URL: "itms://itunes.apple.com/us/album/ne-olacak-dj-funky-c-vs-og%C3%BCn-dalka-single/id1202943921" - error: "The operation couldn’t be completed. (OSStatus error -10814.)" I'm getting this error now. – Emre Önder Mar 27 '17 at 09:41
  • see this once http://stackoverflow.com/questions/39241201/the-operation-couldn-t-be-completed-osstatus-error-10827 – Anbu.Karthik Mar 27 '17 at 09:49
  • I tried to open it on real device and It works. I think the problem was with simulator. Thank you :) – Emre Önder Mar 27 '17 at 10:06
  • @EmreÖnder - happy to hear bro – Anbu.Karthik Mar 27 '17 at 10:06