准备 Xcode 项目
首先参照为 iOS 构建 Lynx Explorer 指南在本地创建 Lynx Explorer 项目,并用 Xcode 打开项目。

接着在 Lynx Explorer 项目的 modules 文件夹上右键点击,选择 New Fie... 创建原生模块代码文件。

然后使用 Cocoa Touch Class 模板。

将类命名为 NativeLocalStorageModule,语言你可以选择创建 Objective-C,也可以选择 Swift。接着点击 Next,完成文件的创建。

实现原生模块
INFO
原生模块需要实现额外的静态方法 name,返回原生模块的导出名字 ;实现额外的静态方法 methodLookup,将需要导出的方法名和对应的选择器进行映射。
explorer/darwin/ios/lynx_explorer/LynxExplorer/modules/NativeLocalStorageModule.h
#import <Foundation/Foundation.h>
#import <Lynx/LynxModule.h>
NS_ASSUME_NONNULL_BEGIN
@interface NativeLocalStorageModule : NSObject <LynxModule>
@end
NS_ASSUME_NONNULL_END
explorer/darwin/ios/lynx_explorer/LynxExplorer/modules/NativeLocalStorageModule.m
#import "NativeLocalStorageModule.h"
@interface NativeLocalStorageModule()
@property (strong, nonatomic) NSUserDefaults *localStorage;
@end
@implementation NativeLocalStorageModule
static NSString *const NativeLocalStorageKey = @"MyLocalStorage";
- (instancetype)init {
if (self = [super init]) {
_localStorage = [[NSUserDefaults alloc] initWithSuiteName:NativeLocalStorageKey];
}
return self;
}
+ (NSString *)name {
return @"NativeLocalStorageModule";
}
+ (NSDictionary<NSString *, NSString *> *)methodLookup {
return @{
@"setStorageItem": NSStringFromSelector(@selector(setStorageItem:value:)),
@"getStorageItem": NSStringFromSelector(@selector(getStorageItem:callback:)),
@"clearStorage": NSStringFromSelector(@selector(clearStorage))
};
}
- (void)setStorageItem:(NSString *)key value:(NSString *)value {
[self.localStorage setObject:value forKey:key];
}
- (void)getStorageItem:(NSString *)key callback:(void(^)(NSString *value)) callback{
NSString *value = [self.localStorage stringForKey:key];
callback(value);
}
- (void)clearStorage {
NSDictionary *keys = [self.localStorage dictionaryRepresentation];
for (NSString *key in keys) {
[self.localStorage removeObjectForKey:key];
}
}
@end
接下来将原生模块注册到 Lynx 运行时环境中。
在 Lynx Explorer 工程的 explorer/darwin/ios/lynx_explorer/LynxExplorer/LynxInitProcessor.m 文件的 setupLynxEnv 方法中添加如下注册代码,将原生模块注册到 Lynx 运行时环境的全局配置中。
explorer/darwin/ios/lynx_explorer/LynxExplorer/LynxInitProcessor.m
#import "NativeLocalStorageModule.h"
- (void)setupLynxEnv {
// ...
// register global JS module
[globalConfig registerModule:NativeLocalStorageModule.class];
// ...
}
运行代码
准备好所有内容后,现在可以构建运 行你的代码:
首先参照编译和运行 iOS Lynx Explorer 指南构建 Lynx Explorer,并安装到你的手机上。
接着参考安装依赖&启动开发服务器指南,在 Lynx 项目根目录下安装依赖并且启动开发服务器。
安装依赖:
启动开发服务器:
你将会看到控制台中看到二维码和产物链接,使用 Lynx Explorer 扫描二维码或者填写产物链接即可打开你的 Lynx 页面。

首先参照为 Android 构建 Lynx Explorer 指南在本地创建 Lynx Explorer 项目。
接着在 Lynx Explorer 项目的 explorer/android/lynx_explorer/src/main/java/com/lynx/explorer/modules/ 路径下新建 NativeLocalStorageModule.java 或 NativeLocalStorageModule.kt 文件,并继承 LynxModule 实现 NativeLocalStorageModule 原生模块。
INFO
需要在原生模块的方法上添加 @LynxMethod 注解,实现方法的导出。
explorer/android/lynx_explorer/src/main/java/com/lynx/explorer/modules/NativeLocalStorageModule.java
package com.lynx.explorer.modules;
import android.content.Context;
import android.content.SharedPreferences;
import com.lynx.jsbridge.LynxMethod;
import com.lynx.jsbridge.LynxModule;
import com.lynx.tasm.behavior.LynxContext;
import com.lynx.react.bridge.Callback
public class NativeLocalStorageModule extends LynxModule {
private static final String PREF_NAME = "MyLocalStorage";
public NativeLocalStorageModule(Context context) {
super(context);
}
Context getContext() {
LynxContext lynxContext = (LynxContext) mContext;
return lynxContext.getContext();
}
@LynxMethod
public void setStorageItem(String key, String value) {
SharedPreferences sharedPreferences = getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
@LynxMethod
public void getStorageItem(String key, Callback callback) {
SharedPreferences sharedPreferences = getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
String value = sharedPreferences.getString(key, null);
callback.invoke(value);
}
@LynxMethod
public void clearStorage() {
SharedPreferences sharedPreferences = getContext().getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
}
}
接下来将原生模块注册到 Lynx 运行时环境中。
在 Lynx Explorer 项目的 explorer/android/lynx_explorer/src/main/java/com/lynx/explorer/modules/LynxModuleAdapter.java 文件的 Init 方法中添加如下注册代码,将原生模块注册到 Lynx 运行时环境中。在这里需要指定导出的原生模块的名称,需要和上面的接口规范保持一致。
explorer/android/lynx_explorer/src/main/java/com/lynx/explorer/modules/LynxModuleAdapter.java
public void Init(Context context) {
// ......
LynxEnv.inst().registerModule("NativeLocalStorageModule", NativeLocalStorageModule.class);
// ......
}
准备好所有内容后,现在可以构建运行你的代码。
首先参照编译和运行 Android Lynx Explorer 指南从源码构建 Lynx Explorer,并安装到你的手机上。
接着参考安装依赖&启动开发服务器指南,在 Lynx 项目根目录下安装依赖并且启动开发服务器。
安装依赖:
启动开发服务器:
你将会在控制台中看到二维码,使用 Lynx Explorer 扫描二维码来打开页面。

首先参照为 HarmonyOS 构建 Lynx Explorer 指南在本地创建 Lynx Explorer 项目。
接着在 Lynx Explorer 项目下新建 NativeLocalStorageModule.ets 文件,并继承 @lynx/lynx 包中的 LynxModule 实现 NativeLocalStorageModule 原生模块。
INFO
你可以为 Module class 加上 @Sendable 注解,将其转换为 Sendable Module。只有 Sendable Module 的 Method 是同步的,其余的 Method 都是异步的。异步 Method 的返回值是 null。
NativeLocalStorageModule.ets
import { LynxContext, LynxModule } from '@lynx/lynx';
import common from '@ohos.app.ability.common';
import { preferences } from '@kit.ArkData';
export class NativeLocalStorageModule extends LynxModule {
private static readonly PREF_NAME: string = "NativeLocalStorageModule";
private dataPreferences: preferences.Preferences | null = null;
constructor(context: LynxContext, param?: Object) {
super(context, param);
this.initPreferencesSync();
}
private initPreferencesSync() {
try {
this.dataPreferences = preferences.getPreferencesSync(
getContext(),
{ name: NativeLocalStorageModule.PREF_NAME }
);
console.info("Preferences initialization succeeded.");
} catch (error) {
console.error(`Failed to initialize preferences: ${error}`);
}
}
public setStorageItem(key: string, value: string) {
if (!this.dataPreferences) {
console.error("Preferences instance is not initialized.");
return;
}
try {
this.dataPreferences.putSync(key, value);
this.dataPreferences.flushSync();
console.info(`Storage item set successfully. key=${key}`);
} catch (err) {
console.error(`Failed to set storage item. key=${key}, error=${err.message}`);
}
}
public getStorageItem(key: string, callback: (value: string)=> void) {
if (!this.dataPreferences) {
console.error("Preferences instance is not initialized.");
callback("");
return;
}
try {
const value = this.dataPreferences.getSync(key, "");
callback(value as string);
return;
} catch (err) {
console.error(`Failed to get storage item. key=${key}, error=${err.message}`);
callback("");
return;
}
}
public clearStorage() {
if (!this.dataPreferences) {
console.error("Preferences instance is not initialized.");
return;
}
try {
this.dataPreferences.clearSync();
this.dataPreferences.flushSync();
console.info("Storage cleared successfully.");
} catch (err) {
console.error(`Failed to clear storage. error=${err.message}`);
}
}
}
接下来将原生模块注 册到 Lynx 运行时环境中。
在 Lynx Explorer 项目的 explorer/harmony/lynx_explorer/src/main/ets/pages/Lynx.ets 文件的 aboutToAppear 方法中添加如下注册代码,将原生模块注册到 Lynx 运行时环境中。在这里需要指定原生模块的名称,需要和上面接口规范里的名称保持一致。
INFO
普通 Module 注册到 this.modules 中,Sendable Module 注册到 this.sendableModules。
src/main/ets/pages/Lynx.ets
import { NativeLocalStorageModule } from '../module/NativeLocalStorageModule';
@Entry
@Component
struct Lynx {
aboutToAppear() {
// `param` 是模块的可选构造参数
this.modules.set('NativeLocalStorageModule', {
moduleClass: NativeLocalStorageModule
});
// Sendable Module 请注册到 this.sendableModules
}
}
准备好所有内容后,现在可以构建运行你的代码。
首先参照编译和运行 Lynx Explorer 指南从源码构建 Lynx Explorer,并安装到你的手机上。
接着参考安装依赖&启动开发服务器指南,在 Lynx 项目根目录下安装依赖并且启动开发服务器。
安装依赖:
启动开发服务器:
你将会在控制台中看到二维码,使用 Lynx Explorer 扫描二维码来打开页面。
