0

I am trying to save a UIPrinter object like this.

-(IBAction)setPrinterInSettings:(id)sender{

  UIPrinterPickerController *picker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:self.SavedPrinter];
  [picker presentFromRect:self.settingsButton.frame inView:self.view animated:YES completionHandler:^(UIPrinterPickerController *controller, BOOL userDidSelect, NSError *err) {
    if (userDidSelect) {
      self.SavedPrinter = controller.selectedPrinter;
      NSLog(@"self.SavedPrinter == %@",self.SavedPrinter);

      [self saveCustomObject:self.SavedPrinter key:@"SavedPrinter"];

          [[NSUserDefaults standardUserDefaults] setObject:self.SavedPrinter forKey:@"SavedPrinter"];
          [[NSUserDefaults standardUserDefaults] synchronize];
    }

  }];

}

It crashes when I try to save at [[NSUserDefaults standardUserDefaults] setObject:self.SavedPrinter forKey:@"SavedPrinter"]; and I get this message as a error -[UIPrinter encodeWithCoder:]: unrecognized selector sent to instance 0x7fb204062760. I need it to remember what printer the user picked and remember it even when the app goes out of the background. If any one could help that would be great!

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Connor
  • 91
  • 11
  • Did you ask this earlier? Anyway, like in the previous question, you need to update this question with details about the crash. What is the error message? – rmaddy May 11 '16 at 22:54
  • Also, read the first few paragraphs of the documentation for `NSUserDefaults`. It tells you what the requirements are. `UIPrinter` doesn't meet those requirements. – rmaddy May 11 '16 at 22:55
  • @rmaddy Yes I did redo and delete my first question and I did look at the documentation and I know that you can not save UIPrinters with the NSUserDefaults. Thats why I am asking for help – Connor May 11 '16 at 23:04

2 Answers2

3

You can't store a UIPrinter instance in NSUserDefaults. What you can do is save the printer's URL and then when your app starts again and you need the UIPrinter, read the URL from NSUserDefaults and use UIPrinter printerWithURL:.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
1

You should use the url property to save in NSUserDefaults with setURL:forKey: .

Something like this:

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];     
[defaults setURL:[self.SavedPrinter url] forKey:@"printer.url"];
[defaults synchronize];

And when you need the same printer again, create another UIPrinter with printerWithURL: using the url you have stored.

NSPunk
  • 502
  • 4
  • 14