iOS 13 Background kill - ios

How do you solve this problem : IOS 13 kept killing app instance after app entering background.
UIApplication *application = [UIApplication performSelector:#selector(sharedApplication)];
__block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
[self deleteOldFilesWithCompletionBlock:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];

I suppose that it's because you are trying to call bgTask
[application endBackgroundTask:bgTask];
and change it
bgTask = UIBackgroundTaskInvalid;
inside its definition.
Try to separate it for 2 different operations - bgTask definition and bgTask calling and changing.

Related

how to resume download when app is not active or screen off condition

I want to download one file from sever it come around 30/MB.
NSURLSessionDataTask are the function I used for download.I want to know how to implement download a file when app in background state or when screen off.
Add below in your - (void)applicationDidEnterBackground:(UIApplication *)application
UIApplication *app = [UIApplication sharedApplication];
UIBackgroundTaskIdentifier bgTask;
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:bgTask];
}];
This will work just fine.

iOS: How to call webservice when app is terminated by the user

I am trying to update the location to server when the app is not running. I am getting the location update when user location is changed but the web service is not getting called... Any help is greatly appreciated.
You need implementation an background task like this
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:#"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}

Handling of response in Background iOS

In my app , I am triggering many GET/POST request and handling the response. But just after initiating the request if I lock the device then on resuming the app after some seconds the response doesn`t come.Is there any way to let the request active even in the background or on the device locked state till the response comes. I went through the Background Execution Docs in iOS but that seems to be used if want to download or do something in background.Not fulfilling my scenario.
If you register a background task for each request, your app will keep running even when the device is locked, as long as a request is still active. The system only allows a maximum of 4 minutes though, so your requests mustn't take too long.
When you start a GET/POST request, do this:
__block UIBackgroundTaskIdentifier bgTask = 0;
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
}];
Then, store bgTask somewhere associated with your GET/POST request. Then when your request is complete, do this:
[[UIApplication sharedApplication] endBackgroundTask:bgTask];
Wen app is closed applicationDidEnterBackground was called so put this background task code. put timer or cal the function what ever u want.
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:#"MyTask" expirationHandler:^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0),
^{
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
//// just u call the method want ever u want example
[self notification];
// (or) timer
timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:#selector(notification:)
userInfo:nil
repeats:YES];
}
i think is help ful to u

Background timed task iOS

I'm developing an app in which I want to make a network call (syncing purpose) on an hourly basis. Even when the app is in the terminated state. How can I achieve this?
There is no UI Update involved anywhere, I just need to update local database and the cloud storage file.
developer apple BackgroundExecution
-(void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithName:#"MyTask" expirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
});
}
if([[UIDevice currentDevice] respondsToSelector:#selector(isMultitaskingSupported)])
{
__block UIBackgroundTaskIdentifier background_task;
background_task = [application beginBackgroundTaskWithExpirationHandler:^ {
[application endBackgroundTask: background_task];
background_task = UIBackgroundTaskInvalid;
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
while(YES)
{
[NSThread sleepForTimeInterval:your_sleep_time];
}
});
}
else
{
NSLog(#"Multitasking Not Supported");
}
We've created a new background task and add a new thread to it, so you can sleep the thread freely :)
I Hope I've help you

Background Task with NSOperation and NSOperationQueue iOS

I want to upload some files which should continue even if the application goes into background.
Currently I am retrieving the files from the DB and than adding it to the Queue via NSOperation which is then starts the upload procedure.
All the files should be uploaded even if the app goes to background or foreground.
Below is the code for single task can anyone give me a hint how we can make it to work for uploading many files.
UIApplication* application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
Why not try doing this? .. I haven't yet tried it out, I'll post an update after attempting this
UIApplication* application = [UIApplication sharedApplication];
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
// Bring up your NSOperation queue instance here and block this thread until it is complete
[queue waitUntilAllOperationsAreFinished];
[application endBackgroundTask: bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
also ensure that you have a way to cancel all these long standing operation in the background
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you.
// stopped or ending the task outright.
[queue cancelAllOperations];
[application endBackgroundTask:endSessionTask];
bgTask = UIBackgroundTaskInvalid;
}];
Apple allows background execution if your app follows and satisfy the necessary condition.
plese refer here

Resources