美文网首页
普元客户端与js交互笔记

普元客户端与js交互笔记

作者: 红凉梦 | 来源:发表于2020-01-14 13:55 被阅读0次

    Utils 静态工具类

    js端

    var r = Utils.xxxxx('index.html');//index.html是参数,r是返回值,没有可以不写
    

    oc端

    #import "SkyBase.h"
    //创建类要继承SkyBase
    @interface Skyxxxx : SkyBase
    @end
    
    //.m中实现方法--举例
    +(SkyDataRef *)getWifiSSID:(NSDictionary *)params{
    //    NSString *callNo = [params stringValue:@"phoneNumber"];
        NSString *SSID = @"wifi";
        id info = nil;
        NSArray *ifs = (__bridge_transfer id)CNCopySupportedInterfaces();
        for (NSString *ifnam in ifs) {
            info = (__bridge_transfer id)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
            info = (NSDictionary *)info;
            if(info[@"SSID"] != NULL)
            {
                SSID = info[@"SSID"];
            }
        }
        return stringToDataRef(SSID);//返回值可以为NULL     return successObjToDataRef(NULL);
    
    }
    
    //oc-js端
    util.getWifiSSID = function() {
        return BridgeUtil.invokeStaticMethod("SkyNetwork","getWifiSSID");
    }
    //或者带有参数
    util.tel = function(phoneNumber) {
        BridgeUtil.invokeStaticMethod("SkyModelAppUrl", "tel", {
            "phoneNumber": phoneNumber
        });//SkyModelAppUrl 类名  tel 方法名 后面是json 参数
    }
    

    Utils 工具类第二种,需要跳转到oc界面,返回状态

    js端

     Utils.CCVideo(json,function(){
           log("登录成功");
         },function(errmsg){
           log(errmsg);
        });
    

    oc端-js代码

    //CC 视频接口-直播
    var __ccvideo = null;
    
    util.CCVideo =
        function(json,LoginSuccess,LoginFailure){
        if(__ccvideo == null)
            __ccvideo = new Emp.CCtools();
            __ccvideo.showVideo(json,LoginSuccess,LoginFailure);
    }
    Emp.CCtools =Emp.extend(Emp.Component,{
                            init: function(json) {
                            Emp.CCtools.superclass.init.call(this, json);
                            this.content.xtype = 'SkyCCVideoView';
                            Emp.newNativeObject(this);
                            },
        showVideo:function(json,LoginSuccess,LoginFailure){
                            this.addEvent('CConSuccess',LoginSuccess);
                            this.addEvent('CConFailure',LoginFailure);
                            empBridge.fireModelMethod(this.modelId,"showCCVideo",json);
            
                             }
    }
    

    oc-.h 和.m文件

    @interface SkyCCVideoView : SkyUiControl
    
    @end
    
    @implementation SkyCCVideoView
    -(instancetype)init{
        if (self =[super init]) {
    //        self.view = [[UIView alloc] init];
    //        [self setUIView:self.view];
        }
        return self;
    }
    -(void)processAttr{
        [super processAttr];
    }
    - (UIView *)render {
       return [super render];
    }
    //-=========集成SkyUiControl的必须三部,//processAttr处理控件所有属性  init创建可能会显示的view   render 渲染布局
    //直播
    -(SkyDataRef *)showCCVideo:(NSDictionary*)dic{
        /*
        *  必填参数 userId;//用户ID
        *  必填参数 roomId;//房间ID
        *  必填参数 viewerName;//用户名称
        *  必填参数 token;//房间密码--cc后台可设置(此处免密)
         */
        NSString *userId = [dic objectForKey:@"userId"];
        NSString *roomId =[dic objectForKey:@"roomId"];
        NSString *viewerName = [dic objectForKey:@"viewerName"];
        NSString *token;
        if ([[dic allKeys] containsObject:@"token"] && ![[dic objectForKey:@"token"] isEqual:[NSNull null]]) {
           token = [dic objectForKey:@"token"];
        }else{
            token = @"";
        }
        SaveToUserDefaults(WATCH_USERID,userId);
        SaveToUserDefaults(WATCH_ROOMID,roomId);
        SaveToUserDefaults(WATCH_USERNAME,viewerName);
        SaveToUserDefaults(WATCH_PASSWORD,token);
        [self integrationSDK];
    
    //回调的成功事件
        [self triggerEvent:@"CConSuccess",intToDataRef(index),nil];
    
    //跳转界面
    [((UIViewController *)self.scriptView.ownerCtrl).navigationController pushViewController:self.playForPCVC animated:YES];
        return NULL;
    }
    @end
    

    UI 类创建类型

    js端代码

    //创建需要显示的view
    var PDFReaderView = new Emp.PDFReaderView({
        height:"100%",
        width:"100%"
    });
    contentDiv.add(PDFReaderView);//contentDiv---div的id
    //调用方法
    PDFReaderView.showPDF(json);
    PDFReaderView.getPageCount();
    
    

    oc--js代码

    Emp.PDFReaderView = Emp.extend(Emp.View, {
                                   
                        init: function(params) {
                          Emp.PDFView.superclass.init.call(this, params);
                          this.content.xtype = 'SkyPDFReaderView';
                        },
                                   
                        // 显示PDF
                        showPDF: function(json) {
                          empPage.fireMethodIfRendered(this, 'showPDF',json);
                        },
                                   
                        // 获取pdf总页数
                        getPageCount: function() {
                          return empPage.fireMethodIfRendered(this, 'getPageCount',"");
                        }
    }
    

    oc--.h--.m

    // PDF阅读器视图
    @interface SkyPDFReaderView : SkyUiControl
    
    @end
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
            self.contentView = view;
            [self setUIView:view];
        }
        return self;
    }
    -(void)processAttr{
        [super processAttr];
    }
    - (UIView *)render {
       return [super render];
    }
    //================
    
    #pragma mark -- javascriptInterface
    // 显示PDF
    - (JSValueRef)showPDF:(JSContextRef)ctxp argc:(size_t)argc argv:(const JSValueRef [])argv {
        NSDictionary *dic = (NSDictionary *)[NSObject JSValueToNSObject:argv[0] ctx:ctxp];
        NSString *filePath = [dic objectForKey:@"filePath"];
        NSString *password = [dic objectForKey:@"password"];
        [self loadDocument:filePath password:password];
        return NULL;
    }
    
    // 获取pdf总页数
    - (JSValueRef)getPageCount:(JSContextRef)ctxp argc:(size_t)argc argv:(const JSValueRef [])argv {
        NSString *pageCount = [NSString stringWithFormat:@"%@",self.document.pageCount];
        return [pageCount toJSValue:ctxp];
    }
    
    
    @end
    

    相关文章

      网友评论

          本文标题:普元客户端与js交互笔记

          本文链接:https://www.haomeiwen.com/subject/hxfaactx.html