4

I've built a today widget and I want to show what version is running in my host app's about screen. Is there a way to do that?

I've tried replacing [NSBundle mainBundle] with bundleWithIdentifier but that fails. So, is there a way to get the infoDictionary from he extension bundle, or am I wasting my time?

Moshe
  • 57,511
  • 78
  • 272
  • 425
  • You can always use NSUserDefaults by suite name, `[[NSUserDefaults alloc] initWithSuiteName:@"group.com.mycompany.myapp"]` set the version in the main app and access it in the extension. – iphonic Apr 13 '15 at 18:03
  • That works if the extension was opened before the app. – Moshe Apr 13 '15 at 18:05

2 Answers2

5

You can pull this off by specifying the direct path to the extension's bundle. It looks like app extensions live in the PlugIns/ folder in your app's main bundle. You can create an instance of NSBundle by using -initWithPath:. For example:

- (NSBundle *)appExtensionBundle {
    NSString *plugInsPath = [NSBundle mainBundle].builtInPlugInsPath;
    NSString *appExtensionPath = [plugInsPath stringByAppendingPathComponent:@"MyTodayExtension.appex"];
    return [[NSBundle alloc] initWithPath:appExtensionPath];
}

NOTE: This code was tested only with a Today extension. You should test to make sure it works correctly if you have other types of extensions.

Grant Butler
  • 246
  • 2
  • 4
0

You can't. Application and Extension cant access each others code and address space. They can only share data using Sharing the container using AppGroup or by using Shared NSUserDefault.

Using both the approach will require to run your extension atleast once.

Arun Gupta
  • 2,628
  • 1
  • 20
  • 37