Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios8 extension and container app how to share data

I'm having trouble to share data between iOS 8 extension and container app. I set up the group from develop portal and Xcode, and my entitlements plist both look like:

<dict>
    <key>com.apple.security.application-groups</key>
    <array>
        <string>my group id</string>
    </array>
</dict>

The suggested solution in apple document to use [[NSUserDefaults standardUserDefaults]

doesn't work for me.

I've tried the solution by @Santa Claus from this post Communicating and persisting data between apps with App Groups to use

[[NSUserDefaults alloc] initWithSuiteName:@"<group identifier>"];

Santa Claus's solution worked on simulator but not on my iPhone 4s, any ideas on how to make it work on real device? Or am I missing anything?

Some updates:

[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group id"];

function returns url, and both urls from container app and extension look the same, as "path /private/var/mobile/Containers/Shared/AppGroup/3DFB9750-A9F3-4C37-BC3C-0D5BF746B‌​233". I created a json file inside that folder from container app but extension can't find any file in the folder when I run on device. But in simulator it finds it well and everything works.

like image 656
woof Avatar asked Jun 05 '14 18:06

woof


People also ask

Can I share data between an app and its app extension?

Even though an app extension bundle is nested within its containing app’s bundle, the running app extension and containing app have no direct access to each other’s containers. To learn about containers, read About the iOS File System in File System Programming Guide . You can, however, enable data sharing.

How do I coordinate shared data access between iOS apps?

In iOS 8.2 and later, you can alternatively employ the UIDocument class to coordinate shared data access. In iOS 9 and later, you can employ the NSFileCoordinator class directly for shared data access, but if you do this you must remove your NSFilePresenter objects when your app extension transitions into the background.

How do I prevent data corruption in a shared iOS container?

To avoid data corruption, you must synchronize data accesses. Use Core Data, SQLite, or Posix locks to help coordinate data access in a shared container. In iOS 8.2 and later, you can alternatively employ the UIDocument class to coordinate shared data access.

How do I synchronize data accesses in a shared container?

When you set up a shared container, the containing app—and each contained app extension that you allow to participate in data sharing—have read and write access to the shared container. To avoid data corruption, you must synchronize data accesses. Use Core Data, SQLite, or Posix locks to help coordinate data access in a shared container.


2 Answers

My solution is edit the Info.plist of my custom keyboard:

NSExtension -> NSExtensionAttributes -> RequestsOpenAccess

Make it YES.

Then delete the keyboard in your device and re-add it. In your keyboard add page you will find a setting named "Allow Full Access", enable it and the keyboard will access the shared data.

like image 184
heheBear Avatar answered Oct 01 '22 21:10

heheBear


For keyboard extension, make sure enable RequestsOpenAccess as heheBear's answer
and try remove and add back keyboard from Setting/Keyoboard occasionally
keyboard extension is not stable and performance is really bad since beta 4, include beta 5

like image 41
Ethan Liu Avatar answered Oct 01 '22 22:10

Ethan Liu