简体   繁体   中英

How can I call Method while app is in background in iOS?

I have one class name as myClassCalculate have the following method

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

NSLog(@"Accelerometer is called");

}

I am making object in

    - (void)applicationDidFinishLaunching:(UIApplication *)application {    


    // Override point for customization after app launch
       myObject = [[myClassCalculate alloc] init];

now when app will go in background, I want this

    - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {

What can I do, so that this method will get called in app background

Thanks

You can't, only VOIP, audio and navigation apps are allowed to run in background. Thus if your app does not fall in one of these it gets suspended and wil not receive any events.

More about backgrounding: App States and Multitasking

Declaring Your App's Supported Background Tasks

Support for some types of background execution must be declared in advance by the app that uses them. An app declares support for a service using its Info.plist file. Add the UIBackgroundModes key to your Info.plist file and set its value to an array containing one or more of the following strings:

audio — The app plays audible content to the user while in the background. (This content includes streaming audio or video content using AirPlay.)

location — The app keeps users informed of their location, even while it is running in the background.

voip — The app provides the ability for the user to make phone calls using an Internet connection.

newsstand-content — The app is a Newsstand app that downloads and processes magazine or newspaper content in the background.

external-accessory — The app works with a hardware accessory that needs to deliver updates on a regular schedule.

At WWDC this year they said that accelerometer updates could be observed in the background but I'm not sure how to do it. I think it would be iOS 5 only.

To do background tasks you need to add them to your info.plist in the UIBackgroundModes key. Here is what's available right now.

audio The application plays audible content in the background.

location The application provides location-based information to the user and requires the use of the standard location services (as opposed to the significant change location service) to implement this feature.

voip The application provides Voice-over-IP services. Applications with this key are automatically launched after system boot so that the application can reestablish VoIP services. Applications with this key are also allowed to play background audio.

newsstand-content The application processes content that was recently downloaded in the background using the Newsstand Kit framework, so that the content is ready when the user wants it. This value is supported in iOS 5.0 and later.

external-accessory The application communicates with an accessory that delivers data at regular intervals.

Try putting location in the plist or using [UIApplication beginBackgroundTaskWithHandler:] This value is supported in iOS 5.0 and later.

当应用进入背景时,此方法称为onUserLeaveHint()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM