Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将今天通知(今天扩展名)中显示的数据发送到appdelegate方法_Ios_Today Extension - Fatal编程技术网

Ios 将今天通知(今天扩展名)中显示的数据发送到appdelegate方法

Ios 将今天通知(今天扩展名)中显示的数据发送到appdelegate方法,ios,today-extension,Ios,Today Extension,我今天用的是分机 我已在tableview today通知中显示了事件列表 单击要在appdelegate方法中发送的选定行事件时 单击“选择我在应用程序中导航的行”并调用方法openurl时 但我无法获取此方法中的选定事件或选定的行号 那么,我们可以从今天的应用程序扩展中获取数据吗 我在todayviewcotroller中的当前代码是 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath

我今天用的是分机

我已在tableview today通知中显示了事件列表

单击要在appdelegate方法中发送的选定行事件时

单击“选择我在应用程序中导航的行”并调用方法openurl时 但我无法获取此方法中的选定事件或选定的行号

那么,我们可以从今天的应用程序扩展中获取数据吗

我在todayviewcotroller中的当前代码是

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  {
      NSLog(@"%s",__PRETTY_FUNCTION__);
     [self.extensionContext openURL:[NSURL URLWithString:@"TestIt://"]
             completionHandler:^(BOOL success) {
     }];
}
单击事件时,将行号发送到appdelegate方法(openurl)


appriciate for help

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

要启用数据共享,请使用Xcode或开发者门户为包含的应用及其包含的应用扩展启用应用组。接下来,在门户中注册应用程序组,并指定要在包含的应用程序中使用的应用程序组。要了解有关使用应用程序组的信息,请参阅“授权密钥参考”中的“将应用程序添加到应用程序组”

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

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"TestIt://%d", indexPath.row];
[self.extensionContext openURL:url] completionHandler:nil];
- (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;
}
//创建并共享对NSUserDefaults对象的访问权限

NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.example.domain.MyShareExtension"];
//使用共享用户默认值对象更新用户帐户

[mySharedDefaults setObject:theAccountName forKey:@"lastAccountName"];
以下是参考资料:

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

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

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

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"TestIt://%d", indexPath.row];
[self.extensionContext openURL:url] completionHandler:nil];
- (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;
}
现在,URL包含用户点击的行的索引

然后确保您的应用程序响应该URL(您在目标设置中执行此操作),并且您将在应用程序代理中收到该URL。您可以这样做:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"TestIt://%d", indexPath.row];
[self.extensionContext openURL:url] completionHandler:nil];
- (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;
}
这样,您的应用程序将知道用户点击了哪一行,并且可以以任何对您的应用程序有意义的方式进行响应。有一个例子可以证明这一点


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

我已从我的应用程序和扩展中启用了应用程序组,并将nsuserdefault与initwithsuitname方法一起使用,并尝试正常运行应用程序,但当通知中的单击事件不调用todayviewcontroller中的方法时,每当我删除调用的nsuserdefault代码方法时,我不知道为什么@Ashraf Tawfeeqa在UserDefaultsIDChange通知中为自己添加一个观察者