2014-12-02 14 views
5

Próbuję wyświetlić komunikat zawarty w powiadomieniu push przez delegata aplikacji, zgodnie z opisem w dokumentacji parse.com.Wyświetlanie alertu od delegata aplikacji przed wyświetleniem alertu z viewDidload

Problem polega na tym, że w mojej metodzie viewdidload dla mojego pierwszego kontrolera widoku, przedstawiam alert, który użytkownik MUSI zobaczyć przed użyciem aplikacji.

Jak mogę wywołać tę metodę z delegata aplikacji, gdy użytkownik zobaczy alert z metody viewdidload?

EDIT:

więc mam, jak zasugerowano w komentarzach, dodał zmienna globalna, która ustawić na true raz mam wyświetlony alert z mojego sposobu viewDidLoad, jednak uderzył nieczysto zawiadomienie od mojego appDelegate nieruchomego nie pojawia się.

tutaj jest mój plik 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]; 


      } 
    } 


} 

I tu jest moja metoda 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

Od delegata aplikacji można ustawić zmienną bool showPushMessage w kontrolerze widoku, a następnie wyświetlać alert wiadomości Push po zwolnieniu pierwszego alertu w przypadku showPushMessage == YES. –

+0

czy mógłbyś podać trochę kodu, żeby to opisać? Jestem trochę nowy w korzystaniu z funkcji delegowania aplikacji w celu dodatkowej manipulacji. – scb998

+0

Aplikacja powinna natychmiast wyświetlać powiadomienie powiadomienia push, jeśli aplikacja jest już otwarta, a użytkownik zaakceptował już ostrzeżenie o naruszeniu zasad, prawda? –

Odpowiedz

4

ponieważ chcesz pokazać czas zrzeczenie jeden i mieć pewność, że użytkownik zobaczyłem to i TAPED w Agree Button przed wyświetleniem jakiegokolwiek powiadomienia. możesz to zrobić za pomocą prostego powiadomienia lokalnego.

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

} 

w was ViewController: Mark

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

Zamiast AppDelegate flagę bool użytkowania mienia NSUserDefaults;

W AppDelegate aktualizację Ta linia od:

if (Notification == true) 

do

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

I ViewController - linia> viewDidLoad aktualizacji metoda od:

AppDelegate.Notification = true; 

do

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

Mam nadzieję, że to pomoże.

Powiązane problemy