iosobjective-cios13uiwindowuiscenedelegate

'keyWindow' is deprecated: first deprecated in iOS 13.0 in Objective-C


This is my simple button action to show a XIB file which is actually a bar code scanner in Objective-C.

 - (IBAction)startCamera:(id)sender {
    BarcodeVC * controller = [[BarcodeVC alloc] initWithNibName:@"BarcodeVC" bundle:[NSBundle mainBundle]];
    
    //[self presentViewController:controller animated:YES completion:nil];
     UIWindow * currentwindow = [[UIApplication sharedApplication] keyWindow];
     [currentwindow.rootViewController presentViewController:controller animated:YES completion:nil];
  }

But unfortunately, a warning comes which is:

keyWindow is deprecated: first deprecated in iOS 13.0

I know that since iOS 13 supports the multiple scenes, but Is there any way to solve this in Objective-C? I have seen Swift versions, but I was unsuccessful with Objective-C.


Solution

  • You can use the window through AppDelegate class like..

    BarcodeScannerVC * controller = [[BarcodeScannerVC alloc] initWithNibName:@"BarcodeScannerVC" bundle:[NSBundle mainBundle]];
    
    //[self presentViewController:controller animated:YES completion:nil];
    UIWindow * currentwindow = [[UIApplication sharedApplication] delegate].window;
    [currentwindow.rootViewController presentViewController:controller animated:YES completion:nil];
    

    Here i changed to get currentwindow line only so just change it.

    UIWindow * currentwindow = [[UIApplication sharedApplication] delegate].window;