目前高德地图IOS SDK文档是用Object-C的,Swift需要桥接调用,我想开发一个uts标准模式组件,调用高德地图,IOS平台是Object-C的话,那UTS要怎么完成桥接呢
demo已在附件
j***@163.com
- 发布:2026-01-22 20:47
- 更新:2026-01-26 14:07
- 阅读:210
该问题请在专业群( uni-app 官方技术交流群 1 ) 咨询,群中有相关专业的管理员和群友。
在 iOS 平台用 UTS 标准模式组件调用「仅提供 Objective-C 接口」的高德地图 SDK,大体分三步:把 OC SDK 变成 Swift 可见 → 用 UTS 语言把 Swift 再包一层 → 在 uni_modules 中暴露给前端。下面给出可直接落地的步骤与示例。
-
让 Objective-C SDK 能被 Swift 看到
- 把高德 SDK(MAMapKit.framework、AMapFoundationKit.framework 等)拖进 uni-app 原生插件目录
uni_modules/your-plugin/ios/SDK/ - 在 ios 目录新建 module.modulemap(没有就建)
module AMapBridge [system] { header "AMapBridge.h" export * } -
新建 AMapBridge.h / AMapBridge.m 做一层极简 Objective-C 封装,把 SDK 里「只有 OC 的接口」转成 Swift 可以直接调用的类/方法。
例:高德地图反地理编码只有 OC 的 AMapSearchKit,封装一个 Swift 可见的类// AMapBridge.h #import <Foundation/Foundation.h> #import <AMapSearchKit/AMapSearchKit.h> NS_ASSUME_NONNULL_BEGIN @interface AMapBridge : NSObject + (void)reGeocodeWithLat:(double)lat lng:(double)lng callback:(void(^)(NSString *address, NSError *error))callback; @end NS_ASSUME_NONNULL_END// AMapBridge.m #import "AMapBridge.h" @implementation AMapBridge + (void)reGeocodeWithLat:(double)lat lng:(double)lng callback:(void(^)(NSString *address, NSError *error))cb { AMapSearchAPI *search = [[AMapSearchAPI alloc] init]; AMapReGeocodeSearchRequest *req = [[AMapReGeocodeSearchRequest alloc] init]; req.location = [AMapGeoPoint locationWithLatitude:lat longitude:lng]; [search AMapReGoecodeSearch:req callback:^(AMapReGeocodeSearchRequest *request, AMapReGeocodeSearchResponse *response, NSError *error) { cb(response.regeocode.address, error); }]; } @end
- 把高德 SDK(MAMapKit.framework、AMapFoundationKit.framework 等)拖进 uni-app 原生插件目录
-
用 UTS 再包一层(uni_modules/your-plugin/ios/index.uts)
UTS 文件里直接 import Swift 可见的 AMapBridge,再导出前端可调用的函数。// index.uts import Foundation // 把 OC 封装的类桥进来 @_silgen_name("AMapBridge_reGeocodeWithLat_lng_callback") external fun AMapBridge_reGeocodeWithLat(lat: Double, lng: Double, callback: (NSString?, NSError?) -> Unit) export function reverseGeo(options: { latitude: number, longitude: number }): Promise<string> { return new Promise((resolve, reject) => { AMapBridge_reGeocodeWithLat( lat = options.latitude, lng = options.longitude, callback = { address, error in if let err = error { reject(err.localizedDescription) } else { resolve(address ?? "") } } ) }) }注意:
- 如果 OC 方法带 delegate,需要在 OC 封装层把 delegate 转成 block,再让 UTS 调用。
- 如果 OC 方法参数没有标签(如
reGeocode: CLLocation!),在 UTS 声明时要加@argumentLabel(""),详见 link。
-
前端调用
在 pages 里直接 import 插件:import { reverseGeo } from '@/uni_modules/your-plugin' reverseGeo({ latitude: 39.9, longitude: 116.3 }) .then(addr => console.log(addr))
常见坑 & 提示
- SDK 必须同时包含 arm64 / x86_64 切片,否则模拟器会 crash。
- 如果高德 SDK 需要配置 appkey,在 manifest.json → App模块配置 → Maps(地图) 里勾选「高德地图」并填写 key,配置文档。
- 若 OC 接口里出现
NS_OPTIONS、NS_ENUM,在 Swift 中会自动变成 OptionSet / Int,UTS 可直接用。 - 调试时先用 Xcode 打开
unpackage/debug/ios工程确认 Swift 能正常 import AMapBridge,再回 HBuilderX 跑自定义基座。
内容为 AI 生成,仅供参考