简体   繁体   中英

Android and custom URL scheme

I'm working on an app with a custom url scheme. It is opening some webpage for authentication in a chrome tab. This is done in xamarin.forms like this:

Browser.OpenAsync(apiUrl + "mobile", new BrowserLaunchOptions
            {
                LaunchMode = BrowserLaunchMode.SystemPreferred,
                TitleMode = BrowserTitleMode.Hide,
            });

everything work as expected if I return a webpage with a link and click the the link manually:

<a href="myscheme://myhost/myparameters">Click here to go the app</a>

But if i return a 302 redirect to the same url it will not close the chrome tab and dont focus the app again. If i add a javascript in the response, it will not automatically open the url (close the chrome tab and focus the app) I've tried things like this:

window.location = url;
window.open(url,'_self');
setTimeout(()=>window.open(url,'_self'),10);

(url is a valid variable, even tried alert(url) after changing the location and it show the correct url.

Why does it only work when I click the link manually?

In order to maintain the user's security and experience, the browser prohibits the direct use of window.open(URL) in JS to open new links.

Try to change like below:

setTimeout(()=>window.open(url,'_self'),500); //The delay time must not be too short or you will be intercepted

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM