Sharing File Data Between Applications in Swift/Ios

Sharing data between apps on iOS

You can have a common file space between apps by using app groups. An example of how to use them can be found here: Sharing data in between apps in IOS

You can use this as part of a solve for not duplicating the data in every bundle. One way might be to have the data hosted on a server somewhere and when the app is installed you can check the App Group for the common data, if it is not there, you can download it and store it there. Then the next app that is installed will have the data already available. This should help avoid having to include it in every small app.

You can set up the code to check the shared location and download the data in a framework and share it between all your apps making it a bit easier to maintain. If you do not already have a content management system then you could google for a few that have iOS support. There are many out there. You would then host the shared data there. This would give you the ability to update the data for each app while they are in the field which could be a time saver. If these apps are very small though, this may be overkill.

Passing data between an iOS app on different devices

Based on what you have said,

You could just use the UIActivityViewController to export your data to AirDrop (or etc)
and using the exported UTI, your app could open it once it detected.

for Sharing, you will have to use a custom data format (or file) and you can refer to this post, Sending custom data via UIActivityViewController

and for reading your custom file, here is a good tutorial of how to implement the Exported UTI https://www.raywenderlich.com/8413525-universal-type-identifiers-tutorial-for-ios-importing-and-exporting-app-data

Cheers,
Kel

How to handle a file shared from another app to my own iOS app?

It looks like you followed the approach from this previous question only partially. In that case, the person had successfully implemented 2 of 3 parts of the solution for the custom URL scheme, so the answer only provided the 3rd part.

As https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app says:

To support a custom URL scheme:

  1. Define the format for your app’s URLs.
  2. Register your scheme so that the system directs appropriate URLs to your app.
  3. Handle the URLs that your app receives.

Their question was:

My app is being displayed in the available applications to which I can send the file to. My question is what happens after? When I choose to send it to my app it then switches over to my app and from there, I don't know how to receive the file and read it to extract its content.

So they had already done steps 1 and 2 (so, their app was being displayed properly in the available applications, etc.).

In your case, it sounds like you haven't done steps 1 and 2 yet?

The other possibility is (also from https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app) if you are using Scenes, note that there are additional entry points to your app.

If your app has opted into Scenes, and your app is not running, the system delivers the URL to the scene(:willConnectTo:options:) delegate method after launch, and to scene(:openURLContexts:) when your app opens a URL while running or suspended in memory.

Something like

func scene(_ scene: UIScene, 
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {

// Determine who sent the URL.
if let urlContext = connectionOptions.urlContexts.first {

let sendingAppID = urlContext.options.sourceApplication
let url = urlContext.url
print("source application = \(sendingAppID ?? "Unknown")")
print("url = \(url)")

// Process the URL similarly to the UIApplicationDelegate example.
}

/*
*
*/
}

and as the Apple docs say, for the case the app is in the background, then implement something like

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>){
let url = URLContexts.first?.url
print("url = \(url)")

// Process the URL similarly to the UIApplicationDelegate example.
}

Sharing Data Between Applications with different App ID Prefix in iOS

No, it's not possible. Since the two apps are not part of the same app group they can not share any resources. The use of a common framework is irrelevant. The two apps will each have their own sandbox with no common area for the framework code to share.

The end result is each app using the framework will end up with its own copy of the cache.

Share data between ios applications

Unfortunately, app groups are indeed restricted to a single developer team only:

https://developer.apple.com/library/archive/documentation/Security/Conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW21

As far as I know, there is no native workaround as apps are sandboxed.

Sharing Data with AppGroup

  1. Save data to UserDefaults in your main App:
UserDefaults(suiteName: <your_app_group>)!.set("test", forKey: "test")

  1. Read data from UserDefaults in your Widget:
let testStr = UserDefaults(suiteName: <your_app_group>)!.string(forKey: "test")

If you want to save other types see:

  • How can I use UserDefaults in Swift?


Related Topics



Leave a reply



Submit