在显示来自viewDidload的警报之前显示来自app delegate的警报

时间:2014-12-02 01:03:58

标签: ios objective-c apple-push-notifications uialertview

我正在尝试通过parse.com文档中列出的app委托显示推送通知中包含的消息。

我遇到的问题是在我的第一个视图控制器的viewdidload方法中,我提出了一个警告,用户必须在使用该应用程序之前看到它。

在用户从viewdidload方法看到警报后,如何从我的app delegate调用该方法?

修改

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

这是我的app delegate.m文件:

    - (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方法:

 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;



    }

}

2 个答案:

答案 0 :(得分:4)

因为您希望在显示任何通知之前显示免责声明一次,并确保用户在同意按钮上看到并轻触。你可以使用简单的本地通知来做到这一点。

委托中的

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

}
你在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];
        }
    }

}

答案 1 :(得分:2)

而不是AppDelegate bool flag属性使用 NSUserDefaults ;

AppDelegate 中更新此行:

if (Notification == true)

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

ViewController - &gt;中viewDidLoad 方法更新行来自:

AppDelegate.Notification = true;

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

希望这有帮助。