IOS WebView loading Local resources (pictures, files, etc.)

Source: Internet
Author: User

NSString *path = [[NSBundle mainbundle] Pathforresource:@"about. docx"Oftype:nil]; Nsurl*url =[Nsurl Fileurlwithpath:path]; NSLog (@"%@", [self mimetype:url]); //WebView loading local files, you can use the way you load data//The first argument is a nsdata, the data corresponding to the local file//The second parameter is a mimetype//The third parameter is the encoding format//relative address, generally loading local files are not used, you can find related files in the specified baseurl. //loads the files in the sandbox as binary data,NSData *data =[NSData Datawithcontentsoffile:path]; [Self.webview loaddata:data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document"Textencodingname:@"UTF-8"Baseurl:nil];
NSString *html; NSString*cachepath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) ObjectAtIndex:0]; NSString*htmlfilepath=[cachepath stringbyappendingpathcomponent:@"123.html"]; //nsstring *html=[[nsstring alloc]initwithcontentsoffile:htmlfilepath encoding:nsutf8stringencoding Error:nil]; Nsurl *baseurl=[Nsurl Fileurlwithpath:htmlfilepath]; if([[[Nsfilemanager Defaultmanager] Fileexistsatpath:htmlfilepath]) {nsstring*string=[NSString Stringwithcontentsoffile:htmlfilepath encoding:nsutf8st                Ringencoding Error:nil]; if(string) {HTML=string; }    }//nsstring *path = [[NSBundle mainbundle] bundlepath];//Nsurl *baseurl2 = [Nsurl Fileurlwithpath:path];[Self.webview loadhtmlstring:html Baseurl:baseurl];

As for the picture inside the sandbox, want to load into the web inside, found inside the simulator is normal, and then the real machine can not be loaded out

Refer to this article IOS native loading H5 pictures in GitHub source code: Https://github.com/CoderJackyHuang/iOSLoadWebViewImage

Multiple attempts, no results, find the following method can be loaded in the sandbox picture

NSData *imagedata=[nsdata Datawithcontentsoffile:imagepath];//ImagePath: Sandbox picture PathNSString *imagesource = [NSString stringWithFormat:@"data:image/jpg;base64,%@", [ImageData Base64encodedstringwithoptions:nsdatabase64encodingendlinewithlinefeed]]; NSString*strjs=[nsstring stringWithFormat:@"document.images[0].src= '%@ '", ImageSource]; [WebView evaluatejavascript:strjs Completionhandler:^(ID_nullable Response, Nserror *_nullable Error) {NSLog (@"WebView Response:%@ error:%@", response, error); }];

IOS WebView loading Local resources (pictures, files, etc.)

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.