2014-12-02 67 views
5

我试图通过parse.com文档中概述的应用程序委托来显示包含在推送通知中的消息。显示来自应用程序委托的警报,然后显示来自viewDidload的警报

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

如何在用户从viewdidload方法看到警报后从我的应用程序委托调用方法?

编辑:

所以我有,如在评论中建议,增加了一个全局变量,我设置为true,一旦我已经从我的viewDidLoad方法显示的警报,但是从我的appDelegate仍然通知警报没有出现。

这里是我的应用程序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:@"[email protected]", 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; 



    } 

} 
+0

从应用程序委托中,您可以在视图控制器中设置showPushMessage bool变量,然后在showPushMessage == YES的情况下解除第一个警报后显示推送消息警报。 – 2014-12-02 01:20:02

+0

你能提供一些代码来概述一下吗?我有点新使用应用程序委托本身进行其他操作 – scb998 2014-12-02 01:21:36

+0

如果应用程序已经打开并且用户已经接受了免责声明警告,应用程序应立即显示推送通知警报,对不对? – 2014-12-08 12:10:12

回答

4

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

在委托(... 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]; 
       } 


      //... 
} 

编译马克 - 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]; 
     } 
    } 

} 
2

相反的AppDelegate的布尔标志财产使用NSUserDefaults;

的AppDelegate更新这一行从:

if (Notification == true) 

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

而在的ViewController - > viewDidLoad中方法更新线从:

AppDelegate.Notification = true; 

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

希望这会有所帮助。