SwiftUI中ShareLink分享自定义结构体:AirDrop显示App选项问题
SwiftUI自定义结构体跨App分享问题
我正在SwiftUI中尝试通过ShareLink(底层为UIActivityViewController)在App的两个用户间分享自定义结构体。目前已完成以下配置:
- 创建自定义文档类型(Document Type)
- 注册导出类型标识符(Exported Type Identifier)
- 将结构体标记为
Transferable
现在可以将结构体保存到文件,但通过AirDrop等方式分享时,本App并未出现在应用活动选项中,无法直接接收分享内容。
相关代码
import SwiftUI import UniformTypeIdentifiers struct SwiftUIView: View { @State private var customStruct: CustomStruct = CustomStruct() var body: some View { ShareLink(item: customStruct, preview: SharePreview(customStruct.name)) } } struct CustomStruct: Codable { var name: String = "Test Example" var description: String = "Test" } extension CustomStruct: Transferable { static var transferRepresentation: some TransferRepresentation { CodableRepresentation(contentType: .customStruct) } } extension UTType { static var customStruct: UTType { UTType(exportedAs: "com.TestExample.CustomStruct") } } struct SwiftUIView_Previews: PreviewProvider { static var previews: some View { SwiftUIView() } }
配置详情
- 已完成Document Type配置,关联自定义类型
- 已完成Exported Type Identifier配置,标识符为
com.TestExample.CustomStruct - 当前分享面板未显示本App作为接收选项
内容的提问来源于stack exchange,提问作者Nimbahus
相关产品推荐
相关产品推荐

