2

Is it possible to open an app from a background task in iOS?

I want my app to run in the background, and I want it to reappear for whatever reason without user input. Is this possible?

In Android you can have a service that runs in the background, and at any time you can start an Activity, which starts the app for that activity.

I know this does not make for a good user experience. I still want to know whether it can be done.

bneely
  • 9,083
  • 4
  • 38
  • 46
gjrwebber
  • 2,658
  • 2
  • 22
  • 26
  • 1
    do you want to submit app to the store ? – Kunal Balani Mar 06 '14 at 02:56
  • Also , whats wrong with generating a notification and allowing user to open you app ? Thats a default iOS model !! Also , Have you tried [[UIApplication sharedApplication] openUrl:] – Kunal Balani Mar 06 '14 at 02:59
  • You need to initiate a background task and call [[UIApplication sharedApplication] openUrl:]. That should do it. Look at this http://stackoverflow.com/questions/7374540/launch-iphone-application-with-identifier – Kunal Balani Mar 06 '14 at 03:00
  • This is a custom app that will not be used by the public. I do not want the user to interact. – gjrwebber Mar 06 '14 at 03:00
  • @KunalBalani I tried your suggestion, but it doesn't seem to work. I have created a custom URL, then a background task using http://code.tutsplus.com/tutorials/ios-multitasking-background-tasks--mobile-6913. When I call [[UIApplication sharedApplication] openUrl:[NSURL ...]]; from the background task it does not open the app. – gjrwebber Mar 06 '14 at 03:30

1 Answers1

1

You can create schedule a local notification with a time offset of 0 (eg show an alert immediatly). If the use taps on the notification, the app will be launched.

But otherwise, you will not be able to launch out of the background into the forground without a notification.

    NSDate * theDate = [[NSDate date] dateByAddingTimeInterval:0]; //

    UIApplication* app = [UIApplication sharedApplication];
    NSArray  *    oldNotifications = [app scheduledLocalNotifications];


    // Clear out the old notification before scheduling a new one.
    if ([oldNotifications count] > 0)
        [app cancelAllLocalNotifications];

    // Create a new notification.
    UILocalNotification* alarm = [[UILocalNotification alloc] init];
    if (alarm)
    {
        alarm.fireDate = theDate;
        alarm.timeZone = [NSTimeZone defaultTimeZone];
        alarm.repeatInterval = 0;
        alarm.soundName = @"sonar";
        alarm.alertBody = @"Background task complete, tap to show app" ;

        [app scheduleLocalNotification:alarm];
    }

From Apples Documentation: "Notifications are a way for an app that is suspended, is in the background, or is not running to get the user’s attention. Apps can use local notifications to display alerts, play sounds, badge the app’s icon, or a combination of the three. For example, an alarm clock app might use local notifications to play an alarm sound and display an alert to disable the alarm. When a notification is delivered to the user, the user must decide if the information warrants bringing the app back to the foreground. (If the app is already running in the foreground, local notifications are delivered quietly to the app and not to the user.)"

John Ballinger
  • 7,380
  • 5
  • 41
  • 51
  • Thanks, but I am interested in opening without user input. Is there documentation that states you cannot open from the background? – gjrwebber Mar 06 '14 at 03:46
  • https://developer.apple.com/library/ios/documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html#//apple_ref/doc/uid/TP40007072-CH4-SW20 – John Ballinger Mar 06 '14 at 03:50
  • 1
    Looks like it is not possible, though only 98% sure of this. I was hoping for concrete doco saying that Apps cannot come to the foreground from the background without user input. But it just implies this. Thanks anyway. – gjrwebber Mar 07 '14 at 05:50