2

I'm trying to make a SpriteKit app that has a button to go to my website if the user so desires to. I thought it would be a good practice to make an alert (since it's made for iOS 8, I'd use a UIAlertController) confirming if they want to be redirected to Safari to see it.

Here's what I have in code for the method to make/present this alert so far:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Go to website" message:@"Would you like to visit DannyDalton.com? This will open Safari." preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *yes = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            [self goToSite];
        }];
        UIAlertAction *no = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){}];
        [alertController addAction:yes];
        [alertController addAction:no];
        [alertController presentViewController:(UIViewController*)self animated:YES completion:nil];

My problem is presenting alertController onto the view controller, so it pops up. Any suggestions on how to make the SKView into a UIViewController so that alertController can present itself onto the screen? Thank you!

DDPWNAGE
  • 1,423
  • 10
  • 37

1 Answers1

3

One way to do this is to use NSNotification.

In the GameViewController(containing SKView) add a NSNotification observer to call the function to show the alert when receiving the notification.

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "showAlert:", name: "showAlert", object: nil)
     }

    func showAlert(object : AnyObject) {

       // Your alert code.
    }
}

Then post this notification from the place where you want to show the alert.

NSNotificationCenter.defaultCenter().postNotificationName("showAlert", object: nil)
rakeshbs
  • 24,392
  • 7
  • 73
  • 63