2014-12-02 10 views
5

Sto tentando di visualizzare il messaggio contenuto all'interno di una notifica push tramite il delegato dell'app, come indicato nella documentazione di parse.com.Visualizzazione dell'avviso dal delegato dell'app prima della visualizzazione dell'avviso da viewDidload

Il problema che sto avendo è che nel mio metodo viewdidload per il mio primo controller di visualizzazione, sto presentando un avviso che l'utente DEVE vedere prima di usare l'app.

Come posso chiamare il metodo dal mio delegato dell'app dopo che l'utente ha visualizzato l'avviso dal metodo viewdidload?

EDIT:

così ho, come suggerito nei commenti, ha aggiunto una variabile globale, che ho impostato a true una volta che ho visualizzato l'avviso dal mio metodo viewDidLoad, ma la notifica di avviso dalla mia AppDelegate ancora non appare.

qui è la mia applicazione di file 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]; 


      } 
    } 


} 

E qui è il mio metodo 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

Dal delegato dell'app, è possibile impostare una variabile bool di showPushMessage all'interno del controller di visualizzazione, quindi mostrare l'avviso del messaggio push dopo che il primo avviso è stato eliminato nel caso in cui showPushMessage == YES. –

+0

potresti fornire un po 'di codice solo per delineare questo? Sono un po 'nuovo nell'utilizzo dell'app delegato stesso per ulteriori manipolazioni – scb998

+0

L'app dovrebbe mostrare immediatamente l'avviso di notifica push se l'app è già aperta e l'utente ha già accettato l'avviso di disclaimer, giusto? –

risposta

4

dal momento che si desidera visualizzare il tempo di esclusione di responsabilità ONE e per essere sicuri che l'utente visto e TAPED sul pulsante Accetto prima di mostrare qualsiasi notifica. puoi farlo usando una semplice notifica locale.

a delegato (... 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 voi viewController: marchio

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

Invece di AppDelegate bool bandiera alcune stanze sono NSUserDefaults;

In AppDelegate aggiornamento di questa linea da:

if (Notification == true) 

a

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

E in ViewController - line> viewDidLoad metodo di aggiornamento da:

AppDelegate.Notification = true; 

a

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

Spero che questo aiuti.