Diego Vieira
Diego Vieira

Reputation: 199

Open page in WebView by clicking on an external link

I have a website that has a web version and a webview. It is necessary to open directly in the app (if installed) the link to recover password that the user receives in his email.

After adding the code below to AndroidManifest.xml, I was able to open the app by clicking on the link.

 <intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:host="*" android:scheme="myapp"/>
 </intent-filter>

Link I created to test:

<a href="myapp://www.site.com/password-reset/token/abc123">Click</a>

However, it always opens the home page in the app, instead of opening the specific page in the link.

Do I need to make any further modifications to the app to interpret this type of link?

MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    webView = findViewById(R.id.webview);
    webView.loadUrl("https://www.site.app");

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView wv, String url) {
            
        }

        @Override
        public void onPageFinished(WebView view, String url) {

        }
   });
}

SOLUTION:

    Intent intent = getIntent();
    Uri data = intent.getData();
    
    if (data != null && data.toString().startsWith("myapp:")) {
        urlIntent = data.toString().replace("myapp", "https");
    } else {
        urlIntent = "https://www.site.app";
    }

    webView.loadUrl(urlIntent);

Upvotes: 0

Views: 515

Answers (1)

nuhkoca
nuhkoca

Reputation: 1943

Deeplinks normally just triggers the app to be opened, they don't help you navigate or open a specific location unless you define Fragment or Activity based intent-filters or manually handle.

You should get the data which includes URL from intent first then give it to WebView as a URL. You can do that inside either onNewIntent or onCreate methods like so;

override fun onNewIntent(intent: Intent){
    super.onNewIntent(intent)

    val uri = intent.data
}

then load it in WebView like;

webview.loadUrl(uri.toString())

Upvotes: 1

Related Questions