Display warning from application delegate before display warning from viewDidload

I am trying to display the message contained in the push notification through the application delegate, as indicated in the parse.com documentation.

The problem I am facing is that in my viewdidload method for my first view controller, I present a warning that the user MUST see before using the application.

How can I call a method from my application delegate after the user sees a warning from the viewdidload method?

EDIT:

So, as suggested in the comments, a global variable is added that I set to true after I displayed a warning from my ViewDidload method, but the alert notification from my appDelegate application is still not showing.

here is my delegate.m file application:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. [Parse setApplicationId:@"xxxxxxxxxxxxxxxx" clientKey:@"xxxxxxxxxxxx"]; // Register for Push Notitications, if running iOS 8 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound); UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil]; [application registerUserNotificationSettings:settings]; [application registerForRemoteNotifications]; } else { // Register for Push Notifications before iOS 8 [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)]; } return YES; NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; if (Notification == true) { if (![pushText isEqual: @""]) { pushText = [[notificationPayload objectForKey:@"aps"] objectForKey:@"alert"]; UIAlertView *alert_news = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"News", "") message:pushText delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert_news show]; } } } 

And here is my viewdidload method:

  RoadSafetyAppAppDelegate *AppDelegate; - (void)viewDidLoad { AppDelegate = (RoadSafetyAppAppDelegate *)[[UIApplication sharedApplication] delegate]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. backgroundImage.alpha = 0.3; toRecipients = [[NSArray alloc]initWithObjects:@" records@shellharbour.nsw.gov.au ", nil]; static int appCounter; if ( appCounter < 1 ) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Disclaimer", "") message:NSLocalizedString(@"Using a mobile phone whilst driving is against the law. Ensure that you are not behind the wheel when using this app.", "") delegate:nil cancelButtonTitle:@"I agree to not use a mobile phone while driving" otherButtonTitles: nil]; [alert show]; appCounter = appCounter+1; AppDelegate.NotificationAlert = @"1"; AppDelegate.Notification = true; } } 
+5
source share
2 answers

since you want to show the rejection of ONE time and make sure that the user saw it and TAPED on the "Agree" button before showing any notification. You can do this with a simple local notification.

in the delegate (... doneFinishLaunchingWithOptions :)

  -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ //......you code here if ([[NSUserDefaults standardUserDefaults] boolForKey:@"disclaimerShown"]==nil) [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"disclaimerShown"]; [[NSUserDefaults standardUserDefaults] synchronize]; } //......you code here if ([[NSUserDefaults standardUserDefaults] boolForKey:@"disclaimerShown"]){ //YES if (![pushText isEqual: @""]) { pushText = [[notificationPayload objectForKey:@"aps"] objectForKey:@"alert"]; UIAlertView *alert_news = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"News", "") message:pushText delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert_news show]; } } } -(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{ NSString *value=[NSString stringWithFormat:@"%@",[notification.userInfo valueForKey:@"key"]]; if ([value isEqualToString:@"disclaimerShown"]) { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"disclaimerShown"]; [[NSUserDefaults standardUserDefaults] synchronize]; ///continue handle parse.com notification } } 

in your ViewController:

 -(void)viewDidLoad{ //... if ([[NSUserDefaults standardUserDefaults] boolForKey:@"disclaimerShown"]==NO){ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Disclaimer", "") message:NSLocalizedString(@"Using a mobile phone whilst driving is against the law. Ensure that you are not behind the wheel when using this app.", "") delegate:nil cancelButtonTitle:@"I agree to not use a mobile phone while driving" otherButtonTitles: nil]; alert.tag = 1; [alert show]; } //... } 

pragma sign - UIAlertViewDelegate

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 1) {//the disclaimer alert if (buttonIndex == 0) { UILocalNotification *alarm = [[UILocalNotification alloc] init]; alarm.userInfo = @{@"key": @"disclaimerShown"}; alarm.fireDate = [NSDate date]; alarm.timeZone = [NSTimeZone defaultTimeZone]; [[UIApplication sharedApplication] scheduleLocalNotification:alarm]; } } } 
+4
source

Instead of using the AppDelegate bool flag property, use NSUserDefaults ;

In AppDelegate, update this line:

 if (Notification == true) 

to

 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Notification"] == YES) 

And in the line ViewController -> viewDidLoad :

 AppDelegate.Notification = true; 

to

 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"Notification"]; [[NSUserDefaults standardUserDefaults] synchronize]; 

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1208096/


All Articles