【问题标题】:How to open my app from activity view controller and retrieve data in the opened app?如何从活动视图控制器打开我的应用程序并在打开的应用程序中检索数据?
【发布时间】:2022-01-10 04:36:16
【问题描述】:

我需要将我的一个应用程序中的 JSON 文件共享到我的另一个应用程序。

我想要的用户体验

  • 我要打开活动视图控制器
  • 从应用水平列表中选择我的其他应用
  • 打开所选应用并接收发送的 JSON 文件

我尝试了共享扩展,但我想从活动控制器打开应用并处理那里的数据

【问题讨论】:

    标签: ios xcode uiactivityviewcontroller


    【解决方案1】:

    想通了

    在您计划接收数据的应用上...

    • 在 Target > Info > Document types > "+" 按钮下转到您的项目名称。

    • 在“名称”字段中添加文件类型的名称

    • 在“类型”字段中添加正确的 UTI(统一类型标识符)。 List of known UTIs

    • 运行应用程序

    应用现在应该出现在 JSON 文件类型的 ActivityViewControllers 中

    现在我们将介绍如何从 ActivityViewController 应用接收数据

    在您计划接收数据的应用上...

    • 去现场委托

    • 添加此功能

        func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
        dump(URLContexts.first)
      
        for urlContext in URLContexts {
      
            do {
                let data = try Data(contentsOf:  urlContext.url)
                let obejct = try JSONDecoder().decode(SomeCodableClass.self, from: data)    
      
            } catch {
                print("line \(#line) file \(#file) error\(error)")
            }
        }
      

      }

    我相信这就是我们所需要的。 SomeCodableClass 是您需要创建的类以反映文件中的 JSON。

    This tutorial helped me with this problem

    For custom types, follow this tutorial

    【讨论】:

      猜你喜欢
      • 2020-09-20
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 2012-01-11
      相关资源
      最近更新 更多