I'm coding an iPhone app with a UIWebView, in Xamarin using C#.
By default embedded links within the web view open a web page in that same web view. I would instead like them to launch the linked page in a new safari browser instance.
This has been answered for objective C in X-Code but as far as I can see not for Xamarin C#
webView.LoadHtmlString(html, new NSUrl(Const.ContentDirectory, true));
Thanks in advance
Adam
You can open a web page in the brower of the device (Safari) with this command.
UIApplication.SharedApplication.OpenUrl(new NSUrl("www.google.com"));
You don't have to use UIWebView at all. If you want to open some web pages in UIWebView and some with Safari you need to implement the ShouldStartLoad delegate. Here you can determine whether to open the web page in UIWebView or rather in Safari.
private bool HandleShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
{
// you need to implement this method depending on your criteria
if (this.OpenInExternalBrowser(request))
{
// open in Safari
UIApplication.SharedApplication.OpenUrl(request.Url);
// return false so the UIWebView won't load the web page
return false;
}
// this.OpenInExternalBrowser(request) returned false -> let the UIWebView load the request
return true;
}
At last somewhere in ViewDidLoad (or other place where you initiliaze the WebView) add the following code.
webView.ShouldStartLoad = HandleShouldStartLoad;
If loading content into a UIWebView and you want to use Safari to open links, doing it the way described above will get you a blank page. You need to check the NavigationType.
private bool HandleShouldStartLoad(UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navType)
{
if (navType == UIWebViewNavigationType.LinkClicked)
{
UIApplication.SharedApplication.OpenUrl(request.Url);
return false;
}
return true;
}
The easiest/simplest way to do it is to assign a delegate to the UIWebView's ShouldStartLoad property:
webView.ShouldStartLoad = (w, urlRequest, navigationType) => {
// Open the url in Safari here.
// The urlRequest parameter is of type NSUrlRequest, you can get the URL from it.
return false; //return true for urls you actually want your web view to load.
};
Make sure the delegate returns false, for the links you want to load in Safari, so that your UIWebView does not load the link.
Related
So I am told just by using Javascript inside the Response.Write method, I can display an alert/message box on ASP.NET C#.
Using Response.Write("<script>alert('Data inserted successfully')</script>") as the template given in other answers.
When I first attempted to use it, it worked, displaying the message and redirecting me sucessfully. Since then, it has not worked, I only changed the message to display and also where the redirect URL would go after. Logically it is working as it is taking me back to the application form, it just isn't displaying the message.
Can anyone explain why it suddenly doesn't work?
Code;
public ActionResult UpdateApplication(int ApplicationId, ApplicationEdit applicationEdit)
{
if(applicationEdit.Firm == false)
try
{
applicationService.UpdateApplication(applicationEdit, ApplicationId);
return RedirectToAction("GetUser", "User", new { ApplicationId = applicationEdit.ApplicationId });
}
catch
{
return View();
}
else
{
Response.Write("<script language=javascript>alert('Sorry, You can only have 1 firm application to send off, please update the old application and untick firm option on other application first.')</script>");
return RedirectToAction("UpdateApplication", new { ApplicationId = applicationEdit.ApplicationId });
}
}
Because you're redirecting the user:
return RedirectToAction("UpdateApplication", new { ApplicationId = applicationEdit.ApplicationId });
This returns a redirect HTTP code to the browser (HTTP 307 or 308 I imagine) which tells the browser to direct the user to the specified URL. Since the browser knows it's redirecting the user, it has no reason to render any content in the response.
Either return content to be rendered in the browser or return a redirect response, but not both. If you return the view instead then you should see your <script> element in the resulting response. If you want to redirect then any message you want to display to the user should be on that next page, not in the current response.
As an aside, using Response.Write in an ASP.NET MVC application is pretty much always the wrong approach. Any client-side code should be in or referenced by the view, and you can use data on the view's model to conditionally render or not render that content.
I'm automating a windows based desktop application(C#, LeanFT).
Clicking on a button, opens a web page in the browser.
How can I verify the web page is opened?
Two ways:
Brute force
By describing the browser that was opened, which has a title, a url and other attributes, and then attaching to it.
The problem with this approach is that, if the browser was not opened, it will throw an error, so you'll have to try..catch that error
For example:
/*
* Desktop related logic that opens a browser
*/
// Use "Attach" to connect a new (or replacement) browser tab with the LeanFT test.
try {
IBrowser yourPage = BrowserFactory.Attach(new BrowserDescription
{
Title = "The title of the page",
Url = "https://thesitethatwasopened.com"
});
} catch (Exception ex) {
// the browser was not opened
}
/*
* Rest of the desktop app actions
*/
Iterating over all opened browsers
You'd still need the same description, but this way you can either get no browsers at all, which means that the page was not opened, or one or more browsers - in either case, this doesn't throw an error, so you can call it a "cleaner" way:
For example:
/*
* Desktop related logic that opens a browser
*/
// Use "GetAllOpenBrowsers" to get a collection of IBrowser instances that matches the description
IBrowser[] yourPages = BrowserFactory. GetAllOpenBrowsers(new BrowserDescription
{
Title = "The title of the page",
Url = "https://thesitethatwasopened.com"
});
/*
* Rest of the desktop app actions (maybe by making use of yourPages.Count
*/
I currenly use a webview to load a site into a native xamarin.ios app.
public override void ViewDidLoad()
{
base.ViewDidLoad();
addressTxt.Text = "192.0.0.0";
var request = new NSMutableUrlRequest(new
NSUrl("http://test/"));
request.HttpMethod = "GET";
webview.LoadRequest(request);
}
This loads the site correctly into the webview. But i need to grab a hidden field value from the page for use within the app. Is there any i can do this?
I've seen a few examples, but nothing for xamarin.ios.
Thanks
You need to evaluate JavaScript returning needed field to get value from page in UIWebView.
string result = _webViewInstance.EvaluateJavascript(yourJavaScriptCode);
also you can see this answer WKWebView evaluate JavaScript return value
In my app i'm using Web Browser Control. And in that i'm loading a web page, which contains Cookies. The reqiurement is that i should get all the cookies and then delete it from the web page.
My code,
cookies = browserControl.GetCookies();
if (cookies.Count == 0)
{
setURL();
}
else
{
foreach (Cookie cookie in cookies)
{
bool check;
// Store the value
cookie.Discard = true;
cookie.Expired = true;
}
}
ClearCookie();
And the ClearCookie function is,
private async void ClearCookie()
{
await this.browserControl.ClearCookiesAsync();
}
But the problem is that cookies are not deleted, How can i delete the cookies in the web page loaded in Web Browser Control(browsercontrol).
Thanks in advance
If your app targets Windows Phone 8, there is a new simple API for clearing cookies: ClearCookiesAsync.
The instance of WebBrowser it's called on doesn't matter.
Sample code:
await new WebBrowser().ClearCookiesAsync();
Here is a tutorial that makes use of it: http://www.developer.nokia.com/Community/Wiki/Integrate_Facebook_to_Your_Windows_Phone_Application
Using ClearCookiesAsync function is very much straightforward, what else not clear?
await browserControl.ClearCookiesAsync();
that's the code that works to clear web browser control's cookies in your question context.
UPDATE :
Using await or not is a design decision according to your needs. If next codes need to be executed only after ClearCookiesAsync completed, then you need to add await keyword. And to be able to use await the function where it resides should be declared as async, as for example :
private async void MyFunction()
Otherwise, you can just remove await keyword, and keep your function declaration as is. Check this link or search to know more about await/async feature in new .NET framework.
We're working on a wrapped WebBrowser component. We'd like to display one page (e.g. oursite.com/thispage.html) if the user is online and another page (e.g. C:\somewhere\thispage_offline.html) if the user is offline. I'm able to to properly display both pages, but my issue is detecting the online/offline status.
I tried
WebBrowser.IsOffline; however it seems that only relays the Offline Mode status, not whether or not the computer is actually able to reach the internet.
Is there a way to detect this from the WebBrowser component? Is there a way at all?
Thanks for all your help!
The simplest way to check for internet connectivity is to ping your server.
Try this code:
public static bool IsOnline() {
var pinger = new Ping();
try {
return pinger.Send("oursite.com").Status == IPStatus.Success;
} catch(SocketException) { return false; } catch(PingException) { return false; }
}
Alternatively, you can try using the WebRequest class to send a request to a simple page on your site and see whether it succeeds. This is a better option because it will also make sure that IIS (or Apache) is running on the server.
What if you just used the Ping class to see if the site is available?