Make the application restart by itself on a jailbroken device

无人久伴 提交于 2019-11-30 21:58:28

问题


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?


回答1:


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.

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.




回答2:


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.



来源:https://stackoverflow.com/questions/12652297/make-the-application-restart-by-itself-on-a-jailbroken-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!