Home > Software design >  How to open my app from activity view controller and retrieve data in the opened app?
How to open my app from activity view controller and retrieve data in the opened app?

Time:12-07

I need to share a JSON file from one of my apps to another one of my apps.

User experience I want to have

  • I want to open the activity view controller
  • select my other app from the horizontal list of apps
  • have the selected app open and receive the sent JSON file

I tried the share extension, but I want to open the app from activity controller and handle the data there

CodePudding user response:

Figured it out

On the app that you plan on RECEIVING the data with...

  • Go to your project name under your Target > Info > Document types > " " button.

  • Add the name of the file type in the "name" field

  • Add the correct UTI (Uniform Type Identifier) in the "Types" field. List of known UTIs

  • Run the app

The app should now appear in ActivityViewControllers for JSON file types

Now we will go through how to receive data from the ActivityViewController app

On the app that you plan on RECEIVING the data with...

  • go to the scene delegate

  • add this function

      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)")
          }
      }
    

    }

I believe that's all we need. SomeCodableClass is a class you will need to create to reflect the JSON in the file.

This tutorial helped me with this problem

For custom types, follow this tutorial

  • Related