2016-03-22 7 views
11

Ich arbeite mit PushNotification auf iOS App. Ich möchte einen UIalertcontroller zeigen, wenn die App eine Benachrichtigung erhält.So zeigen Sie UIAlertController von Appdelegate

Ich versuche, diesen Code in der unten stehenden AppDelegate:

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

Aber die UIAlertcontroller im Stammansicht (Erster Bildschirm) und für andere Uiviewcontroller zeigt i bekam Warnung oder die App stürzt ab.

+0

was der creash Bericht –

+0

Aber die UIAlertcontroller in der Stammansicht zeigt .... ofcourse Sie die Warnung an Root-Controller hinzufügen. Natürlich wird es auf anderen uiview-Controller abstürzen, weil Sie versuchen, einen Alarm auf dem Controller hinzuzufügen, der dem Benutzer nicht angezeigt wird. –

+1

Ja, ich weiß, dass ich ualertcontroller zum rootView und nicht zur aktiven Ansicht hinzufüge, und meine Frage ist, wie kann ich den uialertController in den anderen uiviewcontroller zeigen, wenn eine Benachrichtigung empfangen wird. – Rockers23

Antwort

42

versuchen, diese

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

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

+0

danke @ Anbu.karthik, es funktioniert :) – Rockers23

+0

Wie würde ich gehen über das Hinzufügen einer weiteren Schaltfläche zu UIAlert für swift 3 –

+0

erstellen Sie das Objekt von UIAlertAction und behandeln die Aktion –