You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 12:10:50