2

i have done sample action extension app. am not able to get host app request data.i think is there some issue with this code.

Host App request code

  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];

App Extension code

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);

    }];

am not sure what's wrong above code, can anyone help me.. thanks

venu
  • 2,971
  • 6
  • 40
  • 59

1 Answers1

1

Turn on App Groups and use NSUserDefaults

  1. Turn on App Groups and add group name in your both host target and extension target Capabilities

  2. Input set data in host app source


    //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

    //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);

김정윤
  • 11
  • 2
  • Do not provide link only answers as links may expire in future.Try to quote some relevant code or some part of the code that is useful in answering ques. – Anirudh Sharma May 14 '15 at 06:37
  • Never share any passwords or other sensible data in the `NSUserDefaults`. Use the [Keychain Services API](https://developer.apple.com/documentation/security/keychain_services) instead, which will encrypt it properly. – Hans Knöchel Jan 13 '19 at 13:24