I am trying to load a URL in react native webview which needs credentials. Normally when we open this URL in a browser like Safari and Chrome, it give us a popup to enter the credentials. Not sure how to handle it in React-Native?
We require LinkedIn authentication for our React Native application, we couldn't find a way to work with the WebView component.
This may very well be possible, however, I will highlight what solution we came up with. These steps are for an iOS React Native app, steps will differ for Android but the concept is the same. This also does require some server configuration.
We created a URL Type. In XCode, select your project and click on the Info tab. Scroll to the bottom to see URL Types. Fill in the identifier and URL Schemes. Remember what you've set for the URL Schemes. For this example, we will use myscheme.
When the app opens, we use Linking to open up the URL in Safari.
Linking.openURL('https://foo.bar');
The User could then log in via the website, once logged in, you can redirect your User back to.
myscheme://name=Dan
The myscheme matching the URL Scheme you entered within Xcode. As long as you have your application installed and the scheme matches then your app will open.
You can add any payload of information after ://.
In your React application, you can register
componentWillMount() {
Linking.addEventListener('url', this.handleOpenURL);
}
componentWillUnmount() {
Linking.removeEventListener('url', this.handleOpenURL);
}
handleOpenURL = (event) => {
const { url } = event;
if (url.startsWith('myscheme://')) {
// do something with your data.
}
};
Related
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.
I added a url scheme newConversation to my info.plist so that when a user clicks on a link in a browser/email it will redirect him to the app. This works perfectly fine.
I was wondering how I can open the app to a specific view controller when clicking this url?
I tried using:
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
if ([url.scheme isEqualToString:#"newConversation"]) {
NSLog(#"url schemeeeee");
emailLink = YES;
[self initWindow];
}
return YES;
}
but not getting anywhere with that.
Thanks!
Your app should only have one scheme (such as myappscheme) registered for your app. Then if you need to handle different actions, you provide more specific URLs with that scheme:
myappscheme:///newConversation
myappscheme:///doSomethingElse
Then you get /newConversation for the URL's path. Then your code becomes:
if ([url.path isEqualToString:#"/newConversation"]) {
} else if ([url.path isEqualToString:#"/doSomethingElse"]) {
}
To be honest, this is kind of a pain to implement on your own. A basic custom URL scheme link isn't an ideal solution and has a ton of nasty edge cases, most notably the 'Cannot Open Page" error users will see before if they don't have your app installed, and the fact that many apps actually don't recognize custom scheme URLs as 'clickable' (they just show up as regular text).
A somewhat better approach is to use a regular http:// link, and then redirect the visitor to your app — if they have it installed — or to a fallback URL/App Store page. Until iOS 9, a reasonable basic implementation was a JavaScript redirect like this:
setTimeout(function() {
window.location = "https://itunes.apple.com/path/to/your/app/";
}, 25);
// If "yourapp://" is registered, the user will see a dialog
// asking if want to open your app. If they agree, your app will
// launch immediately and the timer won't fire.
// If not installed, you'll get an ugly "Cannot Open Page"
// dialogue and the App Store will launch when the timer expires.
window.location = "yourapp://";
Unfortunately this would still show a 'Cannot Open Page' error, but until recently it was possible to get around this in a reasonably user-friendly way by using a more nuanced version of this script. Sadly, Apple intentionally broke that with the iOS 9.2 update, so custom URL schemes are actually pretty much useless for deep linking now, unless you are certain the app is already installed on that device.
The best solution is a combination of custom URL scheme links (with intelligent JavaScript redirections) and Apple's new Universal Links. Universal Links let you use a normal http:// URL to a page on your website (the page could be a simple redirection to the App Store without the custom URL trigger that causes the 'Cannot Open Page' error), which is intercepted by your phone and sent directly into your app if installed. Unfortunately Universal Links only work in iOS 9+, and don't work yet when opened inside a lot of apps.
This is quite a lot to handle, so the best option might be a free service like Branch.io (full disclosure: I work with the team) to take care of all the technical aspects.
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.
I am trying to implement Facebook App Links using the Mobile Hosted API. Everything goes smoothly but when I test the App Link URL the app doesn't open even if installed and the URL redirects to the App Store. The custom URL for my app is set properly as when I type the custom scheme inside Safari it does open the app. It seems that something is off but can't tell why.
Here is some data:
The url that I test in the browser is:
http://fb.me/780961121977733
This is the registered data with the Mobile Hosted API:
{
id = 780961121977733;
ios =(
{
"app_name" = GoPhrazy;
"app_store_id" = 903559056;
url = "gophrazy://playerPuzzle/leo3/1420663071896";
}
);
}
The custom url scheme is registered in the info.plist as:
gophrazy://
I thought maybe the app_name case would affect it but I tested that to all lower with no effect.
Anyone has any tips on this?
Thanks
Url scheme in the info.plist MUST be always defined without leading ://
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'