IOS: uiwebview loadhtmlstring & Local CSS/JS/image resources

Source: Internet
Author: User

Uiwebview can load by URL or HTML string. That is, you can generate HTML string by yourself to use Web View display. A typical application of load HTML string is: the URL corresponds to too many web pages. If you only need part of the HTML content, you can get the HTML content of the URL through HTTP request, then, select only the desired part and display it by load HTML string.

Sometimes, you cannot avoid using local CSS, JS, or image (of course, you can also use URLs to link to CSS/JS/image on the Internet ).

Assume that a webpage.css and a test. js have been stored in resource folder in your iOS app, then the HTML string you generated should include them like this.

Nsstring * htmlheader = @ "<HTML> Src = 'test. js'> </SCRIPT>

Nsstring * htmlbody = @ "<p> </P> ";

Nsstring * htmlfooter = @ "</body>

Nsstring * strhtml = [[nsstring
Alloc] initwithformat: @ "% @", htmlheader, htmlbody, htmlfooter];

[Webview
Loadhtmlstring: strhtml
Baseurl: [nsurl
Fileurlwithpath: [[nsbundle
Mainbundle] resourcepath]
Isdirectory: Yes
]];

Note:

1. baseurl is your resource folder path

2. if <SCRIPT type = 'text/JavaScript 'charset = 'utf-8' src = 'test. JS '> </SCRIPT> to <SCRIPT type = 'text/JavaScript' charset = 'utf-8' src = 'test. JS '/>, you cannot load JS (ref
Link:
Http://stackoverflow.com/questions/7840127/uiwebview-loadhtmlstring-not-working-in-ios5)

3. when you create Js in IOS project or add JS, by default. by default, JS files will be compiled as code (you will see warning when building a project), so you need. JS files from "compile sources" move to "Copy bundle resources", see

Related Article

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.