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.)