0

Is it possible that my iOS app can auto-restart, each and every time the user accesses their home screen? This is for a jailbroken device -- the app is not destined for the App store.

In general, how can I make my app restart given specific user actions outside the app?

Charles
  • 50,943
  • 13
  • 104
  • 142
Taskinul Haque
  • 724
  • 2
  • 16
  • 36
  • Can you explain what you mean by "every time the user accesses their home screen"? What do you mean by "access"? If your app runs every time the user gets to the home screen, then your app would be the only one that could ever run. Is that what you want? Also, is this behaviour something you want coded into your app, or is this just for your own phone, and you just need a way to configure the phone to start your app on accelerometer conditions? – Nate Sep 30 '12 at 11:47
  • Ok, My app runs detect location and based on those inputs, it opens certain applications. Every time a user unlocks the device I ant the app to run by itself! - is this possible? and yes I also would like a way to configure my phone to run my app based on accelerometer conditions, any help or guidance is very appreciated ! – Taskinul Haque Sep 30 '12 at 16:52

2 Answers2

2

Accelerometer

If all you want to do is make your app run when you encounter certain accelerometer conditions, you can use Activator for that. Activator is a great app, by Ryan Petrich, available on Cydia for free. It lets you configure your device to run any app (or toggle) whenever a certain user action is taken. That could be a home button press, power/lock button press, or accelerometer shake.

enter image description here

If a basic shake isn't what you want, or you are building an app to give to many users, and don't want them to have to setup Activator themselves, then you probably need to write some code yourself.

For example, you could write a Launch Daemon, in addition to your main UI app, and have the launch daemon monitor the accelerometer.

When you detect the specific kind of motion you're interested in, you can launch your UI app with the open command. If this is just for your own use, just download the open package from Cydia. If this is for release to others, make sure your app depends on open to ensure that it's installed. For example, if packaging in a Debian .deb package, the DEBIAN/control file might have this:

Depends: open

to make sure users installing your app will also automatically get open, which your app needs.

Unlock

Your other problem concerns launching the app when the user unlocks the phone. Again, I would use your Launch Daemon to listen for this condition. On iOS 5, I see this notification when I unlock the phone:

Notification intercepted: com.apple.springboard.lockstate

(I detected this by running the notificationWatcher utility from the command line, while SSH'd into my phone. NotificationWatcher is also available from Cydia, as part of Erica Sadun's Erica Utilities package)

So, I would have your launch daemon register for Darwin notifications for "com.apple.springboard.lockstate". Something like this:

CFNotificationCenterAddObserver(CFNotificationCenterGetDarwinNotifyCenter(), //center
                                self, // observer: can be NULL if callback doesn't need self
                                onLockStateChanged, // callback
                                CFSTR("com.apple.springboard.lockstate"), // name
                                NULL, // object
                                CFNotificationSuspensionBehaviorDeliverImmediately);

where the callback function is here:

static void onLockStateChanged(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
    // if you need access to member data (ivars):
    MyLaunchDaemon* this = (MyLaunchDaemon*)observer;

    //if (userInfo != nil) {
    //    CFShow(userInfo);
    //}
    NSDictionary* info = (NSDictionary*)userInfo;
    // I'm not sure if the userInfo object has any useful
    //  description for the lock state event

    if (/* unlocked */) {
        // force app to open, or resume from the background
        system("/usr/bin/open com.mycompany.MyAppName");
    }
}

I see this same notification when the screen is locked, or unlocked, so you may need to have the launch daemon keep track of the locked/unlocked state, or inspect the userInfo object to see if that tells you whether this is a lock or unlock event. I'm sure there's other ways, too.

Update: if you want help sorting out whether the notification occurs when the screen is locked or unlocked, you can see my Update 2 in this other SO answer. notify_get_state() can be used to determine whether the event is an on, or off, event.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
0

Set the value of UIApplicationExitsOnSuspend to YES in your app's Info.plist file.

UIApplicationExitsOnSuspend (Boolean - iOS) specifies that the app should be terminated rather than moved to the background when it is quit. Apps linked against iOS SDK 4.0 or later can include this key and set its value to YES to prevent being automatically opted-in to background execution and app suspension.

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144
  • thanks for this, but i want the app to Run as if it was just opened, not be terminated completely ! just be restarted ! (like automate the process of terminating and re opening the app) ? - is this possible ? – Taskinul Haque Sep 30 '12 at 16:55