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

js 与 ios Android交互

时间:2015-04-13 14:21:53      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

一.android 交互

1.js调用webview

在android API Level 17及以上的版本中,就会出现js调用不了android的代码,这是版本兼容的问题,需要在调用的方法上面加一个注解:@JavascriptInterface,这个注解需要导入一个包:import android.webkit.JavascriptInterface;

public void onCreate(Bundle savedInstanceState) {
    //给js设置调用的方法
    this.appView.addJavascriptInterface(new JavaScripdtObject(), "android");//这个android是注入对象
}
/**
* 供js调用的对象方法
* */
public class JavaScripdtObject {

    @JavascriptInterface//加入这个注解
    public void runAndroidMethod(String str) {
        Toast.makeText(MainActivity.this, "被js调用"+str, Toast.LENGTH_SHORT).show();
} }

js 代码

<script>
    window.android.runAndroidMethod(str);
</script>

2. android调用 js 这个简单

this.appView.getSettings().setJavaScriptEnabled(true);
this.appView.loadUrl("javascript:view.test()");

二. js 调用ios

- (void)addShowTypeView:(NSString *)nibName{
    
    UIApplication *app = [UIApplication sharedApplication];
    AppDelegate *delegeat = app.delegate;
    self.creativeView = [[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil][0];
    __weak __typeof__(self)self_weak = self;
    
    //返回
    self.creativeView.backBtnBlock = ^(UIView *sideView){
        [self_weak backBtnClickMainView:self_weak.webView View:sideView];
    };
    self.creativeView.frame = CGRectMake(kScreenWidth, 0, kScreenWidth/2, kScreenHeight);
    [delegeat.window addSubview:_creativeView];
    [self TranslationViewAnimationMainView:self.webView rightView:self.creativeView];
}

#pragma mark - UIWebView代理
/**
 *  网页的JS动态调用OC的方法!!!
 */
// 此代理方法与加载页面有关系
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    NSString *urlString = [[request URL] absoluteString];
    DLog(@"url = %@",urlString);

    NSArray *urlComps = [urlString componentsSeparatedByString:@"://"];
    if([urlComps count] && [[urlComps objectAtIndex:0] isEqualToString:@"objc"])
        
    {
        if (urlComps.count > 1) {
            
            if ([urlComps[1] hasPrefix:@"showTypeView"]) {
                
                NSArray *arr = [urlComps[1] componentsSeparatedByString:@"&"];
                if (arr.count > 0) {
                    SEL sel = NSSelectorFromString(arr[0]);
                    if ([self respondsToSelector:sel]) {
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Warc-performSelector-leaks"
                        [self performSelector:sel withObject:((arr.count>1)?arr[1]:nil)];
# pragma clang diagnostic pop

                    }
            }
                
            }
            else if ([urlComps[1] hasPrefix:@"showshareView"]) {
                NSArray *arr = [urlComps[1] componentsSeparatedByString:@"&"];
                if (arr.count > 0) {
                    SEL sel = NSSelectorFromString(arr[0]);
                    if ([self respondsToSelector:sel]) {
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Warc-performSelector-leaks"
                        [self performSelector:sel withObject:((arr.count>1)?arr[1]:nil)];
# pragma clang diagnostic pop

                    }
                }

            }
        }
    }
        
    return YES;
}

js 代码

document.location="objc://showTypeView:&1";

 

js 与 ios Android交互

标签:

原文地址:http://www.cnblogs.com/turnswing/p/4421993.html

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