带你玩转ArkUI-X 调用原生 iOS 端代码通信
2025-02-01 00:46:05
238次阅读
0个评论
前言导读
各位同学,在这个2024年工作日的最后一天,就给各位分享一个ArkUI-X 调用原生 iOS 端代码通信 的案例。如果你正在使用 ArkUI-X 开发跨平台应用那么相信这个案例对你大有帮助,我们废话不多说正式开始
效果图
通过观察界面变化我们可以看到 我们通过和ios的oc,代码交互我们可以从arkui端调用 oc的方法也可以通过 oc 调用我们的arkui代码
具体实现
-
arkui 端
// 导入平台桥接模块
import bridge from '@arkui-x.bridge';
-
创建平台桥接对象
// 创建平台桥接对象
private bridgeImpl = bridge.createBridge('Bridge');
-
调用ios端逻辑
// 发送数据到iOS侧,并通过状态变量,
//将iOS侧的响应数据显示在页面上
await this.bridgeImpl.sendMessage('Hello ArkUI-X!').then((data)=>{
this.nativeResponse=data?.toString();
})
-
接收ios 端回调逻辑
getHelloArkUI() {
// 调用iOS侧方法
this.bridgeImpl.callMethod('getHelloArkUI').then((result) => {
// 通过状态变量,将iOS侧方法的返回值显示在页面上
this.helloArkUI = result?.toString();
});
}
完整代码
// Index.ets
// 导入平台桥接模块
import bridge from '@arkui-x.bridge';
@Entry
@Component
struct Index {
// 创建平台桥接对象
private bridgeImpl = bridge.createBridge('Bridge');
@State helloArkUI: string|undefined= '';
@State nativeResponse: string|undefined = '';
aboutToAppear() {
this.getHelloArkUI();
}
getHelloArkUI() {
// 调用iOS侧方法
this.bridgeImpl.callMethod('getHelloArkUI').then((result) => {
// 通过状态变量,将iOS侧方法的返回值显示在页面上
this.helloArkUI = result?.toString();
});
}
build() {
Row() {
Column() {
Text(this.helloArkUI)
.fontSize(15)
.margin(10)
Button('sendMessage')
.fontSize(15)
.margin(10)
.onClick(async () => {
// 发送数据到iOS侧,并通过状态变量,将iOS侧的响应数据显示在页面上
await this.bridgeImpl.sendMessage('Hello ArkUI-X!').then((data)=>{
this.nativeResponse=data?.toString();
})
})
Text('Response from Native: ' + this.nativeResponse)
.fontSize(15)
.margin(10)
}
.width('100%')
}
.height('100%')
}
}
iOS端代码
-
创建插件类 BridgeClass
-
.h文件
// BridgeClass.h
// 引用平台桥接模块
#import <libarkui_ios/BridgePlugin.h>
NS_ASSUME_NONNULL_BEGIN
@interface BridgeClass : BridgePlugin
- (NSString*)getHelloArkUI;
@end
NS_ASSUME_NONNULL_END
-
.m 文件
// BridgeClass.m
#import "BridgeClass.h"
// iOS侧方法,供ArkUI侧调用
@implementation BridgeClass
- (NSString*)getHelloArkUI {
return @"Hello ArkUI!";
}
@end
-
注册插件建立平台桥接
// 建立与ArkUI侧同名的平台桥接,即可用于消息传递
// 创建平台桥接实例(将在since 13废弃,推荐使用新构造方法)
// self.plugin = [[BridgeClass alloc] initBridgePlugin:
//@"Bridge" instanceId:instanceId];
// 创建平台桥接实例
self.plugin = [[BridgeClass alloc] initBridgePlugin:@"Bridge"
bridgeManager:[mainView getBridgeManager]];
self.plugin.messageListener = self;
-
监听ArkUI侧发来的消息
// 监听ArkUI侧发来的消息
#pragma mark - listener
- (NSString*)onMessage:(id)data {
NSLog(@"data 原生鸿蒙传递过来的数据 %@",data);
return @"oc onMessage success";
}
完整代码
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#import "AppDelegate.h"
#import "EntryEntryAbilityViewController.h"
#import <libarkui_ios/StageApplication.h>
#import <libarkui_ios/StageApplication.h>
#import <libarkui_ios/BridgePlugin.h>
#import "BridgeClass.h"
#define BUNDLE_DIRECTORY @"arkui-x"
#define BUNDLE_NAME @"com.example.bridgestage"
#define BUNDLE_DIRECTORY @"arkui-x"
#define BUNDLE_NAME @"com.example.arkuitoios"
@interface AppDelegate ()
<IMessageListener> {}
@property (nonatomic, strong) BridgeClass* plugin;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[StageApplication configModuleWithBundleDirectory:BUNDLE_DIRECTORY];
[StageApplication launchApplication];
NSString *instanceName = [NSString stringWithFormat:@"%@:%@:%@",BUNDLE_NAME, @"entry", @"EntryAbility"];
EntryEntryAbilityViewController *mainView = [[EntryEntryAbilityViewController alloc]
initWithInstanceName:instanceName];
[self setNavRootVC:mainView];
// 建立与ArkUI侧同名的平台桥接,即可用于消息传递
// 创建平台桥接实例(将在since 13废弃,推荐使用新构造方法)
// self.plugin = [[BridgeClass alloc] initBridgePlugin:
//@"Bridge" instanceId:instanceId];
// 创建平台桥接实例
self.plugin = [[BridgeClass alloc] initBridgePlugin:@"Bridge"
bridgeManager:[mainView getBridgeManager]];
self.plugin.messageListener = self;
return YES;
}
// 监听ArkUI侧发来的消息
#pragma mark - listener
- (NSString*)onMessage:(id)data {
NSLog(@"data 原生鸿蒙传递过来的数据 %@",data);
return @"oc onMessage success";
}
- (void)onMessageResponse:(id)data {
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
NSLog(@"appdelegate openUrl callback, url : %@", url.absoluteString); // eg: (com.entry.arkui://entry?OtherAbility)
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;
}
}
[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 {
id rootVC = [[UIApplication sharedApplication].delegate window].rootViewController;
BOOL hasRoot = NO;
if ([rootVC isKindOfClass:[UINavigationController class]]) {
hasRoot = YES;
}
id subStageVC = nil;
if ([moduleName isEqualToString:@"entry"] && [abilityName isEqualToString:@"EntryAbility"]) {
NSString *instanceName = [NSString stringWithFormat:@"%@:%@:%@",bundleName, moduleName, abilityName];
EntryEntryAbilityViewController *otherVC = [[EntryEntryAbilityViewController alloc] initWithInstanceName:instanceName];
subStageVC = (EntryEntryAbilityViewController *)otherVC;
} else if ([moduleName isEqualToString:@"entry"] && [abilityName isEqualToString:@"EntryAbility"]) {
NSString *instanceName = [NSString stringWithFormat:@"%@:%@:%@",bundleName, moduleName, abilityName];
EntryEntryAbilityViewController *otherVC = [[EntryEntryAbilityViewController alloc] initWithInstanceName:instanceName];
subStageVC = (EntryEntryAbilityViewController *)otherVC;
} // other ViewController
if (!subStageVC) {
return NO;
}
if (!hasRoot) {
[self setNavRootVC:subStageVC];
} else {
UINavigationController *rootNav = (UINavigationController *)self.window.rootViewController;
[rootNav pushViewController:subStageVC animated:YES];
}
return YES;
}
- (void)setNavRootVC:(id)viewController {
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
UINavigationController *navi = [[UINavigationController alloc]initWithRootViewController:viewController];
[self setNaviAppearance:navi];
self.window.rootViewController = navi;
}
- (void)setNaviAppearance:(UINavigationController *)navi {
UINavigationBarAppearance *appearance = [UINavigationBarAppearance new];
[appearance configureWithOpaqueBackground];
appearance.backgroundColor = UIColor.whiteColor;
navi.navigationBar.standardAppearance = appearance;
navi.navigationBar.scrollEdgeAppearance = navi.navigationBar.standardAppearance;
}
@end
最后总结:
如果是你正在使用ArkUI-X开发自己的跨平台项目,在开发过程中遇到arkui 实现的点时候,例如获取iphone 设备的唯一标识idfa 我们就需要用桥接的方式去实现,来达到我们的跨平台开发目的,arkui-x 刚起步我们也希望有更多开发者加入进来共同打造一个更好的鸿蒙生态。有更新多想学习的知识点可以关注我的文章和坚果派社区。
来源:坚果派
著作权归作者所有,禁止任何未经授权的个人或组织以任何形式将本案例集及其附属资料、创新、创意、架构设计、算法、衍生作品等用于任何商业目的、盈利活动、各类竞赛(比赛)、直播教学、录播教学、线下课程、书籍编写、教材编写、会议、培训、公益活动、项目课题、毕业设计、毕业论文、学术论文等。商业转载请联系作者获得授权,非商业转载请注明出处。否则追究相关责任。
00
- 0回答
- 4粉丝
- 0关注
相关话题