Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS8: backgroundSessionConfiguration is deprecated

Before the iOS8 will be released, I downloaded iOS8 SDK to watch how my application will be work. Sometimes my app need to download data from server and I use NSUrlSession for it. And now, when app preparing session for downloading, appears the next warning:

+backgroundSessionConfiguration: is deprecated. Please use +backgroundSessionConfigurationWithIdentifier: instead.

After it my app crashes with the next exception:

Unable to cast object of type 'MonoTouch.Foundation.NSUrlSessionTask' (Objective-C type: '__NSCFBackgroundDownloadTask') to type 'MonoTouch.Foundation.NSUrlSessionDownloadTask'.
Additional information:
    Selector: URLSession:downloadTask:didFinishDownloadingToURL:
    Method: Microsoft.Synchronization.ClientServices.NSUrlDownloadDelegate:DidFinishDownloading (MonoTouch.Foundation.NSUrlSession,MonoTouch.Foundation.NSUrlSessionDownloadTask,MonoTouch.Foundation.NSUrl)

Code for creating NSUrlSession:

NSUrlSessionConfiguration sessionConfiguration = NSUrlSessionConfiguration.BackgroundSessionConfiguration(urlSessioinId);
NSUrlDownloadDelegate downloadDelegate = new NSUrlDownloadDelegate();
NSUrlSession downloadSession = NSUrlSession.FromConfiguration(sessionConfiguration, downloadDelegate, new NSOperationQueue());

Thanks for any help!

like image 662
SDmitry Avatar asked Aug 15 '14 10:08

SDmitry


2 Answers

I have use following to solve this error in Objective-C :

NSURLSessionConfiguration *sessionConfig;

if ([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0f)
{
    sessionConfig =[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"downloads"];
}
else
{
    sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:@"downloads"];
}
like image 157
Vishal Khatri Avatar answered Oct 01 '22 17:10

Vishal Khatri


This should work.

if(UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
 configDownload = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(sessionId);
}
else
{
configDownload = NSUrlSessionConfiguration.BackgroundSessionConfiguration(sessionId);
}
like image 44
Edwin van Vliet Avatar answered Oct 01 '22 17:10

Edwin van Vliet