I want my iOS app to download some product information from the background to the server. It should be active when I could get data using app active and passive mode
NSURLSessionDataTask
but the data can not be processed in passive mode.
Can you please guide me, if the app is in the background / inactive state, then can I process the data? Or any other suggestions.
See my code below:
NSURLSessionconfiguration * sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURL session * session = [configuration with NSURL sessions session: session configuration delegation: self-delegate queue: zero]; NSURLSessionDataTask * sessinURLResData = [session datatoscwith url: urlIs complete handler: ^ (NSDT * data, NSRRSpons * response, NSERR * error) {feedback data = data; // data process and save in local DB]]; [SesinURLResData resume];
There are at least two types of backgrounds:
-
If you want to check the data from time to time in the foreground, you can enable "background fun" in the app's "capabilities", and then your app
performFetchWithCompletionHandler
to the representative. You can only request a quick (30 seconds or less) network but your app will make the network request, and the app representative will call the appropriate completion operator code, either report the failure, the presence of new data, or the new data Deficit:- (zero) Application: (UIApplication *) application performFetchWithCompletionHandler: (Zero (^) (UIBackgroundFetchResult)) Finishing Handler {NSURL * url = [NSURL URLWithString: @ "... "]; NSURL session * session = [NSURL session sharing session]; NSURLSessionTask * task = [session datatoscwith url: url completionHandler: ^ (NSDT * data, NSRRSpons * response, NSER * error) {if (error) {completionHandler (UIBackgroundFetchResultFailed); Return; } BOOL found new data; // Parsing data; Setting 'foundNewData` in proper form if (found new data) {completionHandler (UIBackgroundFetchResultNewData); } Else {completionHandler (UIBackgroundFetchResultNoData); }}]; [Resume work]; }
-
A completely different background type of background network request is
NSURL session configuration
, large upload / upload to it Good for background For this, any upload / upload started by you will continue even when the app is no longer in the foreground (even if the app has ended), and when you do all that, your app will restart if necessary. And called appropriate representative methods. But, to do this, you must:-
use
[NSURLSessionConfiguration backgroundSessionConfiguration: identifier]
; -
Use the representative-based
NSURL session
(which are you); -
Use download or upload functions, not data work;
-
Use the download / upload function factory method that uses the not completion block (you have to apply representative methods);
-
Respond to the application representative on the
handleEventsForBackgroundURL session
, capturethe completed handler
; And -
Apply
URLSessionDidFinishEventsForBackgroundURL session:
in the session rep, which will callcompletionHandler
, when you will process everything
-
There are other techniques to request backgrounds, but these two major iOS7 specific background are networking. You have not told us what you are using, and we do not know that enough code has been shown to find out what's going on wrong.
Comments
Post a Comment