Open app or App Store item in one link [duplicate] - ios

Is there a way to check iOS to see if another app has been installed and then launched? If memory serves me this was not possible in early versions but has this been changed?

Doable, but tricky.
Launching installed apps, like the FB or Twitter apps, is done using the Custom URL Scheme. These can be used both in other apps as well as on web sites.
Here's an article about how to do this with your own app.
Seeing if the URL is there, though, can be tricky. A good example of an app that detects installed apps is Boxcar. The thing here is that Boxcar has advanced knowledge of the custom URL's. I'm fairly (99%) certain that there is a canOpenURL:, so knowing the custom scheme of the app you want to target ahead of time makes this simple to implement.
Here's a partial list of some of the more popular URL's you can check against.
There is a way to find out the custom app URL : https://www.amerhukic.com/finding-the-custom-url-scheme-of-an-ios-app
But if you want to scan for apps and deduce their URL's, it can't be done on a non-JB device.
Here's a blog post talking about how the folks at Bump handled the problem.

There is a script like the following.
<script type="text/javascript">
function startMyApp()
{
document.location = 'yourAppScheme://';
setTimeout( function()
{
if( confirm( 'You do not seem to have Your App installed, do you want to go download it now?'))
{
document.location = 'http://itunes.apple.com/us/app/yourAppId';
}
}, 300);
}
</script>
Calling this script from the web (Try to start MyApp), you can determine if your app with scheme "yourAppScheme" is installed on the device or not.
The App will launch if it is installed on the device and "yourAppScheme" is registered in it.
If the app is not installed you can suggest the user to install this app from iTunes.

To check if an app is installed (e.g. Clear):
BOOL installed = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"clearapp://"]];
To open that app:
BOOL success = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"clearapp://"]];

Hides the error message if the app is not installed
At Branch we use a form of the code below--note that the iframe works on more browsers. Simply substitute in your app's URI and your App Store link.
<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
window.onload = function() {
// Deep link to your app goes here
document.getElementById("l").src = "my_app://";
setTimeout(function() {
// Link to the App Store should go here -- only fires if deep link fails
window.location = "https://itunes.apple.com/us/app/my.app/id123456789?ls=1&mt=8";
}, 500);
};
</script>
<iframe id="l" width="1" height="1" style="visibility:hidden"></iframe>
</body>
</html>
There's a second possibility that relies on cookies first and the javascript redirect only as a fallback. Here's the logic:
When a user without the app first taps on a link to your app, he or she is redirected straight to the App Store. This is accomplished by a link to your app actually being a dynamically-generated page on your servers with the redirect. You create a cookie and log a "digital fingerprint" of IP address, OS, OS version, etc. on your backend.
When the user installs the app and opens it, you collect and send another "digital fingerprint" to your backend. Now your backend knows the link is installed On any subsequent visits to links associated with your app, your servers make sure that the dynamically-generated redirect page leads to the app, not the App Store, based on the cookie sent up with the request.
This avoids the ugly redirect but involves a ton more work.

To my understanding, because of privacy issues, you can't see if an app is installed on the device. The way around this is to try and launch the app and if it doesn't launch to have the user hit the fall back url. To prevent the mobile safari error from occurring I found that placing it in an iframe helps resolve the issue.
Here's a snippet of code that I used.
<form name="mobileForm" action="mobile_landing.php" method="post">
<input type="hidden" name="url" value="<?=$web_client_url?>">
<input type="hidden" name="mobile_app" value="<?=$mobile_app?>">
<input type="hidden" name="device_os" value="<?=$device_os?>">
</form>
<script type="text/javascript">
var device_os = '<? echo $device_os; ?>';
if (device_os == 'ios'){
var now = new Date().valueOf();
setTimeout(function () {
if (new Date().valueOf() - now > 100)
return;
document.forms[0].submit(); }, 5);
var redirect = function (location) {
var iframe = document.createElement('iframe');
iframe.setAttribute('src', location);
iframe.setAttribute('width', '1px');
iframe.setAttribute('height', '1px');
iframe.setAttribute('position', 'absolute');
iframe.setAttribute('top', '0');
iframe.setAttribute('left', '0');
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe = null;
};
setTimeout(function(){
window.close()
}, 150 );
redirect("AppScheme");

I struggled with this recently, and here is the solution I came up with. Notice that there is still no surefire way to detect whether the app launched or not.
I serve a page from my server which redirects to an iPhone-specific variant upon detecting the User-Agent. Links to that page can only be shared via email / SMS or Facebook.
The page renders a minimal version of the referenced document, but then automatically tries to open the app as soon as it loads, using a hidden <iframe> (AJAX always fails in this situation -- you can't use jQuery or XMLHttpRequest for this).
If the URL scheme is registered, the app will open and the user will be able to do everything they need. Either way, the page displays a message like this at the bottom: "Did the app launch? If not, you probably haven't installed it yet .... " with a link to the store.

Related

Launching an App from Custom URL from Safari not working as intended in iOS >12.3

We share the App deeplinks (Universal links) with our users over email, sometimes they get wrapped by email service providers for safety.
When user taps on these wrapped deeplinks, instead of opening the App directly it opens the url in Safari.
We have a page hosted on that url. We capture the deeplink there and try to open the App using Custom URL scheme (myurlscheme://). But if the App is not installed, we try to redirect the user to the App Store page.
It all worked okay until now, but seems like Apple made some changes in Safari in the new versions of iOS (>12.3).
What’s happening now is, if the App is installed and we open the App from Safari (from Custom URL), the App Store page opens in a split second after opening our App.
This is the Javascript code that we are using:
window.location.href = 'myurlscheme://';
setTimeout(function() {
window.location.href = "https://itunes.apple.com/us/app/myapp/id123456789?ls=1&mt=8";
}, 500);
Is anyone else experiencing this. If yes, were you able to find any solution?
Update:
If we set the timeout to 4000 (i.e. 4 seconds), then it does not redirect to the App Store after launching the App.

create a phone Gap app by just passing the url of website

I have created a phone gap app by just passing the url of my developed website into my PhoneGap project but in the app addressbar is coming want to remove that
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.open('http://hitchmeright.com', '_self ', 'location=yes');
}
</script>
Your app is likely opening the url in the InAppBrowser. From the InAppBrowser docs:
_self: Opens in the Cordova WebView if the URL is in the white list, otherwise it opens in the InAppBrowser.
So you need to whitelist your url.
Now that said, this is not a good design for a PhoneGap application, and will likely provide a terrible user experience. You're treating PhoneGap as a browser rather than a hybrid app. You should build your PhoneGap app properly (package your web assets and load them locally), and only load data from the server as needed. I'd recommend doing a bit more research and checking out some example apps.

How to define url schemes starting with http ios7

I can define custom schemes like myapp so that third applications can redirect links like: myapp://mypage.com to my app(if user installed it). but I want that third applications open my app if user try to open links like http://mysite/mypage.com too.
Right now we can see that safari open yourtube when we type links like:
http://www.youtube.com/watch?v=WZH30T99MaM
Or map application opens if we type links like:
http://maps.google.com/maps.....
So how can I define a custom scheme that third applications open my apps if user type:
http://a.myapp.com
Short answer: You can't without server support. Apple does tricks that are not available to third party apps to redirect HTTP URLs like Maps and Youtube.
The only way you could do this would be to set up a web server at http://a.myapp.com that redirected to myapp://
Possible workaround, you register your custom URL Scheme and then in your HTML/JS code of the start page of your site you check if the the browser agent is Mobile Safari and forward it to the URL with custom scheme.
You can also check if app is not installed and redirect to AppStore, simply by opening AppStore link with the timeout, so if the redirection attempt to custom URL Scheme link fails you go to to App Store.
<script type="text/javascript">
var app = {
isSafariMobile: function () {
return navigator.userAgent.match(/(iPod|iPhone|iPad)/) && navigator.userAgent.match(/AppleWebKit/)
},
launchApp: function() {
window.location.replace("myapp://");
this.timer = setTimeout(this.openAppStore, 1000);
},
openAppStore: function() {
window.location.replace("https://itunes.apple.com/app/Myapp");
}
};
if (app.isSafariMobile()){
app.launchApp();
}
</script>
UPDATE: The Safari detection method may be slightly adjusted, the ios chrome app could also be detected as Safari by this code as it has WebKit in its UserAgent string on iPhone.

How to make deferred deep linking?

How to make deferred deep linking and generate unique mobile signature. I try using IP Address, screen-size, OS version, device name but still not get succeed.
The comment links to a great answer, certainly. High level, here are the steps:
Your links should point to a page on your site that collects a digital fingerprint
That page should collect, at minimum, IP address, OS, OS version and screen size (width and height). Should send to your server and place in a persistent store. Redis works great for this because of its fast lookup times. Also record some sort of unique identifier for which link was clicked (that could be the value in redis).
Then redirect to the app (URI scheme) and have a fallback to the App Store/Play Store. Here's an example for iOS. The beauty of the iframe is that it kills the alertView if the app is not installed. This should be placed in the body:
<script type="text/javascript">
window.onload = function() {
// Deep link to your app goes here
document.getElementById("l").src = "my_app://";
setTimeout(function() {
// Link to the App Store should go here -- only fires if deep link fails
window.location = "https://itunes.apple.com/us/app/my.app/id123456789?ls=1&mt=8";
}, 500);
};
</script>
<iframe id="l" width="1" height="1" style="visibility:hidden"></iframe>
When a user opens your app, send up the same combination of params to your servers and search your persistent store to see if this device recently clicked on a link. Send a response down to your app (e.g. { link_id: "1234" } or { link_id: -1 }) Your app logic should then respond based on which link was clicked.
Hopefully this makes sense. We do this at Branch and can assure you that it's harder than it looks to roll this solution from scratch. There are a ton of edge cases introduced by individual browsers and even individual apps (e.g. when links are shared to Twitter and clicked on in the native Android app). But at it's core fingerprinting is relatively simple. Hopefully the above was helpful.

Test if any app can handle a url scheme in mobile safari on iOS

Is there any way to test if an iOS can handle a custom URL scheme? I have an app that registered a custom url scheme to be able to open the app from a hyperlink in mobile safari. How ever, I'd like to tell the user they need to go to the appstore to download the app if they dont have it installed.
Is there a clever way to test a URL and catch when it fails and the reason for it to fail?
This is the best I can come up with, but it's only tested in iOS 5:
If your link in mobile safari is link text,
change it to: link text,
then at the top of the /launchapp page, put a hidden iframe with the desired URL: <iframe src="myapp://path" style="display:none"></iframe>. In the body of the page, put your message about needing to go to the appstore to download the app.
If the user has the app:
They will not see the launchapp page, they will be directed seemlessly to the app url.
If the user does not have the app:
They will get a nasty alert about 'The URL could not be loaded', click OK, then they will be looking at your 'you need to download the app' page.
Update - March 2013
Check out this comment on a related SO answer. Apparently, if your app isn't already installed, you can seamlessly redirect to your app in the App Store using an approach like this (not tested):
// setup fallback (redirect to App Store)
var start = (new Date()).valueOf();
setTimeout(function() {
var end = (new Date()).valueOf();
// prevent App Store redirect upon returning to safari from myapp
if (end - start > 1000) return;
// get a seamless redirect if you use itms://... instead of http://itunes.com/...
window.location = 'itms://my/app/URI';
}, 25);
// Attempt to load my custom url scheme
window.location = 'myapp://my/path'

Resources