js 与 ios Android交互

一.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";

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。