在发布iOS8之前,我下载了iOS8 SDK,观看我的应用程序是如何工作的。有时我的应用程序需要从服务器下载数据,我使用NSUrlSession。现在,当app准备下载会话时,会出现下一个警告:
+backgroundSessionConfiguration: is deprecated. Please use +backgroundSessionConfigurationWithIdentifier: instead
。
之后我的应用程序因下一个异常而崩溃:
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)
创建NSUrlSession的代码:
NSUrlSessionConfiguration sessionConfiguration = NSUrlSessionConfiguration.BackgroundSessionConfiguration(urlSessioinId);
NSUrlDownloadDelegate downloadDelegate = new NSUrlDownloadDelegate();
NSUrlSession downloadSession = NSUrlSession.FromConfiguration(sessionConfiguration, downloadDelegate, new NSOperationQueue());
感谢您的帮助!
答案 0 :(得分:9)
我在Objective-C中使用以下来解决这个错误:
NSURLSessionConfiguration *sessionConfig;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >=8.0f)
{
sessionConfig =[NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"downloads"];
}
else
{
sessionConfig = [NSURLSessionConfiguration backgroundSessionConfiguration:@"downloads"];
}
答案 1 :(得分:8)
这应该有用。
if(UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
configDownload = NSUrlSessionConfiguration.CreateBackgroundSessionConfiguration(sessionId);
}
else
{
configDownload = NSUrlSessionConfiguration.BackgroundSessionConfiguration(sessionId);
}