9***@qq.com
9***@qq.com
  • 发布:2026-03-04 15:06
  • 更新:2026-03-04 15:07
  • 阅读:17

我现在有一个封装好的xcframework,我如何把它集成到一个uts组件中,并在页面中调用其中的的方法

分类:uts
2026-03-04 15:06 负责人:无 分享
已邀请:
Ask小助手

Ask小助手 - Ask社区AI助手

欢迎到专业群( uni-app 官方技术交流群 1 ) 咨询,群中有相关专业的管理员和群友。

要将一个封装好的 .xcframework 集成到 uts 插件中,并在页面中调用其方法,需按以下步骤操作:

一、准备 uts 插件结构

uni_modules/你的插件名/utssdk/app-ios/ 下创建以下文件:

utssdk/app-ios/  
├── config.json          // 系统库配置  
├── index.uts            // 插件入口  
└── Frameworks/  
    └── YourSDK.xcframework   // 放置你的 xcframework

二、配置系统依赖(config.json)

config.json 中添加 .xcframework 依赖的系统库,例如:

{  
  "frameworks": [  
    "YourSDK.xcframework",  
    "CoreTelephony.framework",  
    "AVFoundation.framework"  
  ]  
}

如果 .xcframework 依赖其他系统库,一并列出。

三、编写 uts 插件入口(index.uts)

// index.uts  
export function callNativeMethod(options: any) {  
  // 调用原生方法  
  return nativeCall(options)  
}

四、原生端实现(Objective-C)

utssdk/app-ios/ 下创建 YourPlugin.m

#import <Foundation/Foundation.h>  
#import "DCUniModule.h"  
#import <YourSDK/YourSDK.h>  // 引入 xcframework 头文件  

@interface YourPlugin : DCUniModule  
@end  

@implementation YourPlugin  

// 暴露方法给 uts  
UNI_EXPORT_METHOD(@selector(callNativeMethod:callback:))  

- (void)callNativeMethod:(NSDictionary *)options callback:(UniModuleKeepAliveCallback)callback {  
    // 调用 xcframework 中的方法  
    NSString *result = [[YourSDK shared] doSomethingWithOptions:options];  
    callback(@{@"result": result}, NO);  
}  

@end

五、配置插件信息

utssdk/app-ios/ 下创建 info.plist(或修改现有):

<key>dcloud_uniplugins</key>  
<array>  
  <dict>  
    <key>class</key>  
    <string>YourPlugin</string>  
    <key>name</key>  
    <string>your-plugin</string>  
    <key>type</key>  
    <string>module</string>  
  </dict>  
</array>

六、页面中调用

在页面中通过 uts 插件调用:

<template>  
  <button @click="test">调用原生方法</button>  
</template>  

<script setup>  
import { callNativeMethod } from '@/uni_modules/your-plugin/utssdk/index.uts'  

function test() {  
  callNativeMethod({ key: 'value' }).then(res => {  
    console.log('原生返回:', res.result)  
  })  
}  
</script>

七、注意事项

  • .xcframework 需放在 Frameworks/ 目录下,并在 config.json 中声明。
  • 如果 .xcframework 包含 Swift 代码,需确保项目支持 Swift。
  • 云打包时确保 .xcframework 被正确打包,可在 插件市场 搜索类似插件参考。

如需进一步调试,可参考 uts 插件开发文档

内容为 AI 生成,仅供参考

要回复问题请先登录注册