在显示来自 viewDidload 的警报之前显示来自应用程序委托的警报 [英] Displaying alert from app delegate before displaying alert from viewDidload

查看:22
本文介绍了在显示来自 viewDidload 的警报之前显示来自应用程序委托的警报的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 parse.com 文档中概述的应用程序委托显示推送通知中包含的消息.

I am attempting to display the message contained within a push notification through the app delegate as outlined in the parse.com documentation.

我遇到的问题是,在我的第一个视图控制器的 viewdidload 方法中,我显示了一个警报,用户在使用应用程序之前必须看到该警报.

The problem I am having is that in my viewdidload method for my first view controller, i am presenting an alert which the user MUST see before they use the app.

在用户从 viewdidload 方法看到 Alert 后,如何从我的应用委托调用该方法?

How can I call the method from my app delegate after the user sees the Alert from the viewdidload method?

所以,正如评论中所建议的,我添加了一个全局变量,一旦我从我的 ViewDidload 方法显示警报,我就将其设置为 true,但我的 appDelegate 的通知警报仍然没有出现.

So i have, as suggested in the comments, added a global Variable which i set to true once i have Displayed the alert from my ViewDidload method, but the Notification Alert from my appDelegate still does not appear.

这是我的应用程序 delegate.m 文件:

here is my app delegate.m file:

    - (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];


            }
    }


}

这是我的 viewdidload 方法:

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;



    }

}

推荐答案

因为您想显示一次免责声明,并确保用户在显示任何通知之前看到它并点击同意按钮.您可以使用简单的本地通知来做到这一点.

since you want to show the disclaimer ONE time and to be sure that the user saw it and TAPED on Agree Button before showing any notification. you can do that using a simple local notification.

在委托中 (...didFinishLaunchingWithOptions:)

in delegate (...didFinishLaunchingWithOptions:)

 -(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
    }

}

在你的视图控制器中:

-(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 mark - 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];
        }
    }

}

这篇关于在显示来自 viewDidload 的警报之前显示来自应用程序委托的警报的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆