Comment montrer UIAlertController de Appdelegate

je travaille avec PushNotification sur l'application iOS. Je voudrais montrer un UIalertcontroller lorsque l'application reçoit une notification.

j'ai essayer ce code ci-dessous dans l'AppDelegate:

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

mais le Controller UIAlertcontroller s'affiche dans la vue racine (premier écran) et pour les autres Controller uiviewcontroller j'ai reçu un avertissement ou l'application s'écrase.

20
demandé sur Anbu.karthik 2016-03-22 16:31:25

5 réponses

essayez ceci

Objectif-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; // if you want to hide the topwindow then use this
    topWindow = nil; // if you want to remove the topwindow then use this 
}]];

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

Swift3

var topWindow: UIWindow? = 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 // if you want to hide the topwindow then use this
    topWindow = nil // if you want to hide the topwindow then use this
}))
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 // if you want to hide the topwindow then use this
    topWindow = nil // if you want to remove the topwindow then use this 
}))
topWindow.makeKeyAndVisible()
topWindow.rootViewController.presentViewController(alert, animated: true, completion: { _ in })

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

59
répondu Anbu.karthik 2018-04-20 08:42:49

La Plus Courte & La Plus Simple :

Créer une extension :

extension UIApplication {
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
    if let navigationController = controller as? UINavigationController {
        return topViewController(controller: navigationController.visibleViewController)
    }
    if let tabController = controller as? UITabBarController {
        if let selected = tabController.selectedViewController {
            return topViewController(controller: selected)
        }
    }
    if let presented = controller?.presentedViewController {
        return topViewController(controller: presented)
    }
    return controller
} }

et ensuite l'utiliser n'importe où, comme

UIApplication.topViewController()?.present(UIViewController, animated: true, completion: nil)

avec ceci vous pouvez présence d'Alerte ou quoi que ce soit n'importe où Exemple :

let alert = UIAlertController(title: "Your title", message: "Your message", preferredStyle: .alert)
let cancelButton = UIAlertAction(title: "Ok", style: .cancel, handler: nil)
alert.addAction(cancelButton)
UIApplication.topViewController()?.present(alert, animated: true, completion: nil)

MÉTHODE ALTERNATIVE:

pas besoin de créer une Extension ou une méthode ou n'importe quoi simplement écrire les 3 lignes ci-dessus pour créer une alerte et pour présenter utilisation :

self.window?.rootViewController?.present(alert, animated: true, completion: nil)

C'est ça.! =)

4
répondu Yash Bedi 2018-04-05 07:29:05

réponse ci-dessus mais dans Swift 4.1

var topWindow: UIWindow? = UIWindow(frame: UIScreen.main.bounds)
topWindow?.rootViewController = UIViewController()
topWindow?.windowLevel = UIWindowLevelAlert + 1
let alert: UIAlertController =  UIAlertController(title: "APNS", message: "received Notification", preferredStyle: .alert)
    alert.addAction(UIAlertAction.init(title: "OK", style: .default, handler: { (alertAction) in
        topWindow?.isHidden = true
        topWindow = nil
    }))

topWindow?.makeKeyAndVisible()
topWindow?.rootViewController?.present(alert, animated: true, completion:nil)

Merci d'avoir lu ceci.

3
répondu Najam 2018-07-17 12:29:25

Pour la facilité, j'ai utilisé de la catégorie

UIAlertController+UIWindow.h

@interface UIAlertController (UIWindow)

- (void)show;
- (void)show:(BOOL)animated;

@end

UIAlertController+UIWindow.m

#import <objc/runtime.h>

@interface UIAlertController (Private)

@property (nonatomic, strong) UIWindow *alertWindow;

@end

@implementation UIAlertController (Private)

@dynamic alertWindow;

- (void)setAlertWindow:(UIWindow *)alertWindow {
    objc_setAssociatedObject(self, @selector(alertWindow), alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIWindow *)alertWindow {
    return objc_getAssociatedObject(self, @selector(alertWindow));
}

@end

@implementation UIAlertController (UIWindow)

- (void)show {
    [self show:YES];
}

- (void)show:(BOOL)animated {
    [self setupWindow];
    [self.alertWindow.rootViewController presentViewController:self animated:animated completion:nil];
}

- (void)setupWindow {
    self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.alertWindow.rootViewController = [[UIViewController alloc] init];

    id<UIApplicationDelegate> delegate = [UIApplication sharedApplication].delegate;
    if ([delegate respondsToSelector:@selector(window)]) {
        self.alertWindow.tintColor = delegate.window.tintColor;
    }

    UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
    self.alertWindow.windowLevel = topWindow.windowLevel + 1;

    [self.alertWindow makeKeyAndVisible];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];

    // precaution to insure window gets destroyed
    self.alertWindow.hidden = YES;
    self.alertWindow = nil;
}

Utiliser:

UIAlertController *alertController;
// -- code --
[alertController show];
0
répondu Lal Krishna 2018-03-27 07:11:15

j'ai écrit une classe statique pour rendre le code réutilisable dans swift 4, cette classe a fourni différentes méthodes pour afficher les alertes.

        class AlertUtility: NSObject {

        static func showAlert(title: String!, message : String!, viewController: UIViewController) {
            let alert = UIAlertController(title: title, message: message ,preferredStyle: UIAlertControllerStyle.alert)

            alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "ok"), style: UIAlertActionStyle.cancel, handler: nil))

            viewController.present(alert, animated: true, completion: nil)
        }

        static func showAlertAutoDismiss(title: String!, message : String!) -> Void {
            //let appDelegate = UIApplication.shared.delegate as! AppDelegate

            // the alert view
            let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

            let topWindow = UIWindow(frame: UIScreen.main.bounds)
            topWindow.rootViewController = UIViewController()
            topWindow.windowLevel = UIWindowLevelAlert + 0.8

            topWindow.makeKeyAndVisible()
            topWindow.rootViewController?.present(alert, animated: true, completion: {})

            // change to desired number of seconds (in this case 5 seconds)
            let when = DispatchTime.now() + 1
            DispatchQueue.main.asyncAfter(deadline: when){
                // your code with delay
                alert.dismiss(animated: true, completion: nil)
                topWindow.isHidden = true
            }
        }

        // Show alert view with call back
        static func showAlertWithCB(title: String, message: String, isConditional: Bool, viewController: UIViewController, completionBlock: @escaping (_: Bool) -> Void) {

            let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)

            // Check whether it's conditional or not ('YES' 'NO, or just 'OK')
            if isConditional
            {
                alert.addAction(UIAlertAction(title: NSLocalizedString("yes", comment: ""), style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in
                    alert.dismiss(animated: true, completion: nil)
                    completionBlock(true)
                }))

                alert.addAction(UIAlertAction(title: NSLocalizedString("no", comment: ""), style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in
                    alert.dismiss(animated: true, completion: nil)
                    completionBlock(false)
                }))
            }
            else
            {
                alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "ok"), style: UIAlertActionStyle.default, handler: { (action: UIAlertAction) in
                    alert.dismiss(animated: true, completion: nil)
                    completionBlock(true)
                }))
            }

            viewController.present(alert, animated: true, completion: nil)
        }

        static func showAlert(title: String!, message : String!) -> Void {
            //let appDelegate = UIApplication.shared.delegate as! AppDelegate

            // the alert view
            let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)

            let topWindow = UIWindow(frame: UIScreen.main.bounds)
            topWindow.rootViewController = UIViewController()
            topWindow.windowLevel = UIWindowLevelAlert + 1
            topWindow.makeKeyAndVisible()
            topWindow.rootViewController?.present(alert, animated: true, completion: {})

            alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "ok"), style: UIAlertActionStyle.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
            }))

        }

        static func showComingSoon(viewController: UIViewController) {
            let alert = UIAlertController(title: "", message: "Coming Soon", preferredStyle: .alert)

            viewController.present(alert, animated: true, completion: {})

            // change to desired number of seconds (in this case 1 seconds)
            let when = DispatchTime.now() + 0.6
            DispatchQueue.main.asyncAfter(deadline: when){
                // your code with delay
                alert.dismiss(animated: true, completion: nil)
            }
        }

        static func showGenericErrorMessageAlert(viewController: UIViewController) {
            let alert = UIAlertController(title: NSLocalizedString("error", comment: ""), message: NSLocalizedString("generic.error.message", comment: "") ,preferredStyle: UIAlertControllerStyle.alert)

            alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "ok"), style: UIAlertActionStyle.cancel, handler: nil))

            viewController.present(alert, animated: true, completion: nil)
        }

 static func showAlertWithTextField(viewController : UIViewController,completionBlock: @escaping (_: Bool, String) -> Void) {

        //1. Create the alert controller.
        let alert = UIAlertController(title: "Report Event?", message: "", preferredStyle: .alert)
        alert.view.tintColor = APP_ORANGE_COLOR
        //2. Add the text field. You can configure it however you need.
        //AlertUtility.addte
        alert.addTextField { (textField) in

            let heightConstraint = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50)
            textField.addConstraint(heightConstraint)
            textField.placeholder = "Enter report reason here"
            textField.tintColor = APP_ORANGE_COLOR
            textField.autocapitalizationType = .sentences
        }

        // 3. Grab the value from the text field, and print it when the user clicks OK.
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { (_) in
            // Force unwrapping because we know it exists.
            completionBlock(true,"")
            //print("Text field: \(textField.text)")
        }))

        // 3. Grab the value from the text field, and print it when the user clicks OK.
        alert.addAction(UIAlertAction(title: "Submit", style: .default, handler: { [weak alert] (_) in
            let textField = alert?.textFields![0] // Force unwrapping because we know it exists.
            completionBlock(true,(textField?.text)!)
            //print("Text field: \(textField.text)")
        }))

        // 4. Present the alert.
        viewController.present(alert, animated: true, completion: nil)

        let textField = alert.textFields![0]
        let v = UIView.init(frame: textField.frame)
        textField.addSubview(v)
        v.frame = textField.frame
        v.bounds = textField.bounds
        v.backgroundColor = APP_ORANGE_COLOR
        v.superview?.bringSubview(toFront: v)
       }

    }
0
répondu Pramod More 2018-10-10 10:16:22