2

I have a webpage that is being viewed from a webview in a native app, this webpage has a element. The file input works fine in the web browser but does not work in the app (different problems on both iPhone and Android).

The app has already been published and I would really not like to update the native portion of the app.

The ideal solution would be to add java-script to the webpage that can detect that it is running inside the app and then launch a browser (outside of the app) with the URL.

Is this possible somehow?

MCM
  • 469
  • 1
  • 6
  • 17

3 Answers3

0

It's possible if your app overrides default user agent used by webview. By default the user agent for webview is same as that of default browser. So you don't have any way to differentiate those. If your app overrides useragent, then you can sniff useragent using javascript & take action based on that. Ideal solution will be to try to fix the problem with your html though.

Nilesh
  • 5,955
  • 3
  • 24
  • 34
-1

My understanding is that it's impossible to launch the browser which is outside of the app without changing the app.

You may add a code like below in iOS.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *address = request.URL.absoluteString;
    if( address is needed to be launched on the webbrowser ) {
        [[UIApplication sharedApplication] openURL:request.URL];
        return NO;
    }  
    return YES;
}

It's recommanded to define a protocol between the webpage and the app. Cheers.

Joey
  • 2,912
  • 2
  • 27
  • 32
  • This question is tagged Android; an Android code sample (rather than an iOS one) would be substantially more useful :) – Owen Blacker Mar 07 '18 at 11:59
-1

Launching a browser should be no problem, as posted here:

Can I launch an intent using only javascript in an Android browser?

However, detecting the app is tough. If you are already using a javascript bridge in the app, then you can make a call to the native app javascript bridge and if it succeeds, then you know you are in "your app" and can respond with the intent call above.

Failing that, user agent detection, as mentioned elsewhere, may be your best option.

In other words, you need an identifiable and unique action within the webview in order to have confidence that you are actually in it. If you did a plain vanilla implementation, then you will be forced into either having user interaction ("If you are using our app, click here" - horrible!) or updating the native app.

Community
  • 1
  • 1
Jim
  • 10,172
  • 1
  • 27
  • 36
  • The link talks about launching a specific Intent, not opening the default browser. – Owen Blacker Mar 07 '18 at 11:58
  • My response does not require the default browser - the link explains that an Intent with multiple targets on the device may already be set to a "default" which is not within the control of the app - or within the scope of the question. – Jim Mar 07 '18 at 12:06
  • Sorry, by "opening the default browser", that's precisely what I meant — for example on my phone, Android would prompt whether I wanted to open the URL in Chrome or Orfox – Owen Blacker Mar 07 '18 at 12:10
  • A webpage can specify a URI - a browser or other resource capable of handling the Intent. An app cannot override a generic Intent with a selected default, so the OP needs to determine the response. The critical part is actually determining from a webpage that the user is viewing the content in *their app* as opposed to another app or inside a browser. Once it's clear the webpage is being viewed in the app, the OP should have unique knowledge of the URI to provide for the desired outcome.. or knowledge that there is no way to perform the desired outcome without updating the app - which is the c – Jim Mar 07 '18 at 12:17