Accessing the contents of the Watchkit mainBundle

I am writing a WatchKit extension and I would like to read a file from the main application [NSBundle mainBundle]

. I've tried [NSBundle bundleWithIdentifier:]

but this just returns nil

.

I have several possible workarounds, but nothing that is as simple as "just read what you need from the host mainBundle

."

Is there a way to do this?

+3


source to share


3 answers


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

  • General application group
  • Including the file for both purposes


They run in separate processes and are not available to each other outside of approved methods.

+2


source


I ran into a similar problem similar to yours. The main host application has a specific pList that I needed to read and I couldn't read from the clock extension because they are isolated.

So in hours I called the method openParentApplication

and in the main application my handler was something like strings



-(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 content was returned to me in the closing callback on the openParentApplication clock. Seems to work. Although your situation may be different, this method may not be practical in that case.

+2


source


From the Apple WatchKit Programming Guide:

To share customization data between applications, create an NSUserDefaults object using the shared group ID. The initWithSuiteName: NSUserDefaults method creates an object that allows access to default data for default users. Both processes can access this data and write changes to it.

Your main app can write an NSDictionary / NSArray for the generic prefs and then the clock set can pull it out without starting the main app, however the main app will need to start at least once to update the generic prefs.

0


source







All Articles