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:
Click here to go the app
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
Related
I'm been using the WebView in my project to show a couple of webpages in app.
On Android this works fine. but on iOS it's open the default browser insted of showing it in app, like when you use
Device.OpenUri(new Uri(e.Url))
My code look like this
webView = new WebView
{
Source = new UrlWebViewSource
{
Url = "http://www.google.com",
},
VerticalOptions = LayoutOptions.FillAndExpand
};
this.Content = webView ;
Any one here who know how to make the iOS open the page in-app ?
Using Device.OpenUri will often even according to the Xamarin documentation here, navigate outside the application.
If you want to host the content within your application, the best approach would be to embed the Webview, that it looks like your doing, and set the .Source to a url.
I don't understand why your using Device.OpenUri if you have a WebView instance already on your page.
Just set the .Source property of the WebView to point at some url.
For example:-
WebView objMyWebView = new WebView();
objMyWebView.Source = "http://www.google.com";
I am trying to open en external url from a local html file. For example, i am using super.loadUrl("file:///android_asset/www/index.html"); to call local file and the application opens it in the browser provided by DroidGap. However, there is a problem with the external url. In index.html, i am trying to reach http://www.google.com via an image button and the device opens another browser to show www.google.com (in my device, chrome is opening to show the page www.google.com). I want to let DroidGap browser to open both external and local urls in DroidGap browser.
I have achieved this because I needed the same solution. I assume you already know how to load the local file but just in case ...
//By "SomeLocalFile.html" I mean an html file in the /www/ folder of the actual app installation **not** on the server
var ref = window.open('somelocalfile.html', '_blank', 'location=no');
First Add and event listener like this to the main js of your app from which you are calling var ref = window.open().
ref.addEventListener('loadstart', LoadStart); // in YourMainJs.js
Then create a local file called closeInAppBrowser.html some where in your /www/ folder it just so happens that mines is in /www/pages/
Now define the LoadStart function so that when the page starts to load LoadStart() will fire
//I use match(/[^/]+$/g) because I find that matching the file name is just easier than the path.
// This code is place in in YourMainJs.js
fileName = event.url.match(/[^/]+$/g);
if(fileName[0] == "closeInAppBrowser.html"){
// alert("fun load stop runs");
ref.close();
}
Now in the SomeLocalFile.html that you are going to use window.open() with, place a link and js like this.
// in SomeLocalFile.html that was called via the window.open()
<a class="close">Close This Window Like A BOSS!</a>
$('.close').click(function(){
//We use window location cause you can't navigate to any other local file just by using an href in an <a> tag
//We aslo use ../../ because that is the correct path usage from within InAppBrowser to another local file
window.location = '../../pages/closeInAppBrowser.html';
});
Now when you click the link it will attempt to navigate to closeInAppBrowser.html this will trigger LoadStart() and it will check if the event.url file name matches "closeInAppBrowser.html" and if so it will close the window.
I have tested this and it works 100%. Let me know if you have any other questions
Actually there is no need to use child browser plugin, if you upgrade phonegap to 2.3.0 or above, so that you can use InAppBrowser
sample
var ref = window.open(authorize_url, '_blank', 'location=no');
ref.addEventListener('loadstart', function() { me.locChanged(event.url,ref); });
Now i try to Implement Twitter Integration in android using twitter4j library.Few months back I successfully implemented this using a dummy call back url like "PicPuzzle://tkxel".
But now Twitter changes its scheme and now not allow to create application with dummy URL.
Now the problem is after authentication It redirect to callbackurl But not return to application.
I specify the same url in application and registration page. I refered Problem in Callback in Twitter in Android and How I can call back in Android using OAuth for Twitter?. But these solutions are now not working because now twitter not allow to create application with dummy URL. Please help me to solve this problem.
Follow below steps:
Go to Twitter Apps: https://apps.twitter.com/
Then, Go to your app.
Go to Settings Tab
In section "Callback URLs", add the below line:
twittersdk://
Click button "Update Settings" at the bottom. That's it.
Hope this works!
Create your own WebViewClient subclass.
Override shouldOverrideUrlLoading(WebView view, String url) method.
In shouldOverrideUrlLoading(), check if url.startsWith(YOUR_CALLBACK_URL) is true.
If true, retrieve "oauth_verifier" parameter from the URL and return true (true from shouldOverrideUrlLoading() prevents the WebView instance from loading the URL).
Get an access token using the value of the parameter obtained in the step 4.
Your application can get control back from the WebView instance after the step 4.
If the above steps sound cumbersome, try TwitterOAuthView. Its usage is very simple. Just call
view.start(CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL, true, listener);
and receive the result via TwitterOAuthView.Listener interface defined as below.
void onSuccess(TwitterOAuthView view, AccessToken accessToken);
void onFailure(TwitterOAuthView view, TwitterOAuthView.Result result);
If true is given to TwitterOAuthView.start() method as the forth argument, TwitterOAuthView does not access the callback URL, and I think this behavior is what you want to implement. The source code, TwitterOAuthView.java, may be of help.
I want to open a url(ex. www.google.com) in jquery mobile android app.
I want to open it with in the context of the app itself. i dont want to open a diffrent browser to launch it on the click of the link or button. i want to open it with in the app.
is it possible.
Thanks.
Found the solution here:
function openInWebView(url)
{
var anchor = document.createElement('a');
anchor.setAttribute('href', url);
var dispatch = document.createEvent('HTMLEvents')
dispatch.initEvent('click', true, true);
anchor.dispatchEvent(dispatch);
}
Then call the function like this in your app:
openInWebView('http://google.com')
I take the response from an HTTP connection in the form of string and show that to webview like this:
WebView engine = (WebView)findViewById(R.id.webview);
engine.loadData(endResult, "text/html", "UTF-8"); /*endresult is string*/
I actually get a response that contains the google page (google search result direct from google.com).
The loadData method works well i.e it shows the web page but when I click on one of the links on that page it shows "page not available" and said that "xyz link might be temporarily down or it may have moved to permanently to a new web address".
this happens for all links accept the first present link on that page. i.e it shows correct page from first link on that page but fails for others..
I noticed that OSes prior to 2.3 failed to follow links if setHorizontalScrollBarEnabled and setVerticalScrollBarEnabled are set to false.
try to use loadDataWithBaseURL of the WebView class
I would avoid using engine.loadData - it seems to cause all sorts of crazy problems.
Use engine.loadDataWithBaseURL instead, and pass the base URL of where the content exists. I would think that the content you are loading is using relative paths in it's HTML so it's looking inside your app resources. By specifying the base URL you get around this problem.