1

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

4

2 回答 2

2

您可以使用 NSUserDefaults 在应用程序和扩展程序之间存储数据。但首先您需要启用应用程序组。

要启用数据共享,请使用 Xcode 或开发人员门户为包含应用程序及其包含的应用程序扩展启用应用程序组。接下来,在门户中注册应用程序组并指定要在包含应用程序中使用的应用程序组。要了解如何使用应用程序组,请参阅权利密钥参考中的将应用程序添加到应用程序组。

启用应用组后,应用扩展及其包含的应用都可以使用 NSUserDefaults API 来共享对用户首选项的访问权限。要启用此共享,请使用 initWithSuiteName: 方法实例化一个新的 NSUserDefaults 对象,并传入共享组的标识符。例如,共享扩展程序可能会更新用户最近使用的共享帐户,使用如下代码:

// 创建和共享对 NSUserDefaults 对象的访问。

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

// 使用共享用户默认对象更新用户帐户。

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

这是参考:https ://developer.apple.com/library/mac/documentation/General/Conceptual/ExtensibilityPG/ExtensionScenarios.html#//apple_ref/doc/uid/TP40014214-CH21-SW1

编辑:如何将自己注册到 userdefaults 通知

 NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
           selector:@selector(defaultsChanged:)  
               name:NSUserDefaultsDidChangeNotification
             object:nil];
于 2015-02-26T09:45:02.887 回答
0

只要确保在 URL 中包含重要数据,您就可以使用 URL 执行此操作。例如,您可以将代码更改为如下所示:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"TestIt://%d", indexPath.row];
[self.extensionContext openURL:url] completionHandler:nil];

现在 URL 包含用户点击的行的索引。

然后确保您的应用程序响应该 URL(您在目标设置中执行此操作)并且您将在您的应用程序委托中收到该 URL。你会做这样的事情:

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    NSString *tableIndexRow = [url resourceSpecifier];

    // tableIndexRow is a string that contains the tapped row number

    // Do something to handle the tap

    return YES;
}

这样,您的应用程序将知道用户点击了哪一行,并且它可以以任何对您的应用程序有意义的方式做出响应。Github 上有一个项目可以证明这一点。

如果您需要传递不同的数据,请在 URL 中包含您需要的任何内容,并修改您的应用程序委托以处理该数据。

于 2015-02-26T17:44:12.177 回答