Search code examples
iostoday-extension

send data displayed in today notification(Today extension) to appdelegate method


I am using today extension

I have displayed the list of events in tableview today notification.

while click on selected row event i want to send in appdelegate method

when click select row I am navigating in my app and call method openurl but i cann't get selected event in this method or selected row number.

so can we get data from today extension to our app

my current code in todayviewcotroller is

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
      NSLog(@"%s",__PRETTY_FUNCTION__);
     [self.extensionContext openURL:[NSURL URLWithString:@"TestIt://"]
             completionHandler:^(BOOL success) {
     }];
}

when click on event send row number to appdelegate method(openurl).

appriciate for help


Solution

  • You can use NSUserDefaults to store the data between the app and the extensions. But firstly you need to enable app groups.

    To enable data sharing, use Xcode or the Developer portal to enable app groups for the containing app and its contained app extensions. Next, register the app group in the portal and specify the app group to use in the containing app. To learn about working with app groups, see Adding an App to an App Group in Entitlement Key Reference.

    After you enable app groups, an app extension and its containing app can both use the NSUserDefaults API to share access to user preferences. To enable this sharing, use the initWithSuiteName: method to instantiate a new NSUserDefaults object, passing in the identifier of the shared group. For example, a Share extension might update the user’s most recently used sharing account, using code like this:

    // Create and share access to an NSUserDefaults object.

    NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.example.domain.MyShareExtension"];
    

    // Use the shared user defaults object to update the user's account.

    [mySharedDefaults setObject:theAccountName forKey:@"lastAccountName"];
    

    Here's the reference: https://developer.apple.com/library/mac/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

    Edit: How to register yourself to a userdefaults notification

     NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self
               selector:@selector(defaultsChanged:)  
                   name:NSUserDefaultsDidChangeNotification
                 object:nil];