• 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(); } }); }); }
  • 相关阅读:
    排序算法-总览
    MySQL插入大批量测试数据
    【剑指offer】面试的流程
    并发编程-内置锁
    并发编程-使用线程安全类
    规约先行-(二十一)设计规约
    规约先行-(二十)服务器
    [转]web.xml什么时候被加载进内存的
    DOM和BOM的理解
    代理&反向代理
  • 原文地址:https://www.cnblogs.com/davidgu/p/3951676.html
Copyright © 2020-2023  润新知