个人觉得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
@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];
}
以上!