zoukankan      html  css  js  c++  java
  • iOS的WebView中使用javascript调用原生的api

    1. 首先在javascript中加入相关代码

    $('.content .saveCode').on('touchstart', function () {//touchstart
            if (temp == 0) {
                jump();
            }
            if (temp == 1) { android.savePic(image.src); }
            
        });
    
        function jump() {//IOS
            var touchstart = true;
            window.location = "/touchstart";     //改变URL  注意:要使用"/"分隔符 
        }
    
        function postStr() {//IOS
            return image.src;
        }

    2. iOS保存图片到本地代码

    // 当加载完网页取到imageurl
    - (void)webViewDidFinishLoad:(UIWebView *)webView { _urlString = [_myWebView stringByEvaluatingJavaScriptFromString:@"postStr();"]; NSLog(@"%@",_urlString); } // 调用次序 - shouldStart->FinishLoad->回调block函数
    - (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:didFinishSavingWithError:contextInfo: ), nil ); }, ^(void){ NSLog(@"error!"); }); return false; } return true; } void UIImageFromURL( 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(); } }); }); }
  • 相关阅读:
    Android开源图表图形库K线图
    交易所8种作死方式
    Android百大框架排行榜
    15类Android通用流行框架
    如何避免Scrum敏捷开发团队反思会形式化,海星法介绍
    2018年Android面试题含答案--适合中高级
    FCoin优势
    golang学习笔记16 beego orm 数据库操作
    金融系统中加密机的简介
    ESB(Enterprise Service Bus)企业服务总线介绍
  • 原文地址:https://www.cnblogs.com/davidgu/p/3951676.html
Copyright © 2011-2022 走看看