如何托管应用程序在IOS操作扩展中将数据发送到应用程序扩展

时间:2015-02-09 05:33:48

标签: ios ios8 ios8-extension

我已经完成了示例动作扩展应用。我无法获取主机应用程序请求数据。我认为此代码存在一些问题。

主机应用程序请求代码

  NSDictionary *request = @{@"username”:@“username”,@“password”:@“password”};
            UIActivityViewController *extensionController = [[UIActivityViewController alloc] initWithActivityItems:@[request] applicationActivities:nil];
           extensionController.popoverPresentationController.sourceView = self.view;
        [self presentViewController:extensionController animated:YES completion:nil];

应用扩展程序代码

NSExtensionItem *inputItem = self.extensionContext.inputItems[0];

    NSItemProvider *itemprovider = inputItem.attachments[0];

    [itemprovider loadItemForTypeIdentifier:(NSString *)kUTTypePropertyList options:nil completionHandler:^(NSDictionary *item, NSError *error) {

        NSDictionary *results = (NSDictionary *)item;

        NSLog(@“Host app Request Data=%@",results);

    }];

我不确定上面的代码是什么错误,任何人都可以帮助我..谢谢

1 个答案:

答案 0 :(得分:1)

启用应用程序组并使用 NSUserDefaults

  1. 启用应用程序组并在主机目标和扩展目标功能中添加组名

  2. 在主机应用源中输入设置数据

  3. 
        //use our group user defaults
        NSUserDefaults *defaults = [[NSUserDefaults alloc]  
        initWithSuiteName:@"group.com.organization.ProductName"];
    
        //set a greeting
        [defaults setObject:@"username" forKey:@"username"];
        [defaults setObject:@"password" forKey:@"password"];
    
        //synchronise
        [defaults synchronize];
    
    
    1. SomeExtension.m
    2. 
          //use our group user defaults
          NSUserDefaults *defaults = [[NSUserDefaults alloc] 
          initWithSuiteName:@"group.com.organization.ProductName"];
      
          //get the greeting    
          NSString *username = [defaults stringForKey:@"username"];
          NSString *password = [defaults stringForKey:@"password"];
      
          //check if greeting isn't empty
          NSLog(@"username = %@ / password = %@", username, password);