1

IOS Share Extension NSMutableArray addObject Unrecognized selector sent to insta...

 2 years ago
source link: https://www.codesd.com/item/ios-share-extension-nsmutablearray-addobject-unrecognized-selector-sent-to-instance.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

IOS Share Extension NSMutableArray addObject Unrecognized selector sent to instance

advertisements

After clicking post to the share dialog, the Host App(e.g. Safari) hangs up if arrSites variable is currently not empty. I can only store 1 object inside my arrSites variable. How can I addObject to my NSMutableArray variable?

Below is my implemented code and it generates an error in [arrSites addObject:dictSite] line.

- (void)didSelectPost
{
inputItem = self.extensionContext.inputItems.firstObject;

NSItemProvider *urlItemProvider = [[inputItem.userInfo valueForKey:NSExtensionItemAttachmentsKey] objectAtIndex:0];

if ([urlItemProvider hasItemConformingToTypeIdentifier:(__bridge NSString *)kUTTypeURL])
{
    NSLog(@"++++++++++ Attachment is a URL");
    [urlItemProvider loadItemForTypeIdentifier:(__bridge NSString *)kUTTypeURL options:nil completionHandler:^(NSURL *url, NSError *error)
    {
         if (error)
         {
             NSLog(@"Error occured");
         }
         else
         {
             NSMutableArray *arrSites;
             if ([sharedUserDefaults valueForKey:@"SharedExtension"]){
                 arrSites = [sharedUserDefaults objectForKey:@"SharedExtension"];
             }else{
                 arrSites = [[NSMutableArray alloc] init];
             }

             NSDictionary *dictSite = [NSDictionary dictionaryWithObjectsAndKeys:self.contentText, @"Text", url.absoluteString, @"URL",nil];

             [arrSites addObject:dictSite];
             [sharedUserDefaults setObject:arrSites forKey:@"SharedExtension"];
             [sharedUserDefaults synchronize];

             UIAlertController * alert=   [UIAlertController
                                           alertControllerWithTitle:@"Success"
                                           message:@"V7 Posted Successfully."
                                           preferredStyle:UIAlertControllerStyleAlert];
             UIAlertAction* ok = [UIAlertAction
                                  actionWithTitle:@"OK"
                                  style:UIAlertActionStyleDefault
                                  handler:^(UIAlertAction * action)
                                  {
                                      [UIView animateWithDuration:0.20 animations:^
                                       {
                                           self.view.transform = CGAffineTransformMakeTranslation(0, self.view.frame.size.height);
                                       }
                                                       completion:^(BOOL finished)
                                       {
                                           [self.extensionContext completeRequestReturningItems:nil completionHandler:nil];
                                       }];
                                  }];

             [alert addAction:ok];
             [self presentViewController:alert animated:YES completion:nil];
         }
     }];
}
}


Without memory allocation you can't add the object to array, use like

// allocate the memory of array in before
NSMutableArray *arrSites = [[NSMutableArray alloc] init];
if ([sharedUserDefaults valueForKey:@"SharedExtension"]){
    [arrSites addObjectsFromArray:[sharedUserDefaults objectForKey:@"SharedExtension"]];
}
[arrSites addObject:dictSite];




About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK