6

So I'm working on an in app purchase in a SpriteKit game on AppleTV, but in the event of an error with payment or any other variety of errors, I want to display an error. UIAlertView is how I did it on iOS, but that's not an option on tvOS. Is there something similar that could be done instead? Basically, I need something to popup describing the error and a button to dismiss the popup. The ability to add more buttons (like UIAlertView) would be icing.

Note: I have researched this a bit and most things seem to point to using TVML, however, I don't believe that's an option mixed in with SpriteKit. I'll accept an answer related to this if it explains how to import something TVML (which I know next to nothing about) and run it alongside SpriteKit. I assume I'm looking for an answer unrelated to TVML though.

mredig
  • 1,736
  • 1
  • 16
  • 29

1 Answers1

19

Check tvOS UIAlertController class :

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"
                           message:@"This is an alert."
                           preferredStyle:UIAlertControllerStyleAlert];


UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
   handler:^(UIAlertAction * action) {}];

[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];

Edit : When using SpriteKit, last line is to replace with

UIViewController* controller = [UIApplication sharedApplication].keyWindow.rootViewController;
[controller presentViewController:alert animated:YES completion:nil];

Note that this class is also available in iOS since iOS 8 !

Sylverb
  • 939
  • 6
  • 14
  • I'm looking into this currently. The catch is calling the UIViewController class from a singleton off in the middle of nowhere and I'm not sure what the best practice is for that (http://stackoverflow.com/questions/1340434/get-to-uiviewcontroller-from-uiview is what I found) seeing as you're not supposed to call the VC from the view (and I assume its descendants). – mredig Oct 22 '15 at 22:54
  • SK and UIAlertController has been discussed here already as it's not a tvOS specific question ;) For example here : http://stackoverflow.com/questions/31152621/how-to-present-uialertcontroller-from-skscene – Sylverb Oct 22 '15 at 23:24
  • I was more referring to the concept of MVC to avoid calling on the controller from the view or its descendants. I got it working, but I'm not sure it's kosher is all. :) I'll be writing a more detailed comment to augment your answer and mark it soon. – mredig Oct 22 '15 at 23:34
  • 1
    By the way, I made a mistake in the property linking on the first line of the SpriteKit code - `UIViewController* controller = [UIApplication sharedApplication].keyWindow.rootViewController.view.window.rootViewController;` should be `UIViewController* controller = [UIApplication sharedApplication].keyWindow.rootViewController;` – mredig Nov 04 '15 at 16:31