Haris Farooqui
Haris Farooqui

Reputation: 984

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

I am new to iphone app development.

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?

    - (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];
      }

Upvotes: 2

Views: 5303

Answers (4)

Lisarien
Lisarien

Reputation: 1136

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

[[UIApplication sharedApplication] scheduleLocalNotification: backupAlarm];

Upvotes: 0

username tbd
username tbd

Reputation: 9382

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

Upvotes: 0

David Brunow
David Brunow

Reputation: 1299

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

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;

Upvotes: 8

Daij-Djan
Daij-Djan

Reputation: 50089

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

Upvotes: 0

Related Questions