2014-12-02 12 views
5

Ich bin versucht, die Nachricht innerhalb einer Push-Benachrichtigung durch den AppDelegate enthält angezeigt werden, wie in der parse.com Dokumentation beschrieben.angezeigte Warnung von AppDelegate vor Benachrichtigung von viewDidLoad Anzeigen

Das Problem, das ich habe ist, dass für meine ersten View-Controller in meiner viewDidLoad Methode, ich eine Warnung vorstelle, die der Benutzer sehen muß, bevor sie die App verwenden.

Wie kann ich die Methode von meinem AppDelegate, nachdem der Benutzer rufen Sie die Benachrichtigung von der viewDidLoad Methode sieht?

EDIT:

Also ich habe, wie es in den Kommentaren vorgeschlagen, eine globale Variable hinzugefügt, die ich auf true gesetzt, sobald ich die Warnung von meiner viewDidLoad Methode angezeigt, aber die Meldung Benachrichtigung von meiner AppDelegate noch erscheint nicht.

hier ist meine App delegate.m Datei:

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


      } 
    } 


} 

Und hier Methode meine viewDidLoad ist:

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

Vom AppDelegate können Sie eine showPushMessage Bool Variable innerhalb des View-Controller gesetzt, zeigen dann die Push-Nachricht Alarm nach dem ersten Alarm im Falle des Feldes verwiesen, dass showPushMessage == YES. –

+0

könnten Sie ein wenig Code zur Verfügung stellen, nur um dies zu skizzieren? Ich bin ein bisschen neu das AppDelegate selbst für zusätzliche Manipulation – scb998

+0

Die App zu verwenden, sollte sofort den Push-Benachrichtigung Alarm angezeigt, wenn die App bereits geöffnet ist, und der Benutzer hat bereits akzeptiert Ihren Haftungsausschluss Alarm, nicht wahr? –

Antwort

4

, da Sie den Disclaimer EINEMmal zeigen wollen und sicher sein, dass der Benutzer sah es und TAPED auf Zustimmen Button vor dem Anzeigen einer Benachrichtigung. Sie können dies mit einer einfachen lokalen Benachrichtigung tun.

in Delegat (... 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 Ihnen 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 Marke - 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

von Statt AppDelegate bool flag Nutzungsart NSUserDefaults;

In AppDelegate Update diese Zeile aus:

if (Notification == true) 

zu

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

Und in Viewcontroller -> viewDidLoad Methode Update Zeile von:

AppDelegate.Notification = true; 

zu

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

Hoffe, das hilft.

Verwandte Themen