5

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!

3
  • Chances are, you recompiled with iOS 8, so you should use the new method. So you should download the latest alpha for this as well. Aug 18, 2014 at 13:03
  • So, I updated Xamarin.iOS to version 7.9.3.52, and still not working. There is no method in NSUrlSessionConfiguration like "BackgroundSessionConfigurationWithIdentifier" and method "BackgroundSessionConfiguration" still use in export attribute "backgroundSessionConfiguration:" parameter.
    – SDmitry
    Aug 20, 2014 at 13:24
  • I'm facing this problem on iOS 7 as well.. Never updated to iOS 8, so this might be a general problem.
    – Mittchel
    Sep 23, 2014 at 6:13

2 Answers 2

9

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"];
}
8

This should work.

if(UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
 configDownload = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(sessionId);
}
else
{
configDownload = NSUrlSessionConfiguration.BackgroundSessionConfiguration(sessionId);
}
1
  • Yes, that's the correct answer, but only if you update your Xamarin iOS to version higher than 8.0.0.55. It was more complicated bug, and after my communication with Xamarin support, they fixed it.
    – SDmitry
    Sep 24, 2014 at 6:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.