3

I'm writing a WatchKit extension and I'd like to read a file out of the host application's [NSBundle mainBundle]. I've tried [NSBundle bundleWithIdentifier:] but that just returns nil.

I have several potential workarounds, but nothing that would be as simple as "just read what you need from the host's mainBundle".

Is there a way of doing this?

Rawling
  • 49,248
  • 7
  • 89
  • 127

3 Answers3

2

The host app and your WatchKit extension can share files in only one of two ways, as far as I know:

  • Shared app group
  • Including a file in both targets

They run in separate processes and aren't accessible to each other outside of approved methods.

bgilham
  • 5,909
  • 1
  • 24
  • 39
2

I ran into a similar problem like yours. The main host app has a particular pList that I needed to read, and I couldn't read from watch extension because they are isolated.

So in the watch I invoked the openParentApplication method

and in the main application my handler was something along the lines of

-(void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply
{
        NSString *request = [userInfo objectForKey:@"request"];
        if ([request isEqualToString:ReadFile])
        {
              //read the file. and then i like to put it into a NSDictionary
                NSDictionary *responseDictionary = //whatever
                 reply(responseDictionary);
        }
        else{ reply(nil); }
}

And then the contents were returned to me in the callback closure on the watch of the openParentApplication. Seems to work. Though your situation could be different in which case this method might not be viable.

prawn
  • 2,643
  • 2
  • 33
  • 49
  • Yes, this is what I was doing originally - I had my whole calculation in the host application. However, starting the host app causes me real problems elsewhere so I've had to look for an alternative. – Rawling Apr 23 '15 at 06:35
0

From the Apple WatchKit programming guide:

To share preferences data between apps, create an NSUserDefaults object using the identifier of the shared group. The initWithSuiteName: method of NSUserDefaults creates an object that allows access to the shared user defaults data. Both processes can access this data and write changes to it.

Your main app can write a NSDictionary/NSArray to the shared prefs, and then the watch kit can pull it out, without starting the main app - however, the main app will have to be run at least once to update the shared prefs.

cwgso
  • 401
  • 3
  • 9