开发者

How to prevent my app from running in the background on the iPhone

Is there any way to cause an app to quit instead of going to background when the home button is pressed? For security reasons, it would be better if the app did not run in background but actually closed when home is pushed. This is not for users' security, but rather for company data on the app, so it is not a user's choice. I could not 开发者_如何学Pythonfind any way to quit other than forcing an exit, which Apple discourages.


See opting out of background execution in the iphone application programming guide:

"If you do not want your application to remain in the background when it is quit, you can explicitly opt out of the background execution model by adding the UIApplicationExitsOnSuspend key to your application’s Info.plist file and setting its value to YES.

When an application opts out, it cycles between the not running, inactive, and active states and never enters the background or suspended states.

When the user taps the Home button to quit the application, the applicationWillTerminate: method of the application delegate is called and the application has approximately five seconds to clean up and exit before it is terminated and moved back to the not running state."


Just go to info.plist of your project and check "Application does not run in background" to YES.


Unfortunately after trying out everything, I was still not able to see my application exiting on pressing home button. It always went into background even though UIApplicationExitsOnSuspend was YES and of type boolean in plist file and I removed the application from Simulator, restarted Xcode and Simulator and tried everything suggested.

Finally I started debugging the application and found one function which was preventing the application from exiting. The function was fairly simple and was downloading some images from network and was getting called from applicationDidFinishLaunching of appDelegate file. This function was delegating the task of creating network connection and downloading data to some other reusable class where I had the below code:

if(isBackgroundProcessingSupported){
        appDelegate.bgTask = [app beginBackgroundTaskWithExpirationHandler:^{}];
        [NSURLConnection connectionWithRequest:request delegate:self];
    }

So finally it turned out that the above code was responsible for putting the application into background on press of home button. When I commented the above code, my application is exiting instead of going into background.

NOTE: The code was there earlier as initially application was supporting background processing.

Hope this helps someone who is also struggling to find the reason like me.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜