1. VoIP
1) First, modify the plist configuration of the application and add the following settings:
Application does not run in Background: No
Required background modes: VoIP
Note: after these configurations are added, the application will automatically run after the program is restarted and unlocked:
Didfinishlaunchingwitexceptions method.
2) Main test code:
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
{
_ Block uibackgroundtaskidentifier background_task;
Background_task = [Application beginbackgroundtaskwithexpirationhandler: ^ {
// Open the background to run the task
}];
Static dispatch_queue_t _ queue;
_ Queue = dispatch_queue_create ("sddfgfgwerty456567fre4ghghkjdsfbnjfrtyrt", dispatch_queue_serial );
Dispatch_async (_ queue, ^ {
While (true)
{
// Print the process ID
Nslog (@ "processid = % d ++ ++ currentthread =% @", [ssprocessinfo processid], [[nsthread currentthread] description]);
[Nsthread sleepfortimeinterval: 5]; // wait for 15 min
}
});
Sleep (1 );
Return yes;
}
Note: The program must be tested on a real machine. Use the organizer tool on xcode to observe the log information.
After analyzing the log information, it is found that processid will change after a period of time, indicating that the program will be killed after a period of time, and then the program will automatically restart. Although it achieves the purpose of continuous operation, it does not meet the requirements of continuous operation of the program.
2. GPS
1) First, modify the plist configuration of the application and add the following settings:
Application does not run in Background: No
Required background modes: app registers for location updates
After adding these two items, you can ensure that the program runs in the background.
2) Main test code:
-(Bool) Application :( uiapplication *) Application didfinishlaunchingwitexceptions :( nsdictionary *) launchoptions
{
If ([cllocationmanager significantlocationchangemonitoringavailable])
{
[Self. locationmanager startupdatinglocation];
}
Else
{
Nslog (@ "significant location change monitoring is not available .");
}
Return yes;
}
-(Cllocationmanager *) locationmanager
{
If (_ locationmanager = nil ){
_ Locationmanager = [[cllocationmanager alloc] init];
_ Locationmanager. Delegate = self;
If ([cllocationmanager locationservicesenabled])
{
Nslog (@ "Unlimited run .................................. .............. ");
// After this branch enters the background, the running time is the same as that of the foreground, Which is unlimited.
[_ Locationmanager startmonitoringsignificantlocationchanges];
_ Locationmanager. desiredaccuracy = kcllocationaccuracynearesttenmeters;
_ Locationmanager. distancefilter = 1;
_ Locationmanager. pauseslocationupdatesautomatically = no;
_ Locationmanager. activitytype = clactivitytypeautomotivenavigation;
[_ Locationmanager startupdatinglocation];
}
}
Return _ locationmanager;
}
-(Void) locationmanager :( cllocationmanager *) manager didupdatelocations :( nsarray *) Locations
{
While (true ){
[Nsthread sleepfortimeinterval: 1];
Nslog (@ "processid = % d ++ ++ currentthread =% @", [ssprocessinfo processid], [[nsthread currentthread] description]);
[Self. locationmanager allowdeferredlocationupdatesuntiltraveled: cllocationdistancemax Timeout: 10];
}
}
Note: The program must be tested on a real machine. Use the organizer tool on xcode to observe the log information.
After analyzing the log information, it is found that processid does not change, indicating that the program can continue to run.