Weex 創(chuàng)建一個(gè) Weex 實(shí)例

2023-12-27 15:58 更新

你既可以在全頁(yè)面中使用 Weex,也可以在一個(gè) view 中渲染 Weex。只需要?jiǎng)?chuàng)建一個(gè) Weex 實(shí)例并指定好回調(diào)方法,提供一個(gè)合法的 URL 就可以了。在 ?onCreate? 回調(diào)方法中將根 view 添加到你想顯示內(nèi)容的地方,并通過(guò)? instance.frame =? 來(lái)設(shè)置它的尺寸和位置。

#import <WeexSDK/WXSDKInstance.h>

- (void)viewDidLoad
{
    [super viewDidLoad];
    _instance = [[WXSDKInstance alloc] init];
    _instance.viewController = self;
    _instance.frame = self.view.frame;
    __weak typeof(self) weakSelf = self;
    _instance.onCreate = ^(UIView *view) {
        [weakSelf.weexView removeFromSuperview];
        weakSelf.weexView = view;
        [weakSelf.view addSubview:view];
    };
    _instance.onFailed = ^(NSError *error) {
        //process failure, you could open an h5 web page instead or just show the error.
    };
    _instance.renderFinish = ^ (UIView *view) {
        //process renderFinish
    };
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"js"];
    [_instance renderWithURL:url options:@{@"bundleUrl":[self.url absoluteString]} data:nil];
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)