电子说
本文介绍将ArkUI框架扩展到iOS平台所需要的必要类及其使用说明,开发者基于OpenHarmony,可复用大部分的应用代码(生命周期等)并可以部署到iOS平台,降低跨平台应用开发成本。
开发前请熟悉鸿蒙开发指导文档:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
StageViewController是Stage模型iOS端视图控制器基类,若要实现跨平台基础能力及触发对应ability生命周期,所有iOS端应用级别的视图控制器均要继承于StageViewController。
@property (nonatomic, readonly) NSString *instanceName;
@property (nonatomic, strong) NSString *params;
- (instancetype)initWithInstanceName:(NSString *_Nonnull)instanceName;
StageApplication本质上是一个调度类,主要用于触发内部相关类实现路径解析与配置、注册应用相关的configuration信息、触发ability部分生命周期事件等。
+ (void)configModuleWithBundleDirectory:(NSString *_Nonnull)bundleDirectory;
+ (void)launchApplication;
+ (void)callCurrentAbilityOnForeground;
+ (void)callCurrentAbilityOnBackground;
+ (BOOL)handleSingleton:(NSString *)bundleName moduleName:(NSString *)moduleName abilityName:(NSString *)abilityName;
+ (void)releaseViewControllers;
+ (StageViewController *)getApplicationTopViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 配置hap包路径
[StageApplication configModuleWithBundleDirectory:@"arkui-x"];
// 启动ability
[StageApplication launchApplication];
// APP自启动,初始化StageViewController子类VC,并设置为APP根视图控制器
if (!launchOptions.count) {
NSString *instanceName = [NSString stringWithFormat:@"%@:%@:%@",@"com.example.iosabilitystage", @"entry", @"MainAbility"];
EntryMainViewController *mainView = [[EntryMainViewController alloc] initWithInstanceName:instanceName];
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:mainView];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = navi;
[self.window makeKeyAndVisible];
}
return YES;
}
当在iOS平台上使用[startability]接口实现页面跳转时,需要参考下述示例进行开发。
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary< NSString *,id > *)options {
// 根据规则截取URL相应参数
NSString *bundleName = url.scheme;
NSString *moduleName = url.host;
NSString *abilityName, *params;
NSURLComponents *urlComponents = [NSURLComponents componentsWithString:url.absoluteString];
NSArray < NSURLQueryItem * > *array = urlComponents.queryItems;
for (NSURLQueryItem * item in array) {
if ([item.name isEqualToString:@"abilityName"]) {
abilityName = item.value;
} else if ([item.name isEqualToString:@"params"]) {
params = item.value;
}
}
// 单实例ability处理
if ([StageApplication handleSingleton:bundleName moduleName:moduleName abilityName:abilityName] == YES) {
return YES;
}
[self handleOpenUrlWithBundleName:bundleName
moduleName:moduleName
abilityName:abilityName
params:params, nil];
return YES;
}
- (BOOL)handleOpenUrlWithBundleName:(NSString *)bundleName
moduleName:(NSString *)moduleName
abilityName:(NSString *)abilityName
params:(NSString *)params, ...NS_REQUIRES_NIL_TERMINATION {
NSString *instanceName = [NSString stringWithFormat:@"%@:%@:%@",bundleName, moduleName, abilityName];
// 根据moduleName和abilityName映射对应的viewController
// 注意:传入的moduleName或者abilityName错误,则无法找到对应的viewController,此时无法打开页面。
if ([moduleName isEqualToString:@"entry"] && [abilityName isEqualToString:@"MainAbility"]) {
EntryMainAbilityViewController *entryMainVC = [[EntryMainAbilityViewController alloc] initWithInstanceName:instanceName];
entryMainVC.params = params;
} else if ([moduleName isEqualToString:@"entry"] && [abilityName isEqualToString:@"Other"]) {
EntryOtherViewController *entryOtherVC = [[EntryOtherViewController alloc] initWithInstanceName:instanceName];
entryOtherVC.params = params;
}
return YES;
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[StageApplication callCurrentAbilityOnBackground];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[StageApplication callCurrentAbilityOnForeground];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[StageApplication releaseViewControllers];
}
注 具体方法使用参考samples示例
iOS端应用info配置里的bundleName需要与Ability的bundleName一致。
iOS端应用内的viewController的viewControllerName组成规则:Ability的moduleName + Ability的abilityName + “viewController”。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 配置hap包路径
[StageApplication configModuleWithBundleDirectory:@"arkui-x"];
// 启动ability
[StageApplication launchApplication];
// APP自启动,初始化StageViewController子类VC,并设置为APP根视图控制器
if (!launchOptions.count) {
NSString *instanceName = [NSString stringWithFormat:@"%@:%@:%@",@"com.example.iosabilitystage", @"entry", @"MainAbility"];
EntryMainViewController *mainView = [[EntryMainViewController alloc] initWithInstanceName:instanceName];
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:mainView];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = navi;
[self.window makeKeyAndVisible];
}
return YES;
}
`HarmonyOS与OpenHarmony鸿蒙文档籽料:mau123789是v直接拿`
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 不在此处进行初始化
ExampleViewController *mainView = [[ExampleViewController alloc] init];
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:mainView];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = navi;
[self.window makeKeyAndVisible];
return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary< NSString *,id > *)options {
// 初始化StageApplication
[StageApplication configModuleWithBundleDirectory:@"arkui-x"];
[StageApplication launchApplication];
/*
other code
*/
return YES;
}
sf
全部0条评论
快来发表一下你的评论吧 !