iOS Weex与Native交互


个人觉得WEEX不是一种特别好的解决动态更新的方式。

本文只说代码方便交互。具体配置请看官网

1:注册(UZGWeexOCModel是跟WEEX交互的一个模型,所有代码全部在这个模型里写就可以了

- (void)initWeexSDK{

    [WXAppConfigurationsetAppGroup:@"AliApp"];

    [WXAppConfigurationsetAppName:@"WeexDemo"];

    [WXAppConfigurationsetAppVersion:@"1.0.0"];

    [WXSDKEngineregisterModule:@"XXX"withClass:[UZGWeexOCModelclass]];

    [WXSDKEngineregisterHandler:[WeexImgLoaderDefaultImplnew] withProtocol:@protocol(WXImgLoaderProtocol)];

    [WXSDKEngineinitSDKEnvironment];

    [WXLogsetLogLevel:WXLogLevelAll];

}

2:创建交互模型

#import <WeexSDK/WXSDKInstance.h>

#import <WeexSDK/WXEventModuleProtocol.h>

#import <WeexSDK/WXModuleProtocol.h>

#import <WeexSDK/WXNetworkProtocol.h>

#import <WeexSDK/WXUtility.h>

#import <WeexSDK/WXResourceRequestHandler.h>


@interface UZGWeexOCModel :NSObject<WXEventModuleProtocol,WXModuleProtocol,WXResourceRequestHandler>

@end


3:模型实现

(1)比如说,如果WEEX那边要调用我们的方法,你需要先Export这个方法,这样WEEX才能调用不出错

WX_EXPORT_METHOD(@selector(wxShare:)),这种是简单的传参方法。

(2)如果有回调的方法的话

WX_EXPORT_METHOD(@selector(getUserInfo:))

- (void)getUserInfo:(WXModuleCallback)callback{

     callback(dic);//通过callback回调给WEEX即可

}

(3)由于WEEX那边没有下载图片的组件,你需要自己定义组件

#import "WeexImgLoaderDefaultImpl.h"

#import <SDWebImage/UIImageView+WebCache.h>


@implementation WeexImgLoaderDefaultImpl

- (id<WXImageOperationProtocol>)downloadImageWithURL:(NSString *)url imageFrame:(CGRect)imageFrame userInfo:(NSDictionary *)userInfo completed:(void(^)(UIImage *image, NSError *error, BOOL finished))completedBlock

{

    if ([urlhasPrefix:@"jpg"] || [urlhasPrefix:@"png"]) {

        /* 做相应的处理 */

    }

    return (id<WXImageOperationProtocol>)[[SDWebImageManagersharedManager]loadImageWithURL:[NSURLURLWithString:url] options:0progress:^(NSInteger receivedSize,NSInteger expectedSize, NSURL * _Nullable targetURL) {

        

    } completed:^(UIImage *_Nullable image, NSData * _Nullable data,NSError * _Nullable error,SDImageCacheType cacheType,BOOL finished, NSURL *_Nullable imageURL) {

        if (completedBlock) {

            completedBlock(image, error, finished);

        };

    }];

}

@end



4:创建加载WEEX的页面

@interface WeexDemoVC :UZGBaseViewController

- (instancetype)initWithJs:(NSString *)filePath;

@end

@implementation WeexDemoVC

{

    NSURL *jsUrl;

}

- (instancetype)initWithJs:(NSString *)filePath

{

    self = [superinit];

    if (self) {

        jsUrl=[NSURLURLWithString:filePath];

    }

    returnself;

}

- (void)viewDidLoad {

    [superviewDidLoad];

    [self.viewsetBackgroundColor:[UIColorwhiteColor]];

    [selfcreateBarTintColorF5f5f5Color];

    [selfcreateBlackBackButton];

    [SMProgressViewshow];

    [selfperformSelector:@selector(hideProgressView)withObject:nilafterDelay:10];


    _instance = [[WXSDKInstancealloc] init];

    _instance.viewController =self;

    _instance.frame=CGRectMake(0,NAVHEIGHT,SCREENW,SCREENH-NAVHEIGHT);

    __weaktypeof(self) weakSelf =self;

    _instance.onCreate = ^(UIView *view) {

        [weakSelf.weexViewremoveFromSuperview];

        weakSelf.weexView = view;

        [weakSelf.viewaddSubview:weakSelf.weexView];

        [weakSelf.viewsendSubviewToBack:weakSelf.weexView];

    };

    _instance.onFailed = ^(NSError *error) {

        NSLog(@"链接---加载错误的原因:%@",[errorlocalizedDescription]);

    };

    _instance.renderFinish = ^ (UIView *view) {

        NSLog(@"链接---加载完成");

    };

    if (!jsUrl) {

        return;

    }

    [_instancerenderWithURL:jsUrl];

}

以上!



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值