通過Stage模型開發(fā)iOS端應(yīng)用指南
簡介
本文介紹將ArkUI框架擴(kuò)展到iOS平臺(tái)所需要的必要類及其使用說明,開發(fā)者基于OpenHarmony,可復(fù)用大部分的應(yīng)用代碼(生命周期等)并可以部署到iOS平臺(tái),降低跨平臺(tái)應(yīng)用開發(fā)成本。
ArkUI-X和iOS平臺(tái)集成所用關(guān)鍵類
開發(fā)前請(qǐng)熟悉鴻蒙開發(fā)指導(dǎo)文檔:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md
]
StageViewController
StageViewController是Stage模型iOS端視圖控制器基類,若要實(shí)現(xiàn)跨平臺(tái)基礎(chǔ)能力及觸發(fā)對(duì)應(yīng)ability生命周期,所有iOS端應(yīng)用級(jí)別的視圖控制器均要繼承于StageViewController。
公共屬性
- instanceName:StageViewController唯一標(biāo)識(shí),拼接規(guī)則為 bundleName:moduleName:abilityName ,其中bundleName的值來自于OpenHarmony應(yīng)用中app.json5配置文件,moduleName、abilityName的值來自于OpenHarmony應(yīng)用中的module.json5配置文件。
@property (nonatomic, readonly) NSString *instanceName;
@property (nonatomic, strong) NSString *params;
初始化方法
- (instancetype)initWithInstanceName:(NSString *_Nonnull)instanceName;
StageApplication
StageApplication本質(zhì)上是一個(gè)調(diào)度類,主要用于觸發(fā)內(nèi)部相關(guān)類實(shí)現(xiàn)路徑解析與配置、注冊(cè)應(yīng)用相關(guān)的configuration信息、觸發(fā)ability部分生命周期事件等。
公共方法
- 配置本地hap包路徑。
+ (void)configModuleWithBundleDirectory:(NSString *_Nonnull)bundleDirectory;
- iOS應(yīng)用觸發(fā)StartAbility、配置進(jìn)程id、本地化信息、configuration等。
+ (void)launchApplication;
- 觸發(fā)ability進(jìn)入前臺(tái)生命周期事件。
+ (void)callCurrentAbilityOnForeground;
- 觸發(fā)ability進(jìn)入后臺(tái)生命周期事件。
+ (void)callCurrentAbilityOnBackground;
- 處理單/多實(shí)例ability。
+ (BOOL)handleSingleton:(NSString *)bundleName moduleName:(NSString *)moduleName abilityName:(NSString *)abilityName;
- 釋放導(dǎo)航視圖棧內(nèi)的所有viewController,觸發(fā)OnDestory事件。
+ (void)releaseViewControllers;
- 獲取導(dǎo)航視圖棧最頂層viewController。
+ (StageViewController *)getApplicationTopViewController;
AppDelegate內(nèi)關(guān)鍵實(shí)現(xiàn)參考
ArkUI應(yīng)用啟動(dòng)及初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 配置hap包路徑
[StageApplication configModuleWithBundleDirectory:@"arkui-x"];
// 啟動(dòng)ability
[StageApplication launchApplication];
// APP自啟動(dòng),初始化StageViewController子類VC,并設(shè)置為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;
}
ArkUI應(yīng)用實(shí)現(xiàn)頁面跳轉(zhuǎn)
當(dāng)在iOS平臺(tái)上使用[startability]接口實(shí)現(xiàn)頁面跳轉(zhuǎn)時(shí),需要參考下述示例進(jìn)行開發(fā)。
- 通過路由模式(openURL:)實(shí)現(xiàn)的iOS應(yīng)用頁面跳轉(zhuǎn)回調(diào),獲取傳遞參數(shù)
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary< NSString *,id > *)options {
// 根據(jù)規(guī)則截取URL相應(yīng)參數(shù)
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;
}
}
// 單實(shí)例ability處理
if ([StageApplication handleSingleton:bundleName moduleName:moduleName abilityName:abilityName] == YES) {
return YES;
}
[self handleOpenUrlWithBundleName:bundleName
moduleName:moduleName
abilityName:abilityName
params:params, nil];
return YES;
}
- 通過解析url得到的參數(shù),映射ability對(duì)應(yīng)的viewController
- (BOOL)handleOpenUrlWithBundleName:(NSString *)bundleName
moduleName:(NSString *)moduleName
abilityName:(NSString *)abilityName
params:(NSString *)params, ...NS_REQUIRES_NIL_TERMINATION {
NSString *instanceName = [NSString stringWithFormat:@"%@:%@:%@",bundleName, moduleName, abilityName];
// 根據(jù)moduleName和abilityName映射對(duì)應(yīng)的viewController
// 注意:傳入的moduleName或者abilityName錯(cuò)誤,則無法找到對(duì)應(yīng)的viewController,此時(shí)無法打開頁面。
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;
}
ArkUI應(yīng)用生命周期回調(diào)相應(yīng)處理
- ArkUI應(yīng)用進(jìn)入后臺(tái),觸發(fā)對(duì)應(yīng)生命周期事件。
- (void)applicationDidEnterBackground:(UIApplication *)application {
[StageApplication callCurrentAbilityOnBackground];
}
- ArkUI應(yīng)用進(jìn)入前臺(tái),觸發(fā)對(duì)應(yīng)生命周期事件。
- (void)applicationWillEnterForeground:(UIApplication *)application {
[StageApplication callCurrentAbilityOnForeground];
}
- 終止ArkUI應(yīng)用程序進(jìn)程。
- (void)applicationWillTerminate:(UIApplication *)application {
[StageApplication releaseViewControllers];
}
注 具體方法使用參考samples示例
Ability與ViewController對(duì)應(yīng)規(guī)則
iOS端應(yīng)用info配置里的bundleName需要與Ability的bundleName一致。
iOS端應(yīng)用內(nèi)的viewController的viewControllerName組成規(guī)則:Ability的moduleName + Ability的abilityName + “viewController”。
StageApplication初始化支持以下兩種方式
在didFinishLaunchingWithOptions函數(shù)中進(jìn)行初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 配置hap包路徑
[StageApplication configModuleWithBundleDirectory:@"arkui-x"];
// 啟動(dòng)ability
[StageApplication launchApplication];
// APP自啟動(dòng),初始化StageViewController子類VC,并設(shè)置為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直接拿`
在openURL回調(diào)函數(shù)中進(jìn)行初始化
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 不在此處進(jìn)行初始化
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
-
控制器
+關(guān)注
關(guān)注
112文章
16367瀏覽量
178122 -
鴻蒙
+關(guān)注
關(guān)注
57文章
2352瀏覽量
42863 -
OpenHarmony
+關(guān)注
關(guān)注
25文章
3722瀏覽量
16323
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論