2016-03-22 39 views
11

Sto lavorando con PushNotification sull'app iOS. Vorrei mostrare un UIalertcontroller quando l'app riceve una notifica.Come mostrare UIAlertController da Appdelegate

provo questo codice qui sotto nella AppDelegate:

[self.window.rootViewController presentViewController:alert animated:YES completion:nil]; 

Ma l'UIAlertcontroller sta mostrando nella radice View (Prima schermo) e per altri UIViewController I Got avvertimento o l'applicazione si blocca.

+0

ciò che il rapporto creash –

+0

Ma l'UIAlertcontroller sta mostrando nella radice View .... naturalmente si sta aggiungendo l'allarme al controllore principale. Ovviamente si bloccherà su altri controller uiview perché si sta tentando di aggiungere un avviso sul controller che non è mostrato all'utente. –

+1

Sì, so che sto aggiungendo uialertcontroller al rootView e non alla vista attiva e la mia domanda è come posso mostrare l'uialertController nell'altro controllore uiview quando viene ricevuta la notifica. – Rockers23

risposta

42

provare questo

Objective-C

UIWindow* topWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; 
topWindow.rootViewController = [UIViewController new]; 
topWindow.windowLevel = UIWindowLevelAlert + 1; 

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"APNS" message:@"received Notification" preferredStyle:UIAlertControllerStyleAlert]; 

[alert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"OK",@"confirm") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { 
    // continue your work 

// important to hide the window after work completed. 
// this also keeps a reference to the window until the action is invoked. 
topWindow.hidden = YES; 
}]]; 

[topWindow makeKeyAndVisible]; 
[topWindow.rootViewController presentViewController:alert animated:YES completion:nil]; 

Swift3

let topWindow = UIWindow(frame: UIScreen.main.bounds) 
topWindow.rootViewController = UIViewController() 
topWindow.windowLevel = UIWindowLevelAlert + 1 
let alert = UIAlertController(title: "APNS", message: "received Notification", preferredStyle: .alert) 
alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "confirm"), style: .cancel, handler: {(_ action: UIAlertAction) -> Void in 
    // continue your work 
    // important to hide the window after work completed. 
    // this also keeps a reference to the window until the action is invoked. 
    topWindow.isHidden = true 
})) 
topWindow.makeKeyAndVisible() 
topWindow.rootViewController?.present(alert, animated: true, completion: { _ in }) 

Swift

var topWindow: UIWindow = UIWindow(frame: UIScreen.mainScreen().bounds) 
topWindow.rootViewController = UIViewController() 
topWindow.windowLevel = UIWindowLevelAlert + 1 
var alert: UIAlertController = UIAlertController.alertControllerWithTitle("APNS", message: "received Notification", preferredStyle: .Alert) 
alert.addAction(UIAlertAction.actionWithTitle(NSLocalizedString("OK", "confirm"), style: .Cancel, handler: {(action: UIAlertAction) -> Void in 
// continue your work 
// important to hide the window after work completed. 
// this also keeps a reference to the window until the action is invoked. 
topWindow.hidden = true 
})) 
topWindow.makeKeyAndVisible() 
topWindow.rootViewController.presentViewController(alert, animated: true, completion: { _ in }) 

descrizione dettagliata: http://www.thecave.com/2015/09/28/how-to-present-an-alert-view-using-uialertcontroller-when-you-dont-have-a-view-controller/

+0

grazie @ Anbu.karthik, funziona :) – Rockers23

+0

Come dovrei aggiungere un altro pulsante a UIAlert per swift 3 –

+0

creare l'oggetto di UIAlertAction e gestire l'azione –