AppleTV UIAlertView equivalent
Asked Answered
U

1

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.

Urethritis answered 22/10, 2015 at 4:54 Comment(0)
G
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 !

Graupel answered 22/10, 2015 at 20:33 Comment(4)
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 (#1340934 is what I found) seeing as you're not supposed to call the VC from the view (and I assume its descendants).Urethritis
SK and UIAlertController has been discussed here already as it's not a tvOS specific question ;) For example here : #31153121Graupel
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.Urethritis
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;Urethritis

© 2022 - 2024 — McMap. All rights reserved.