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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13620284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:12:54  来源:igfitidea点击:

show a popup (UIAlertView) while iPhone app is running in background

iphoneiosxcode

提问by Haris Farooqui

I am new to iphone app development.

我是 iphone 应用程序开发的新手。

My question is how can I show a popup (UIAlertView) while my app is running in background? I am using xcode 4.2 for ios 6 I am unable to find a satisfactory answer over the internet. Can someone please help me with this?

我的问题是如何在我的应用程序在后台运行时显示弹出窗口 (UIAlertView)?我将 xcode 4.2 用于 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];
      }

回答by David Brunow

While you cannot show a UIAlertView, you could show a UILocalNotification. Your code could look something like this:

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

回答by username tbd

It is not possible to have your app show a UIAlertViewwhile running in the background.

您的应用程序UIAlertView在后台运行时无法显示。

回答by Daij-Djan

I dont think you can do it directly BUT you can fire a UILocalNotification!

我不认为您可以直接执行此操作,但是您可以触发 UILocalNotification!

回答by Lisarien

Furthemore the @DavidBrunow 's response, you have to schedule the configured local notification by:

此外,@DavidBrunow 的响应,您必须通过以下方式安排配置的本地通知:

[[UIApplication sharedApplication] scheduleLocalNotification: backupAlarm];