Rails: redirect_to 'myapp://' to call iOS app from mobile safari
Turns out there's a simple solution that is enough for the context of my app as of the moment. I just needed to handle the redirect in javascript. Other solutions are still welcome. :)
<html><head>
<script type="text/javascript">
var userAgent = window.navigator.userAgent;
if (userAgent.match(/iPad/i) || userAgent.match(/iPhone/i)) {
window.location = "myiosapp://"
}
</script>
</head>
<body>
Some html page
</body>
</html>
Rails redirect to iOS custom url
custom://
is not a URI. You must have something after that or otherwise it's not saying to go anywhere. Try going to http://
. That work? No, it's not a URI. Same principle here.
EDIT:
Apple does some strange stuff so you have to do something with JavaScript. That is not actually a valid URI, Safari just detects it and launches the app.
Redirect to application if installed, otherwise to App Store
There is no way to check for this.
However, there is a nice workaround.
The idea is basically this:
- The first time you open your app, you open up mobile safari from within your app to a predefined URL on your server
- On that URL you set up a cookie, like appInstalled to the users mobile safari
- You then kick the user back to your app with your registered scheme (same as FB does with SSO)
- All your email links point to your website, but on the website you check if the browser is mobile Safari and if the appInstalled cookie exists
- If either the browser is not mobile Safari or the cookie is not found, you redirect to the AppStore, or stay in your webpage.
- If the conditions of #4 are true, you redirect the user to your app with the registered scheme
- If the app has been deleted by the user, so the custom url scheme fails, you have a fail-safe redirect to the appstore
The 2 last steps are explained on this SO post
How to link to apps on the app store
Edited on 2016-02-02
Starting from iOS 6 SKStoreProductViewController class was introduced. You can link an app without leaving your app. Code snippet in Swift 3.x/2.x and Objective-C is here.
A SKStoreProductViewController object presents a store that allows the
user to purchase other media from the App Store. For example, your app
might display the store to allow the user to purchase another app.
From News and Announcement For Apple Developers.
Drive Customers Directly to Your App
on the App Store with iTunes Links
With iTunes links you can provide your
customers with an easy way to access
your apps on the App Store directly
from your website or marketing
campaigns. Creating an iTunes link is
simple and can be made to direct
customers to either a single app, all
your apps, or to a specific app with
your company name specified.To send customers to a specific
application:
http://itunes.com/apps/appnameTo send
customers to a list of apps you have
on the App Store:
http://itunes.com/apps/developernameTo send customers to a specific app
with your company name included in the
URL:
http://itunes.com/apps/developername/appname
Additional notes:
You can replace http://
with itms://
or itms-apps://
to avoid redirects.
Please note that itms://
will send the user to the iTunes store and itms-apps://
with send them to the App Store!
For info on naming, see Apple QA1633:
https://developer.apple.com/library/content/qa/qa1633/_index.html.
Edit (as of January 2015):
itunes.com/apps links should be updated to appstore.com/apps. See QA1633 above, which has been updated. A new QA1629 suggests these steps and code for launching the store from an app:
- Launch iTunes on your computer.
- Search for the item you want to link to.
- Right-click or control-click on the item's name in iTunes, then choose "Copy iTunes Store URL" from the pop-up menu.
- In your application, create an
NSURL
object with the copied iTunes URL, then pass this object toUIApplication
' sopenURL
: method to open your item in the App Store.
Sample code:
NSString *iTunesLink = @"itms://itunes.apple.com/app/apple-store/id375380948?mt=8";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
iOS10+:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink] options:@{} completionHandler:nil];
Swift 4.2
let urlStr = "itms-apps://itunes.apple.com/app/apple-store/id375380948?mt=8"
if #available(iOS 10.0, *) {
UIApplication.shared.open(URL(string: urlStr)!, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(URL(string: urlStr)!)
}
Related Topics
Nsindexpath? Does Not Have a Member Name 'Row' Error in Swift
Uialertcontroller Set Attributed Messages
iOS Google Sdk Map Cannot Create Dotted Polylines
Map Url Parameters to Objects Using Restkit
Binding Viewmodel and Textfields with Swiftui
Uitextfield Attributedplaceholder Has No Effect
Store Time Efficiently in Firebase Database
Unrecognized Error in Mapview (Ios)
Having Uiview Drawrect Occur in a Background Thread
Custom Uitableviewcell with Progress Bar Download Update
iOS Appextension:How to Combine Nsextensionactivationrule and Nspredicate
Module Compiled with Swift 5.0.1 Cannot Be Imported by the Swift 5.1 Compiler
Why Self.Locationmanager Stopupdatinglocation Doesn't Stop Location Update
How to Send a Uilongpressgesture Programmatically
Fit Image of Random Size into a Uiwebview (Ios)
How to Reload Tableview of Another Uiviewcontroller in Current Viewcontroller