开发者

How to get iPhone method called periodically when app in background, using NSTimer when in foreground

I am using [NSTimer scheduledTimerWithTimeInterval ...] which works fine when my application is in the foreground, but when I have it in the background it doesn't appear to be working.

What is supposed to happen is that the application calls to a web service on a periodic basis, so in the emulator I can watch it print out the communication.

I am observing the UIApplicationDidEnterBackgroundNotification event so I can change modes, as it seems that I will need to switch to perhaps setting several scheduled notification calls, but then I run into a p开发者_C百科roblem when they switch back to my application as I would then be calling the service twice for a while, or need to cancel all the notification calls.

In the emulator I am not seeing that I am going to applicationDidEnterBackground when I bring another program to the foreground which is also of concern, but the bigger question is how should I get my calling of the method periodically whether in the foreground or background.

In case it makes any difference I am using XCode 3 and the 4.0 SDK.


I found that after restarting my computer that the application did go to applicationDidEnterBackground.

But, in order to make my webservice call I had to call synchronously when in the background, as calling asynchronously seemed to just silently fail. I am using ASIHttpRequest (http://allseeing-i.com/ASIHTTPRequest/).

Since the NSTimer doesn't work while in the background I went to using NSThread and used a task where I can set the time for how long it should sleep. I believe this was done on the main thread, which didn't seem to be a problem since the application was in the background.

In order for any of this to work I had to put location in the info.plist, which may end up using more battery.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜