Opening a url is failing in Swift
Opening a url is failing in Swift
breakpointing inside the callback and logging success shows false.
I added itms-apps to my plist: LSApplicationQueriesSchemes
and put this in my app delegate:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return true
}
func moreTapped() {
let url = NSURL(string: "itms-apps://itunes.com/developer/quantum-productions/id979315877")
if #available(iOS 10.0, *) {
UIApplication.shared.open(url! as URL, options: [:], completionHandler: {(success: Bool) in
})
} else {
UIApplication.shared.openURL(url! as URL)
}
}
I try on a device, get "Cannot Connect to App Store". Device is online
– quantumpotato
Jun 22 at 0:03
You don't call
canOpenURL(_:)
first? Do you have any log message?– Larme
Jun 22 at 5:30
canOpenURL(_:)
Follow this; stackoverflow.com/questions/433907/…
– Shoaib
Jun 22 at 5:52
Try URL as itms://itunes.apple.com/us/app/quantum-productions/id979315877
– Shoaib
Jun 22 at 5:52
3 Answers
3
You have the incorrect format for your URL, and you should use URL
rather than NSURL
in Swift.
URL
NSURL
This will open your developer page in the App Store app:
if let url = URL(string:"itms-apps://geo.itunes.apple.com/developer/quantum-productions/id979315877&mt=8") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
There is no need to call canOpenURL
for standard URL types such as http/s
and itms
.
canOpenURL
http/s
itms
Are you sure the scheme isn't
itms-apps
? I just tried it on mobile safari and at least from there I am not asked to open it in the AppStore app, but with itms-app I am. The domain correction etc you propose (using geo
) seems correct.– Gero
Jun 29 at 7:39
itms-apps
geo
You're right. I'll fix that
– Paulw11
Jun 29 at 7:42
Boinks, you edited right as I saved. Kudos, the answer is correct. :)
– Gero
Jun 29 at 7:42
Itms also kind of works, but it opens the iTunes Store instead of the App Store, which isn't what is wanted.
– Paulw11
Jun 29 at 7:44
Apple just announced the appstore.com URLs.
**
https://developer.apple.com/library/ios/qa/qa1633/_index.html
**
To create an App Store Short Link, apply the following rules to your company or app name:
Remove all whitespace
Convert all characters to lower-case
Remove all copyright (©), trademark (™) and registered mark (®)
symbols
Replace ampersands ("&") with "and"
Remove most punctuation (See Listing 2 for the set)
Replace accented and other "decorated" characters (ü, å, etc.) with
their elemental character (u, a, etc.)
Leave all other characters as-is.
Listing 2 Punctuation characters that must be removed.
!¡"#$%'()*+,-./:;<=>¿?@^_`{|}~
Below are some examples to demonstrate the conversion that takes
place.
App Name examples
Ocarina => http://appstore.com/ocarina
According to Apple's latest document items-apps: or items: won't not work. You need to use
appStoreLink =
"https://itunes.apple.com/us/app/apple-store/id375380948?mt=8"
or
> SKStoreProductViewController
Example: techotopia.com/index.php/…
– GameLoading
yesterday
It's a little confusing what you are trying to do in your code but this works for opening a url
if let url = URL(string:
"your url") {
UIApplication.shared.open(url, options: [:])
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you testing on a real device?
– rmaddy
Jun 21 at 23:57