How iOS introduces local html,image,js,css files in WebView 2014-12-08 20:00:16csdn-sky_2016-views: 10292 Project requirements
Recently developed projects require a webview, and this webview will require the introduction of resources in some projects:
- A local HTML file, as a template for WebView
- Two loading pictures, occupying the picture when it is not loaded
- Jquery.js,scrollloading.js is also local, to realize the scrolling load picture function
Then we started a long Google journey.
Introducing a local HTML file in WebView
One of the main webview here is to load the loadHTMLString:baseURL: contents of the HTML file into the WebView in the form of a string and parse it.
Get the model which is a HTML file for the webviewnsstring * Htmlpath = [[NSBundle mainbundle] pathforresource:@ "index" oftype:@ "html"]; NSString * Htmlcont = [nsstring stringwithcontentsoffile:htmlpath encoding:nsutf8stringencoding error:nil];//Load the HTML file to Webview[_webview Loadhtmlstring:htmlcont Baseurl:nil];
Through the above method, it is convenient to load an HTML file into the WebView, it is very simple, next, to order advanced function!
Introducing the local image file in WebView
The implementation of this feature is largely a reference to this article: Uiwebview–loading External Images and CSS. You can go to this article, or see my continuation description below.
The implementation of this function is to undertake the above method of further expansion, the most critical is that baseurl. Look at the implementation code first:
Get the model which is a HTML file for the webviewnsstring * Htmlpath = [[NSBundle mainbundle] pathforresource:@ "index" oftype:@ "html"]; NSString * Htmlcont = [NSString stringwithcontentsoffile:htmlpath encoding:nsutf8stringencoding Error:nil]; Gets the current app's root directory nsstring *path = [[NSBundle mainbundle] bundlepath]; Nsurl *baseurl = [Nsurl Fileurlwithpath:path]; html//loaded by BaseURL can load js,css,img and other files in HTML via relative directories [_webview Loadhtmlstring:htmlcont Baseurl:baseurl];
In object-c the HTML file is loaded as above, the value of BaseURL is specified as the Bundlepath of the program, and then in the HTML file it is free to load the picture picture file by directly writing the label.
;
Note that all the resource files within the application are the root of the Bundlepath in the BaseURL root directory, so the image resources, regardless of the directory structure within the project, are directly rooted in the HTML reference.
BaseURL, what the hell is this thing? In this, I am also very curious, so I nslog the code inside the BaseURL, and then get the result is: file:///Users/(user name)/library/application%20support/iphone%20simulator/ 7.0.3/applications/(number of uppercase letters plus ligatures multibyte numbers)/(application name). app/. Then I found this directory in the terminal, open a look, found to be some html,image,txt and other static resources.
Bundlepath and its content show
At this point, the ability to insert a local image resource in WebView has been implemented, and the following are the more challenging features: adding a JS file
Add local JS file to WebView
This implementation is actually very simple, because do not need any code level of modification, just to add the image above the way, in the script src directly write JS file name can be.
But if you write this directly, you will find that the JS resource is not loaded at all. Exactly what is the difference between image and JS? Look at the above picture, you can see the default in Bundlepath is not I introduced into the project inside the Jquery.js and scrollloading.js. So, does this cause the JS resource not being loaded correctly?
In this post: How to load a local. CSS File & JavaScript Resources using the iphone UIWebView class and this article iphone based on Lightbox image amplification effects and page layouts are mentioned in a
Select. js file and in the ' Detail ' view unselect the bullseye column indicating it is compiled code
In the "Groups & Files" View expand the "Targets" tree and expand the application then go to "Copy Bundle Resources" a nd drag the *.js files into it.
method is there, but this English description, there is no attached picture, is really let people do not understand, roughly know is: JS file in Xcode, the default is a need to be compiled files, which led to it will not be placed in the bundlepath we just put The more professional name should be the bundle Resources).
So the question to solve is how to make the JS file not compiled and put into the bundle resources.
In order to understand the above two sentences in English and Google's experience is not to say, just say the result.
In Xcode, each project has at least one targets (many also, but I don't understand), in targets (the way to open targets is in the left column, click Project, in the middle of the content area, Project will appear to drink targets), the storage of some resource files, under the build phases can be seen, with this content is the largest associated with two: Compile sources and copy Bundle resources. In the case of no modification, expand compile sources can see Jquery.js and scrollloading.js for a long time.
Open Targets (XCODE5-based interface) to see two JS files after compile sources is expanded
The next thing to do is very simple, remove two JS files from compile sources, and then add these two files in the copy Bundle resources, everything is done. Want to (lazy, do not want Google to continue in-depth understanding), Compile sources is to place those who need to be compiled files,. h,.m and wronged. js files and so on, and the copy Bundle resources contains some resource files, Files that are introduced while the program is running, and that still exist after the project is packaged.
The other format of the resource files, when added is also roughly the process, do not repeat.
How iOS introduces local html,image,js,css files in WebView-sky//////////////////////////////////////zzzzzzzzzzzzzzz