- 使用 Swift 扩展 Weex
- 使用 Swift 进行 module 扩展
- module 使用
使用 Swift 扩展 Weex
Swift和Objective-C 混编
参考完整 例子
使用 Swift 进行 module 扩展
因为 module 暴露 method 是通过Objective-C宏来做的,调用的时候是通过反射,所以Swift扩展 module 通过extension Objective-C的类。
- 新建
WXSwiftTestModule.h/m和WXSwiftTestModule.swift文件, 在新建Swift文件的时候会提示
选择 Create Bridging Header, 因为我们要在 Swift 中访问Objective-C的一些类,正是通过这个 header暴露 OC 的类给 Swift,header 格式为yourTarget-Bridging-Header.h,我这里创建完header文件名称为:WeexDemo-Bridging-Header.h WXSwiftTestModule.h/m中实现WXSwiftTestModule.h 中
#import <Foundation/Foundation.h>#import <WeexSDK/WeexSDK.h>@interface WXSwiftTestModule : NSObject <WXModuleProtocol>@end
WXSwiftTestModule.m 中
WeexDemo-Swift.h 这个文件需要编译一下才可以搜索到,具体的路径
weex/ios/playground/DerivedData/WeexDemo/Build/Intermediates/WeexDemo.build/Debug-iphonesimulator/WeexDemo.build/DerivedSources/WeexDemo-Swift.h
路径具体需要根据自己工程而定
#import "WXSwiftTestModule.h"#import "WeexDemo-Swift.h" // Swift类和方法 被 `Objective-C` 识别需要导入@implementation WXSwiftTestModule#pragma clang diagnostic push //关闭unknow selector的warrning#pragma clang diagnostic ignored "-Wundeclared-selector"WX_EXPORT_METHOD(@selector(printSome:callback:)) //Swift 中定义的方法,XCode 转换成的最终的方法名称,在`WeexDemo-Swift.h`里面查看#pragma clang diagnostic pop@end
Swift 中实现扩展 OC 的类
WXSwiftTestModule,增加了一个方法,这个方法就是我们要暴露出来,在 js 中可以调到的WXSwiftTestModule.swift
import Foundationpublic extension WXSwiftTestModule {public func printSome(someThing:String, callback:WXModuleCallback) {print(someThing)callback(someThing)}}
WXSwiftTestModule和WXModuleCallback因为是 OC 的,需要在WeexDemo-Bridging-Header中暴露WeexDemo-Bridging-Header.h中
//// Use this file to import your target's public headers that you would like to expose to Swift.//#import "WXSwiftTestModule.h"#import "WeexSDK.h"
至此这个使用 Swift 开发的简单的 module 已经完成
module 使用
注册 module
[WXSDKEngine registerModule:@"swifter" withClass:[WXSwiftTestModule class]];
前端脚本中用法
<template><text>Swift Module</text></template><script>module.exports = {ready: function() {var swifter = weex.require('swifter');swifter.printSome("https://www.taobao.com",function(param){nativeLog(param);});}};</script>
