2012-11-29 29 views
2

我是iphone應用程序開發的新手。顯示彈出式菜單(UIAlertView),而iPhone應用程序在後臺運行

我的問題是如何在我的應用程序在後臺運行時顯示彈出窗口(UIAlertView)? 我正在使用xcode 4.2 for ios 6 我無法通過互聯網找到令人滿意的答案。 有人可以幫助我嗎?

- (void)applicationDidEnterBackground:(UIApplication *)application 
     { 
     UIApplication* app = [UIApplication sharedApplication]; 
     bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
      [app endBackgroundTask:bgTask]; 
      bgTask = UIBackgroundTaskInvalid; 
     }]; 

     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(doBackgroundProcessing) userInfo:nil repeats:YES]; 
     [[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode]; 
     [[NSRunLoop currentRunLoop] run]; 
    }); 

}

- (void) doBackgroundProcessing 
    { 
     global = [lMGlobal getInstance]; 

     while(TRUE) 
     { 
      [self showAlertFor:@"Hello" andMessage:@"Wake up"]; 

      [NSThread sleepUntilDate:[lMGlobal getSleepDuration]]; 

     } 

    } 

    - (void) showAlertFor:(NSString *)title andMessage:(NSString*)message 
     { 
      UIAlertView *alertDialog; 
      alertDialog = [[UIAlertView alloc] 
       initWithTitle:title 
       message:message 
       delegate: self 
       cancelButtonTitle: nil 
       otherButtonTitles: @"Mute", nil]; 

      [alertDialog 
      performSelector:@selector(show) 
      onThread:[NSThread mainThread] 
      withObject:nil 
      waitUntilDone:NO]; 
      [alertDialog release]; 
     } 
+0

爲什麼不試試UILoalNotification或彈出通知雙方方式當在後臺運行應用程序時,你在主屏幕上顯示消息.i根本不認爲你的代碼在工作。 – kapil

回答

7

儘管您無法顯示UIAlertView,但您可以顯示UILocalNotification。你的代碼可能是這個樣子:

backupAlarm = [[UILocalNotification alloc] init]; 

backupAlarm.fireDate = alarmTime; 
backupAlarm.timeZone = [NSTimeZone systemTimeZone]; 

backupAlarm.alertBody = @"Good morning, time to wake up."; 
backupAlarm.alertAction = @"Show me"; 
backupAlarm.soundName = UILocalNotificationDefaultSoundName; 
+0

感謝您的回覆。有用。它們在通知中心下可見,但通知似乎不會彈出。有沒有讓它彈出的設置? –

+1

我知道了...你不能做太多的事情,因爲你沒有任何控制權......它取決於用戶如何在設置>通知下設置通知中心警報風格...並添加到痛苦通知中心在模擬器中不可用:( –

+0

這是正確的,它取決於用戶如何顯示。 –

0

這是不可能讓自己的應用顯示UIAlertView在後臺運行時。

+0

我的應用程序正在後臺運行。在applicationDidEnterBackground中,我打電話給doBackgroundProcessing方法 –

0

我不認爲你可以直接做,但你可以消防UILocalNotification!

0

Furthemore的@DavidBrunow的反應,你必須安排配置的本地通知:

[[UIApplication sharedApplication] scheduleLocalNotification: backupAlarm]; 
相關問題