Three ways to reference iOS development WebView

Source: Internet
Author: User

1 simplest and most basic

Nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];

Nsurlrequest *request = [Nsurlrequest Requestwithurl:url];

[Self.webview Loadrequest:request];

2 Refer to Import project files remember to put the corresponding JS CSS file

NSString * Htmlpath = [[NSBundle mainbundle] pathforresource:@ "Mall" oftype:@ "html"];

NSString * Htmlcont = [NSString stringwithcontentsoffile:htmlpath encoding:nsutf8stringencoding Error:nil]; Get the current app's root directory

NSString *path = [[NSBundle mainbundle] bundlepath];

Nsurl *baseurl = [Nsurl Fileurlwithpath:path]; HTML that is loaded by means of BaseURL

Files such as js,css,img can be loaded in HTML in relative directories

[Self.webview Loadhtmlstring:htmlcont Baseurl:baseurl];

3 referencing the files in the sandbox (files need to be downloaded to the sandbox, and subsequent collation of the relevant code)

Nsarray *paths = Nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask,yes);

NSString *docdir = [Paths objectatindex:0];

NSString *filepath = [Docdir stringbyappendingpathcomponent:@ "mall.html"];

Nsurl *baseurl = [Nsurl Urlwithstring:filepath];

NSString *htmlstring=[[nsstring Alloc]initwithcontentsoffile:filepath encoding:nsutf8stringencoding Error:nil];

[Self.webview loadhtmlstring:htmlstring Baseurl:baseurl];

Three ways to refer to iOS development WebView

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.