码迷,mamicode.com
首页 > 移动开发 > 详细

iOS的WebView中使用javascript调用原生的api

时间:2014-09-02 17:43:55      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   os   io   使用   java   ar   

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(); } }); }); }

 

iOS的WebView中使用javascript调用原生的api

标签:android   style   blog   color   os   io   使用   java   ar   

原文地址:http://www.cnblogs.com/davidgu/p/3951676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!