Tag: nsurlsession

支持服务器的iOS 9 ATS SSL错误

我安装了Xcode 7,并尝试在iOS 9下运行我的应用程序。我得到臭名昭着的错误: Connection failed! Error – -1200 An SSL error has occurred and a secure connection to the server cannot be made. Connection failed! Error – -1200 An SSL error has occurred and a secure connection to the server cannot be made. 事情是我的服务器支持TLSv1.2,我正在使用NSURLSession 。 那可能是什么问题?

内存泄漏时使用NSURLSession.downloadTaskWithURL

所以我在斯威夫特的努力中遇到了另一个障碍。 我正在尝试将多个图像加载到图像库中 – 除了一件事情之外,所有的工作都很好。 尽pipe我清除了图像,但应用程序的内存使用仍在不断增长和增长。 基本上所有的代码基本上,我发现这是由我的图像加载脚本引起的: func loadImageWithIndex(index: Int) { let imageURL = promotions[index].imageURL let url = NSURL(string: imageURL)! let urlSession = NSURLSession.sharedSession() let query = urlSession.downloadTaskWithURL(url, completionHandler: { location, response, error -> Void in }) query.resume() } 正如你所看到的,这段代码现在基本上什么都没有。 然而每次我打电话时,我的应用程序内存使用量都在增长。 如果我注释掉查询,内存使用情况不会改变。 我已经阅读了几个类似的问题,但都涉及使用一个代表。 那么,在这种情况下,没有代表还有内存问题。 有谁知道如何消除它,是什么造成的? 编辑:这是一个完整的testing类。 似乎内存只在图像可以加载时才会增长,就像指向图像的指针永远保存在内存中一样。 当图像没有find时,没有任何反应,内存使用率保持低。 也许有些提示如何清理这些指针? import UIKit class ViewController: UIViewController { override […]

与NSURLSessionasynchronous上传将无法正常工作,但同步NSURLConnection不会

编辑:我需要从iPhoneasynchronous上传文件到Python服务器端进程。 我想asynchronous地执行这个请求,这样我可以在工作时显示一个忙碌的animation。 该请求需要包含用户名,密码和文件“multipart / form-data”。 我可以使用NSURLConnection来同步工作,代码如下所示: -(void) uploadDatabase{ Database *databasePath = [[Database alloc] init]; NSString *targetPath = [databasePath getPathToDatabaseInDirectory]; NSData *dbData = [NSData dataWithContentsOfFile:targetPath]; NSString *url = @"http://mydomain.com/api/upload/"; //NSString *username = [[NSUserDefaults standardUserDefaults] stringForKey:USERNAME]; NSString *username = @"user"; NSString *password = @"pass"; NSMutableURLRequest *request = [self createRequestForUrl:url withUsername:username andPassword:password andData:dbData]; NSURLResponse *response; NSError *error; NSData […]

如何以编程方式将代理添加到NSURLSession

查看NSURLSession和NSURLSessionConfiguration的文档,我觉得我应该用像下面这样的字典来configuration它: // Create a dictionary to describe the proxy NSDictionary *proxyDict = @{ (NSString *)kCFProxyHostNameKey : @"myProxyHost.com", (NSString *)kCFProxyPortNumberKey : @"12345", (NSString *)kCFProxyTypeKey : (NSString*)kCFProxyTypeHTTP }; // Create a configuration that uses the dictionary NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; [configuration setConnectionProxyDictionary:proxyDict]; 但是,使用此configuration创build的NSURLSession的请求直接连接。

用NSURLSessionDataTask显示文件下载进度

我想显示特定文件的文件下载进度(接收多less字节)。 它与NSURLSessionDownloadTask正常工作。我的问题是我想实现与NSURLSessionDataTask相同。 下面是将文件接收到NSData并写入文档文件夹的代码: NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]]; NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:theRessourcesURL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { if(error == nil) { NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *pathToDownloadTo = [NSString stringWithFormat:@"%@/%@", docsDir, Name]; NSLog(@"SIZE : %@",[NSByteCountFormatter stringFromByteCount:data.length countStyle:NSByteCountFormatterCountStyleFile]); […]

在iOS10上继续NSUrlSession

iOS 10即将发布,因此值得testing与其兼容的应用程序。 在这样的testing中,我们发现我们的应用程序无法恢复iOS10上的后台下载。 在以前版本上运行良好的代码在仿真器和设备上都无法运行。 我没有将我们的代码减less到最小工作testing用例,而是在网上search了NSUrlSession教程并进行了testing。 行为是一样的:在iOS的previos版本上重新开始工作,但在10日中断。 重现步骤: 下载一个项目表单NSUrlSession教程https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started 直接链接: http : //www.raywenderlich.com/wp-content/uploads/2016/01/HalfTunes-Final.zip build立它并在iOS 10下启动。search一些东西,例如“swift”。 开始下载,然后点击“暂停”,然后“恢复” 预期成绩: 下载被恢复。 你可以检查它是如何与iOS10以前的版本一起工作的。 实际结果: 下载失败。 在xcode控制台中,您可以看到: 2016-09-02 16:11:24.913 HalfTunes[35205:2279228] *** -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL 2016-09-02 16:11:24.913 HalfTunes[35205:2279228] *** -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL 2016-09-02 16:11:24.913 HalfTunes[35205:2279228] Invalid resume data for background download. Background downloads must use http or https […]

如何在Swift中使用NSURLSession downloadTask按顺序下载多个文件

我有一个应用程序必须下载多个大文件。 我希望它逐一下载每个文件,而不是同时下载。 当它同时运行的应用程序得到超载和崩溃。 所以。 我试图包装在NSBlockOperation downloadTaskWithURL,然后在队列上设置maxConcurrentOperationCount = 1。 我在下面编写了这个代码,但它没有工作,因为两个文件同时下载。 import UIKit class ViewController: UIViewController, NSURLSessionDelegate, NSURLSessionDownloadDelegate { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. processURLs() } func download(url: NSURL){ let sessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration() let session = NSURLSession(configuration: sessionConfiguration, delegate: self, delegateQueue: nil) let downloadTask […]