加载中...

1.4.7 扩展iOS


Module 扩展

swift 扩展 module

Weex SDK 只提供渲染,而不是其他的能力,如果你需要 像网络,图片,URL跳转这些特性,需要自己动手实现他们
例如,如果你想实现一个url地址跳转函数,你可以按照如下步骤实现一个 Module

  1. 自定义module的步骤

    1. 自定义的module类 必须实现 WXModuleProtocol

    2. 必须添加宏WX_EXPORT_METHOD, 它可以被weex识别,它的参数是 JavaScript调用 module指定方法的参数

    3. 添加@synthesized weexInstance,每个moudle对象被绑定到一个指定的实例上

    4. Module 方法会在UI线程中被调用,所以不要做太多耗时的任务在这里,如果要在其他线程执行整个module 方法,需要实现WXModuleProtocol- (NSThread *)targetExecuteThread的方法,这样,分发到这个module的任务会在指定的线程中运行

    5. Weex 的参数可以是 String 或者Map

    6. Module 支持返回值给 JavaScript中的回调,回调的类型是WXModuleCallback,回调的参数可以是String或者Map

      1. @implementation WXEventModule
      2. @synthesize weexInstance;
      3. WX_EXPORT_METHOD(@selector(openURL:callback))
      4. - (void)openURL:(NSString *)url callback:(WXModuleCallback)callback
      5. {
      6. NSString *newURL = url;
      7. if ([url hasPrefix:@"//"]) {
      8. newURL = [NSString stringWithFormat:@"http:%@", url];
      9. } else if (![url hasPrefix:@"http"]) {
      10. newURL = [NSURL URLWithString:url relativeToURL:weexInstance.scriptURL].absoluteString;
      11. }
      12. UIViewController *controller = [[WXDemoViewController alloc] init];
      13. ((WXDemoViewController *)controller).url = [NSURL URLWithString:newURL];
      14. [[weexInstance.viewController navigationController] pushViewController:controller animated:YES];
      15. callback(@{@"result":@"success"});
      16. }
      17. @end
  2. Register the module
    通过调用 WXSDKEngine 中的 registerModule:withClass方法来注册自己的module

    1. WXSDKEngine.h
    2. /**
    3. * @abstract Registers a module for a given name
    4. * @param name The module name to register
    5. * @param clazz The module class to register
    6. **/
    7. + (void)registerModule:(NSString *)name withClass:(Class)clazz;
    8. [WXSDKEngine registerModule:@"event" withClass:[WXEventModule class]];
  3. 使用自己的module
    这里的 require 里面的event 就是在 上一步调用registerModule: 注册module 时候的name

    1. var eventModule = require('@weex-module/event');
    2. eventModule.openURL('url',function(ret) {
    3. nativeLog(ret);
    4. });

    handler 扩展

    Weex SDK没有 图片下载,navigation 操作的能力,请大家自己实现这些 protocol

  4. WXImgLoaderProtocol
    weexSDK 没有图片下载的能力,需要实现 WXImgLoaderProtocol,参考下面的例子

    1. WXImageLoaderProtocol.h
    2. @protocol WXImgLoaderProtocol <WXModuleProtocol>
    3. /**
    4. * @abstract Creates a image download handler with a given URL
    5. * @param imageUrl The URL of the image to download
    6. * @param imageFrame The frame of the image you want to set
    7. * @param options : The options to be used for this download
    8. * @param completedBlock : A block called once the download is completed.
    9. image : the image which has been download to local.
    10. error : the error which has happened in download.
    11. finished : a Boolean value indicating whether download action has finished.
    12. */
    13. -(id<WXImageOperationProtocol>)downloadImageWithURL:(NSString *)url imageFrame:(CGRect)imageFrame userInfo:(NSDictionary *)options completed:(void(^)(UIImage *image, NSError *error, BOOL finished))completedBlock;
    14. @end

    实现上述协议

    1. @implementation WXImgLoaderDefaultImpl
    2. #pragma mark -
    3. #pragma mark WXImgLoaderProtocol
    4. - (id<WXImageOperationProtocol>)downloadImageWithURL:(NSString *)url imageFrame:(CGRect)imageFrame userInfo:(NSDictionary *)userInfo completed:(void(^)(UIImage *image, NSError *error, BOOL finished))completedBlock
    5. {
    6. if ([url hasPrefix:@"//"]) {
    7. url = [@"http:" stringByAppendingString:url];
    8. }
    9. return (id<WXImageOperationProtocol>)[[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:url] options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
    10. } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
    11. if (completedBlock) {
    12. completedBlock(image, error, finished);
    13. }
    14. }];
    15. }
    16. @end
  5. handler注册
    你可以通过WXSDKEngine 中的 registerHandler:withProtocol注册handler

    1. WXSDKEngine.h
    2. /**
    3. * @abstract Registers a handler for a given handler instance and specific protocol
    4. * @param handler The handler instance to register
    5. * @param protocol The protocol to confirm
    6. */
    7. + (void)registerHandler:(id)handler withProtocol:(Protocol *)protocol;
    8. [WXSDKEngine registerHandler:[WXImgLoaderDefaultImpl new] withProtocol:@protocol(WXImgLoaderProtocol)]

Custom Native Components for iOS

  1. Component 扩展
    虽然WeexSDK中有很多的native的Component,但这有可能并不能满足你的需求。在之前你可能已经写了一些很酷炫native的组件,想包装一下,导入到Weex中,因此我们提供了让开发者实现自己的native Component
    下面将以WeexSDK 中已经存在的 Component:image为例子,介绍一下如何构建一个native Component.
    假设你已经了解IOS开发

    1. 注册 Component
      注册一个component比较简单,调用 WXSDKEngine 中的 registerComponent:withClass:方法,传入组件的标签名称,还有对应的class
      然后你可以创建一个 WXImageComponent 表示image组件的实现 在.we 文件中,只需要写
      <image></image>

    2. 添加属性
      现在我们要做一些让image component更加强大的事情。既然作为一个图片的component,那它应该要有源,给他加上一个 src的属性,同时给它加上一个resize的属性(可以配置的有contain/cover/stretch

      1. @interface WXImageComponent ()
      2. @property (nonatomic, strong) NSString *imageSrc;
      3. @property (nonatomic, assign) UIViewContentMode resizeMode;
      4. @end

      component中所有的style,attribute,events都会被传递到 Component的初始化方法中,所以,你可以在初始化方法中存储你感兴趣的一些属性值

      1. @implementation WXImageComponent
      2. - (instancetype)initWithRef:(NSString *)ref type:(NSString *)type styles:(NSDictionary *)styles attributes:(NSDictionary *)attributes events:(NSArray *)events weexInstance:(WXSDKInstance *)weexInstance
      3. {
      4. if (self = [super initWithRef:ref type:type styles:styles attributes:attributes events:events weexInstance:weexInstance]) {
      5. _imageSrc = [WXConvert NSString:attributes[@"src"]];
      6. _resizeMode = [WXConvert UIViewContentMode:attributes[@"resize"]];
      7. }
      8. return self;
      9. }
      10. @end

      attribute中拿到的值的类型都是id,我们可以用转换方法把它转换到任何值。Weex SDK提供了一些基础的转换方法,可以参考 WXConvert类,或者你可以添加自己的转换函数

      1. Hooking 渲染生命周期
        native 的component 是由Weex管理的,weex 创建,布局,渲染,销毁。weex的component生命周期都是可以hook的,你可以在这些生命周期中去做自己的事情
      方法 描述
      initWithRef:type:... 用给定的属性初始化一个component.
      layoutDidFinish 在component完成布局时候会调用.
      loadView 创建component管理的view.
      viewWillLoad 在component的view加载之前会调用.
      viewDidLoad 在component的view加载完之后调用.
      viewWillUnload 在component的view被释放之前调用.
      viewDidUnload 在component的view被释放之后调用.
      updateStyles: 在component的style更新时候调用.
      updateAttributes: 在component的attribute更新时候调用.
      addEvent: 给component添加event的时候调用.
      removeEvent: 在event移除的时候调用.

      在image component的例子里面,如果我们需要我们自己的image view 的话,可以复写 loadView这个方法.

    1. - (UIView *)loadView
    2. {
    3. return [[WXImageView alloc] init];
    4. }

    现在我们使用 WXImageView 渲染 image component。
    作为一个image component,我们需要拿到服务器图片,而且把它设置进image view 里. 这个操作可以在 viewDidLoad 方法中做,这个方法是在view已经被创建而且加载了时候weex SDK会调用到,而且viewDidLoad这个方法是你做额外初始化工作比如改变content mode(也就是设置resize) 的最好时间.

    1. - (void)viewDidLoad
    2. {
    3. UIImageView *imageView = (UIImageView *)self.view;
    4. imageView.contentMode = _resizeMode;
    5. imageView.userInteractionEnabled = YES;
    6. imageView.clipsToBounds = YES;
    7. imageView.exclusiveTouch = YES;
    8. // Do your image fetching and updating logic
    9. }

    如果可以改变image的src,也可以hook updateAttributes:方法来做属性更新操作,当updateAttributes:或者 updateStyles:被调用的时候, component的view 已经加载完成

    1. - (void)updateAttributes:(NSDictionary *)attributes
    2. {
    3. if (attributes[@"src"]) {
    4. _imageSrc = [WXConvert NSString:attributes[@"src"]];
    5. // Do your image updating logic
    6. }
    7. if (attributes[@"resize"]) {
    8. _resizeMode = [WXConvert UIViewContentMode:attributes[@"resize"]];
    9. self.view.contentMode = _resizeMode;
    10. }
    11. }

    或许你需要考虑更多的生命周期方法去Hook,当布局完成时候,像layoutDidFinish,如果你想了解更多,可以参考一下WXComponent.h 声明的方法
    现在你可以用在任何 .we文件里面使用 <image>,而且可以加上 image的属性

    1. <image style="your-custom-style" src="image-remote-source" resize="contain/cover/stretch"></image>

还没有评论.