1. First include the relevant code in JavaScript
$ ('. Content. Savecode '). On (' Touchstart ',function() {//Touchstart if(temp = = 0) {jump (); } if(Temp = = 1) {android.savepic (IMAGE.SRC);} }); functionJump () {//IOS varTouchstart =true; Window.location= "/touchstart";//Change URL Note: To use the "/" delimiter } functionPoststr () {//IOS returnimage.src; }
2. iOS save picture to local code
//when you have finished loading the page, take it to ImageUrl
- (void) Webviewdidfinishload: (UIWebView *) webview{_urlstring= [_mywebview stringbyevaluatingjavascriptfromstring:@"poststr ();"]; NSLog (@"%@", _urlstring);} Call Order-Shouldstart->finishload-> Callback block function
-(BOOL) WebView: (UIWebView *) WebView shouldstartloadwithrequest: (Nsurlrequest *Request Navigationtype: (uiwebviewnavigationtype) navigationtype{if([Request.mainDocumentURL.relativePath isequaltostring:@"/touchstart"]) {Uiimagefromurl ([Nsurl urlwithstring:_urlstring],^ (UIImage *image) {Uiimagewritetosavedphotosalbum (image, Self, @selector (Onsavecomplete:didfinishsavingwitherro R:contextinfo:), nil); }, ^(void) {NSLog (@"error!"); }); return false; } return true;}voidUiimagefromurl (Nsurl * URL,void(^imageblock) (UIImage * image),void(^errorblock) (void) {Dispatch_async (Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^(void) {NSData* data =[[NSData alloc] initwithcontentsofurl:url]; UIImage* Image =[[UIImage alloc] initwithdata:data]; Dispatch_async (Dispatch_get_main_queue (),^(void){ if(Image! =Nil) {Imageblock (image); } Else{errorblock (); } }); });}
Use JavaScript to invoke native APIs in iOS WebView