在iPhone应用程序在后台运行时显示弹出窗口(UIAlertView)

时间:2012-11-29 06:46:50

标签: iphone ios xcode

我是iphone app开发的新手。

我的问题是,当我的应用程序在后台运行时,如何显示弹出窗口(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];
      }

4 个答案:

答案 0 :(得分:8)

虽然您无法显示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;

答案 1 :(得分:0)

在后台运行时,您的应用无法显示UIAlertView

答案 2 :(得分:0)

我认为你不能直接做到这一点但你可以解雇UILocalNotification!

答案 3 :(得分:0)

更多@DavidBrunow的回复,您必须通过以下方式安排配置的本地通知:

[[UIApplication sharedApplication] scheduleLocalNotification: backupAlarm];