Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to communicate between iOS App Containing Extension and Extension (not Host App)

TLDR: Is it possible to send realtime messages or notifications between iOS App and it's Extension?

I'm writing an iOS App with an extension that are part of the same App Group and share the same CoreData (SQLite database). I can read and write to the database using CoreData from the App and from the extension, they both share the same content.

My Question is: Is it possible to send messages or notifications between the App and the extension to notify the other to update if necessary?

I tried sending notifications through NSNotificationCenter but that does not go "out" of the App/Extension, same issue if I try to write to the group shared NSUserDefaults and listen to NSUserDefaultsDidChangeNotification. This works inside the App but the extension does not receive anything (when I know that it is launched and it share the same NSUserDefaults). Any idea how to keep things in sync?

like image 951
Ludovic Landry Avatar asked Nov 19 '14 08:11

Ludovic Landry


People also ask

Can iOS apps talk to each other?

Share All sharing options for: iOS 8 apps can talk to each other. Apple is adding one of the most-requested features to iOS 8: inter-app communication.

How do app extensions work iOS?

You create an app extension by adding a new target to an app. As with any target, an extension target specifies settings and files that combine to build a product within your app project. You can add multiple extension targets to a single app (an app that contains one or more extensions is called a containing app).

What extension does iOS apps use?

ipa. An . ipa (iOS App Store Package) file is an iOS application archive file which stores an iOS app.

How do app extensions work?

App extensions showcase your mobile or tablet app by showing a link to your app below your ad. Clicking this link leads you to your app's description in the app store (Google Play or the Apple App Store). Clicking on your ad's headline will still lead to your website.


1 Answers

TLDR: No, but there's a hack

There's no true interprocess communication for iOS apps, with or without extensions. NSDistributedNotification still hasn't made the trip from OS X to iOS, and probably won't.

With some extension types you can open URLs via NSExtensionContext and use them to pass data to an app that handles the URL. This brings the app to the foreground, which doesn't sound like what you want.

There is a hack that might get you what you need, though.

  • Instead of writing to user defaults, write to a file in your app group directory.
  • Don't just write the file directly-- use NSFileCoordinator to do coordinated writes to the file.
  • Implement NSFilePresenter on an object that wants to know about changes to the file, and make sure to call [NSFileCoordinator addFilePresenter:someObject]
  • Implement the optional presentedItemDidChange method on your file presenter.

If you do all of this right, you can write to this file from either the app or the extension, and then have presentedItemDidChange be automatically called in the other one. As a bonus you can of course read the contents of that file, so you can pass arbitrary data back and forth.

like image 148
Tom Harrington Avatar answered Oct 07 '22 16:10

Tom Harrington