AppleTV UIAlertView equivalent - objective-c

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.

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 !

Related

UIAlertView Dismiss Returns to Home Screen

My app starts with a "home screen" navigation controller that sits in its own storyboard, and can segue to different storyboards, each beginning with a new navigation controller. One of my secondary storyboards has a paywall. When the user elects to dismiss the paywall without making a purchase, all I've been able to accomplish is dismissing the paywall and displaying the root view controller of the storyboard that requires a purchase. I'm trying to send the user back to the "home screen" where they can see other content or segue to a different storyboard.
[PaywallManager registerBlockingPaywallClosedHandler:^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Blocked Paywall Closed" message:#"Return to home screen without purchase." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[alertController dismissViewControllerAnimated:true completion:^{
}]
}];
[alertController addAction:okAction];
UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[rootViewController presentViewController:alertController animated:true completion:^{}];
}];
When the user dismisses the UIAlertView by pressing the OK button, I need to segue back to the main navigation controller (home screen). Currently when the user dismisses the UIAlertView, they get access to the paid content. If I remove the closed handler, the app gets stuck on the paywall until a purchase is made or the user kills the app. Any help would be greatly appreciated. I am still struggling with Objective-C and haven't had time to learn Swift so please go easy on me.
I figured it out. The solution is here:
[PaywallManager registerBlockingPaywallClosedHandler:^{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"No Purchase Made" message:#"Return to the Home screen without purchase." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self.navigationController popViewControllerAnimated:YES];
}];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}];

is it possible to show the alert message on top of another sharing dialog

Im using this code to try and show a alert message on the iphone
[[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:alert animated:YES completion:nil];
it works fine.
But is it possible to show the alert message on top of another sharing dialog also shown using presentViewController:
at the moment it shows a warning in the console and the alert does not show, only shows the popup for the sharing activity?
I can get this to work using UIAlertView but that deprecated now
For future reference, it would be good to include the text of the warning you're receiving in the question as well.
My guess is it's along the lines of:
Warning: Attempt to present <UIAlertController: 0x7fe676036000> on <ViewController: 0x7fe674e02f50> which is already presenting <UIActivityViewController: 0x7fe67580f800>
Which basically indicates you can't present another view controller using a view controller that's already presenting a separate view controller.
One way you could work around this is to climb the presentedViewControllers to locate a view controller which isn't already presenting and then present from that (in your case you should only need to move up one presentedViewController to your UIActivityViewController).
Here's an example using the UIWindowScene to grab the rootViewController (iOS 13+), but should basically be the same idea for using the appDelegate's window as well:
- (IBAction)showShareSheetAndAlert:(id)sender {
UIImage *homerImage = [UIImage imageNamed:#"Homer"];
UIActivityViewController *shareSheet = [[UIActivityViewController alloc] initWithActivityItems:#[homerImage] applicationActivities:nil];
[self presentViewController:shareSheet animated:YES completion:^{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Test Alert" message:#"Here's a test alert on top of a share sheet" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *dismissAction = [UIAlertAction actionWithTitle:#"Dismiss" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:dismissAction];
UIWindowScene *windowScene = (UIWindowScene *)[[[[UIApplication sharedApplication] connectedScenes] allObjects] firstObject];
UIViewController *viewControllerToPresentOn = windowScene.windows.firstObject.rootViewController;
// keep climbing the presented view controllers until we find the top one which isn't presenting
while (viewControllerToPresentOn.presentedViewController != nil) {
viewControllerToPresentOn = viewControllerToPresentOn.presentedViewController;
}
[viewControllerToPresentOn presentViewController:alert animated:YES completion:nil];
}];
}
Which will result in this:

How do I create a UIAlertController in a webview?

I've been searching on the forums and still don't really know how to use an UIAlertController in a webview. I found a couple of questions although I didn't really get how to fix it. I'm a beginner if you can't already tell. What I'm trying to do is, if the user's internet turns off a message shows up. Although I get an error. Here's the error I get.
No visible #interface for 'WebViewController' declares the selector 'presentedViewController:animated:completion:'
And here's my code in the WebViewController's .m file.
- (void) webView:(UIWebView*) webView didFailLoadWithError:(nonnull NSError *)error {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"No Internet Connection" message:#"You don't have an internet connection! Please be aware that some pages might not load correctly." preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *Okay = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:Okay];
[self presentedViewController:alert animated:YES completion:nil];}
You've a typo — the method is presentViewController:animated:completion:, not present**ed**ViewController:animated:completion:. The 'present' is meant to be a verb, not an adjective — you're asking yourself to do something, not about something. So the error you've received is completely correct.
Change to:
[self presentViewController:alert animated:YES completion:nil];

Does anyone know the name of this UIObject? (Picture inside)

I'm trying to add something like this to my program but idk what it's called.
I'm talking about the buttons that pop up "Photo Library" "Take Photo or Video" "Cancel"
It's a UIAlertController with style UIAlertControllerStyleActionSheet.
You use it like this:
UIAlertController *aC = [UIAlertController alertControllerWithTitle:#"Title"
message:#"Message"
preferredStyle:UIAlertControllerStyleActionSheet];
[aC addAction:[UIAlertAction actionWithTitle:#"Button 1"
// Style can be default, destructive or cancel
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// handler
}]];
// Add more actions (buttons) here if needed
// Assuming you're in your view controller,
// present the alert view controller like this:
[self presentViewController:aC animated:YES completion:nil];
UIActionSheet class from Cocoa touch.

How to fix run-time error using UIAlertController

I have this code sitting in a UIVIewController (XCode 6.1, iOS 8.1.1):
[UIAlertController showActionSheetInViewController:self
withTitle:#"Test Action Sheet"
message:NSLocalizedString(#"Are you sure you want to delete ALL appointments?",nil)
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:#"Yes"
otherButtonTitles:#[#"No"] // same as Cancel
tapBlock:^(UIAlertController *controller, UIAlertAction *action, NSInteger buttonIndex){
if (buttonIndex == UIAlertControllerBlocksCancelButtonIndex) {
NSLog(#"Cancel Tapped");
} else if (buttonIndex == UIAlertControllerBlocksDestructiveButtonIndex) {
NSLog(#"Delete Tapped");
} else if (buttonIndex >= UIAlertControllerBlocksFirstOtherButtonIndex) {
NSLog(#"Other Action Index %ld", (long)buttonIndex - UIAlertControllerBlocksFirstOtherButtonIndex);
}
}];
When I run it, I get this run-time error:
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x7fdfe3324f00>) of style UIAlertControllerStyleActionSheet. The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.'
What do I need to do to make this work? (I have looked at SO and Google and found nothing specific). I appreciate any help I can get on this...
UPDATE
I re-wrote it without the 3rd-party code; added this code, and now it works!
UIAlertController * view= [UIAlertController
alertControllerWithTitle:#"My Title"
message:#"Select your Choice"
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* ok = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
//Do some thing here
[view dismissViewControllerAnimated:YES completion:nil];
}];
UIAlertAction* cancel = [UIAlertAction
actionWithTitle:#"Cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action)
{
[view dismissViewControllerAnimated:YES completion:nil];
}];
[view addAction:ok];
[view addAction:cancel];
view.popoverPresentationController.sourceView = self.view;
view.popoverPresentationController.sourceRect = CGRectMake(self.view.bounds.size.width / 2.0, self.view.bounds.size.height / 2.0, 1.0, 1.0);
[self presentViewController: view animated:YES completion:nil];
The error message you received appeared because you ran iPhone code on an iPad. For use on an iPad, you have to set the alertController's popoverPresentationController. The source rectangle can be generated without the sloppy dimension calculations, too. Below, is a complete method, showing how you would encounter the code upon pressing a button. After setting up the AlertController the way you want, you get its popoverPresentationController and set it up for use with the iPad. In the method below, the button what was pressed is the sender. So we cast the sender back to that button, then use the button to set the rectangle. No messy dimensions need calculating. Now, if you run the code on the iPad, the popover will not display the Cancel button, (which does appear on the iPhone). That is by design. If you view the Apple UIPopoverController documentation, you see that the popover is cancelled by tapping outside of it.
- (IBAction)showImagePickerButtonTapped:(id)sender;
{
BOOL isCameraAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
BOOL isPhotoLibraryAvailable = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:nil]];
if (isCameraAvailable) {
[alertController addAction:[UIAlertAction actionWithTitle:#"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self _showImagePickerWithSourceType:UIImagePickerControllerSourceTypeCamera];
}]];
}
if (isPhotoLibraryAvailable) {
[alertController addAction:[UIAlertAction actionWithTitle:#"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
[self _showImagePickerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}]];
}
// The following lines are needed for use with the iPad.
UIPopoverPresentationController *alertPopoverPresentationController = alertController.popoverPresentationController;
UIButton *imagePickerButton = (UIButton*)sender;
alertPopoverPresentationController.sourceRect = imagePickerButton.frame;
alertPopoverPresentationController.sourceView = self.view;
[self showDetailViewController:alertController sender:sender];
}
There's precious little information to go on here...
It appears you're using https://github.com/ryanmaxwell/UIAlertController-Blocks, not the standard UIAlertController, in which case the exception suggests changes that the version of the code you're using doesn't cover yet or a use case that requires extra work on your part.
I've never used this 3rd-party code but a quick check doesn't show any obvious "do this" in the docs. My initial recommendation would be to implement the delegate method on the view in question and give it what it wants - the location at which to present the popover.