Load a Web Page

PDF for offline use
Sample Code:
Related SDKs:

Let us know how you feel about this

Translation Quality


0/250

This recipe shows how to load a web page in a UIWebView control.

Recipe

To show a secure web page:

  1. Create a UIWebView and add it to a view:
webView = new UIWebView (View.Bounds);
View.AddSubview(webView);
  1. Load a URL using NSUrl and NSUrlRequest classes:
var url = "https://xamarin.com"; // NOTE: https secure request
webView.LoadRequest(new NSUrlRequest(new NSUrl(url)));

iOS 9 App Transport Security

The example code above uses an HTTPS URL. If the code was changed to the following:

var url = "http://xamarin.com"; // NOTE: http insecure request

Then the following error would appear (on iOS 9 and above) in the Application Output (if debugging) and the web view would remain blank:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.
Temporary exceptions can be configured via your app's Info.plist file.

To get around this issue you must use secure URLs or learn how to configure exceptions to App Transport Security (ATS) in the Info.plist file.

iOS 8 and earlier versions of iOS do not implement ATS. HTTP URLs will continue to work on earlier versions of iOS, and the Info.plist changes made for iOS 9 will be ignored.

Additional Information

By default the web page will open at its normal resolution. To scale it down so that the entire page width is visible set the ScalesPagesToFit property.

webView.ScalesPageToFit = true;

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.