从显示应用委托警报从viewDidLoad中显示警报前 [英] Displaying alert from app delegate before displaying alert from viewDidload

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

问题描述

我试图为parse.com文档中列出显示包含通过应用程序委托推送通知内的信息。

我遇到的问题是,在我为我的第一个视图控制器viewDidLoad方法,我是presenting警报,用户必须了解他们使用的应用程序之前。

我怎么能说从我的应用程序的委托方法用户从viewDidLoad方法看到提示后?

编辑:

所以我有,如意见提出,增加了一个全局变量,我设置为true,一旦我从我的viewDidLoad方法显示警报,但是从我的appDelegate通知警报仍然没有出现。

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

   - (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions
{
    //覆盖点后,应用程序启动定制。
    [解析​​setApplicationId:@xxxxxxxxxxxxxxxx
                  clientKey:@XXXXXXXXXXXX];    //注册推送Notitications,如果运行iOS 8
    如果([应用respondsToSelector:@selector(registerUserNotificationSettings :)]){
        UIUserNotificationType userNotificationTypes =(UIUserNotificationTypeAlert |
                                                        UIUserNotificationTypeBadge |
                                                        UIUserNotificationTypeSound);
        UIUserNotificationSettings *设置= [UIUserNotificationSettings settingsForTypes:userNotificationTypes
                                                                                 类别:无];
        [应用registerUserNotificationSettings:设置]。
        [应用registerForRemoteNotifications]
    }其他{
        // iOS版8日前注册推送通知
        [应用registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
                                                         UIRemoteNotificationTypeAlert |
                                                         UIRemoteNotificationTypeSound)];
    }
    返回YES;    *的NSDictionary = notificationPayload launchOptions [UIApplicationLaunchOptionsRemoteNotificationKey]
    如果(通知==​​真){
        如果([pushText的isEqual:@]!){
            pushText = [[notificationPayload objectForKey:@APS] objectForKey:@警报];
            UIAlertView中* alert_news = [[UIAlertView中页头] initWithTitle:NSLocalizedString(@新闻,)
                                                                 消息:pushText
                                                                代表:无
                                                       cancelButtonTitle:@确定
                                                       otherButtonTitles:无];
            [alert_news秀]
            }
    }
}

这是我的viewDidLoad方法:

  RoadSafetyAppAppDelegate * AppDelegate中; - (无效)viewDidLoad中
{
        的AppDelegate =(RoadSafetyAppAppDelegate *)[[UIApplication的sharedApplication]代表];
        [超级viewDidLoad中];    //装载视图,通常从笔尖后做任何额外的设置。    backgroundImage.alpha = 0.3;
    toRecipients = [[NSArray的页头] initWithObjects:@records@shellharbour.nsw.gov.au零]
    静态INT appCounter;
    如果(appCounter&所述; 1){
        UIAlertView中*警报= [[UIAlertView中页头] initWithTitle:NSLocalizedString(@免责声明,)
                                                        消息:NSLocalizedString(@使用移动电话,而驾驶是违法的确保你是不是方向盘后面使用这个应用程序时。,)
                                                       代表:无
                                              cancelButtonTitle:@我同意不使用移动电话,而驾驶
                                              otherButtonTitles:无];
        [警报显示]
        appCounter = appCounter + 1;       AppDelegate.NotificationAlert = @1;
        AppDelegate.Notification = TRUE;    }}


解决方案

既然你要显示的免责声明一次,以确保用户看到它和录音上显示任何通知之前同意键。你可以做,使用简单的本地通知。

在委托(... didFinishLaunchingWithOptions :)

   - (BOOL)应用:(*的UIApplication)的应用didFinishLaunchingWithOptions:(NSDictionary的*)launchOptions {        //......you code在这里
        如果([[NSUserDefaults的standardUserDefaults] boolForKey:@disclaimerShown] ==无)            [NSUserDefaults的standardUserDefaults] setBool:NO forKey:@disclaimerShown];
            [NSUserDefaults的standardUserDefaults]同步]。
        }
         //......you code在这里        如果([[NSUserDefaults的standardUserDefaults] boolForKey:@disclaimerShown]){// YES                    如果([pushText的isEqual:@]!){
                    pushText = [[notificationPayload objectForKey:@APS] objectForKey:@警报];
                    UIAlertView中* alert_news = [[UIAlertView中页头] initWithTitle:NSLocalizedString(@新闻,)
                                                                         消息:pushText
                                                                        代表:无
                                                               cancelButtonTitle:@确定
                                                               otherButtonTitles:无];
                    [alert_news秀]
                    }
          }
}
- (无效)应用:(*的UIApplication)的应用didReceiveLocalNotification:(UILocalNotification *)通知{    * NSString的值= [的NSString stringWithFormat:@%@,[notification.userInfo valueForKey:@钥匙]];
    如果([值isEqualToString:@disclaimerShown]){
                [NSUserDefaults的standardUserDefaults] setBool:YES forKey:@disclaimerShown];
                [NSUserDefaults的standardUserDefaults]同步]。
     ///继续手柄parse.com通知
    }}

在你的ViewController:

   - (无效)viewDidLoad中{
            // ...
           如果([[NSUserDefaults的standardUserDefaults] boolForKey:@disclaimerShown] == NO){                       UIAlertView中*警报= [[UIAlertView中页头] initWithTitle:NSLocalizedString(@免责声明,)
                                                                    消息:NSLocalizedString(@使用移动电话,而驾驶是违法的确保你是不是方向盘后面使用这个应用程序时。,)
                                                                   代表:无
                                                          cancelButtonTitle:@我同意不使用移动电话,而驾驶
                                                          otherButtonTitles:无];
                    alert.tag = 1;
                    [警报显示]
               }
            // ...
}

编译标志 - UIAlertViewDelegate

   - (无效)alertView:(UIAlertView中*)alertView clickedButtonAtIndex:(NSInteger的)buttonIndex
{
    如果(alertView.tag == 1){//免责声明警告
        如果(buttonIndex == 0){
            UILocalNotification *报警= [[UILocalNotification的alloc]初始化];
            alarm.userInfo = @ {@钥匙:@disclaimerShown};
            alarm.fireDate = [NSDate的日期];
            alarm.timeZone = [NSTimeZone defaultTimeZone]            [UIApplication的sharedApplication] scheduleLocalNotification:报警];
        }
    }}

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

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.

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

EDIT:

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.

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


            }
    }


}

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.

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
    }

}

in you 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 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天全站免登陆