Load a Web Page
- PDF for offline use
- Sample Code:
- Related SDKs:
Let us know how you feel about this
This recipe shows how to load a web page in a UIWebView control.
To show a secure web page:
- Create a
UIWebViewand add it to a view:
webView = new UIWebView (View.Bounds); View.AddSubview(webView);
- Load a URL using
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.
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;