Tag: nsurlconnection

NSURLCache不caching

我正在使用Xcode 6.1(6A1030),iOS7和iOS8模拟器。 NSURLCache似乎没有caching任何东西。 我使用“caching控制”标题。 我的服务器以'max-age = 6000'的forms返回Cache-Control头。 在这个例子中,我篡改了Google的一个请求,这个请求也没有被caching: AppDelegate中: func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { var URLCache = NSURLCache(memoryCapacity: 500 * 1024 * 1024, diskCapacity: 500 * 1024 * 1024, diskPath: "bla") NSURLCache.setSharedURLCache(URLCache) sleep(1) return true } 然后我开始连接: let urlPath: String = "http://www.google.com" var url: NSURL = NSURL(string: urlPath)! var request: […]

Swift:“ViewController.Type”没有名为“URL”的成员

我有我的viewController一些代码 var URL: NSURL = NSURL(string: "http://someurl.com")! var request : NSMutableURLRequest = NSMutableURLRequest(URL:URL) request.HTTPMethod = "POST" var bodyData = "data=something" NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response, data, error) in println(NSString(data: data, encoding: NSUTF8StringEncoding)) } 但是我得到的错误:“ViewController.Type”没有一个名为“URL”的成员。 如果我把代码放在ViewDidLoad中,它可以在应用程序启动时使用。 如何让代码在点击button上运行? 我试图把@IBAction放在ViewDidLoad的function,但是这也不工作:/ 编辑:全视图 // // ViewController.swift // postPhp // // Created by – on 01/12/2014. // Copyright (c) 2014 Dannie […]

如何从Web服务器更新JSON文件

我有我的本地数据在json文件中,如: { "locations": [ { "title": "The Pump Room", "place": "Bath", "latitude": 51.38131, "longitude": -2.35959, "information": "The Pump Room Restaurant in Bath is one of the city's most elegant places to enjoy stylish, Modern-British cuisine.", "telephone": "+44 (0)1225 444477", "visited" : true }, { "title": "The Eye", "place": "London", "latitude": 51.502866, "longitude": -0.119483, "information": "At […]

调用NSURLConnectionDataDelegate方法来下载图像只偶尔工作

我目前在我的故事板中有两个UIImageView ,其中一个下载我自己的Facebook个人资料图片,另一个是朋友的个人资料图片。 然而,我的问题是, 只有60%的时间按预期工作 ,而另外40%的时间是我自己的个人资料图片出现在我的朋友的照片应显示在底部,而顶部框仍然是空的。 我不确定这是否是我下载或完成视图时调用NSURLConnectionDataDelegate方法的结果,或者是调用到Facebook的请求的性质。 我已经将我的两个请求的精简版本粘贴到了viewDidLoad ,一个获取我自己的个人资料图片,另一个获取我的朋友的图片: // —– Gets my own profile picture, using requestForMe —– FBRequest *request = [FBRequest requestForMe]; [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) { //handle response if(!error){ //Some methods not included for breveity, includes facebookID used below NSURL *pictureURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large&return_ssl_resources=1", facebookID]]; self.imageData = [[NSMutableData alloc] […]

Http live streaming:强制MPMoviePlayerController在http请求中打包头文件

我使用Http Live Streaming将video从服务器传输到iPhone。 我需要在手机发送给服务器的http数据包中设置某些标头。 对于普通的数据包,下面的代码就足够了: NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:url] autorelease]; [request setValue:VALUE forHTTPHeaderField:@"Field You Want To Set"]; 不幸的是,我没有findMPMoviePlayerController类似的API。 换句话说,我该如何强制MPMoviePlayerController在它用来与服务器通信的http数据包中设置某些标头?

释放NSMutableData时NSURLConnection崩溃

我遵循使用NSURLConnection的指令,有时(很less)我的项目崩溃的方法。 – (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { [connection release]; [myNSMutableData release]; } 它崩溃时,我试图释放我的NSMutableData 。 我想知道它为什么崩溃! 我使用的一些代码: – (void) start { while (1) { NSString *stringURL = @"http://www.iwheelbuy.com/get.php?sex=1"; NSURL *url = [NSURL URLWithString:stringURL]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (connection) { getData = [[NSMutableData data] retain]; […]

UITableView委托在NSURL委托之后没有被调用

我正在尝试创build一个iOS应用程序,从Web服务获取一些数据并将其显示在自定义单元格中。 到目前为止,我可以通过NSLog cmdvalidation数据实际上是在相应的数组中。 这意味着连接代表被调用。 但不幸的是不是TableView代表。 或者让他们说,他们正在打电话,但也许在错误的地方? 我也尝试在connectionDidFinishLoading方法之后重新加载Table视图,但它不起作用。 任何想法是什么错误? 这也是非常有趣的第一次我的NSLog被调用我有0个对象在数组中,第二次连接后,我有25个对象。 这是应该的。 但仍然。 表单元格是空的… 我的表格视图代表: – (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{ return _matchesArray.count; } – – (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSLog(@"%lu", (unsigned long)_matchesArray.count); return _matchesArray.count; } – – (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIdentifier = @"Cell"; ResultCell * cell = (ResultCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) […]

清除NSURLSessioncaching

我使用NSURLSession来执行一个HTTP Post NSMutableURLRequest ,使用dataTaskWithRequest:completionHandler:方法。 当我第一次执行请求时,需要花费合理的时间来完成并在完成处理程序中显示一些反馈。 在下一次同样的请求被触发时,它几乎是瞬间发生,两者之间的时间很less,这导致我相信系统正在caching这个数据任务的内容。 由于我不需要查看返回的数据,NSURLSession是否是最好的方法呢? 它需要与WatchKit,NSURLSession做的很好,这就是为什么我select在拳头的地方。 我最好想find一种方法来清除每个请求后的caching。 如果需要的话,我可以切换到NSURLConnection,但这是最好的。 谢谢!

如何处理不受信任的服务URL?

我正在使用无效证书服务器上的Web服务,但我想要得到响应。 有没有办法做到这一点? 以下是示例代码: -(void)createHttpHeaderRequest:(serviceType)type { NSMutableURLRequest * theRequest; if (type==kServiceTypeTopAccessory) { NSString * test = @"{\"GetVehicleInventory\": {\"ApplicationArea\": some Json object here}}}}}"; NSString * final = (__bridge NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (__bridge CFStringRef)test, NULL, CFSTR(":/?#[]@!$&'()*+,;=\""), kCFStringEncodingUTF8); NSString * sampleReq = [NSString stringWithFormat:@"https://myuntrutsedServer?XML_INPUT=%@",final]; // NSString * final = [sampleReq stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; NSLog(@"JSON:%@",sampleReq); theRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:sampleReq]]; } NSLog(@"Request:%@",theRequest); […]

iPhone 5 vs 5s的NSHTTPURLResponse不同

这是我见过的最奇怪的事情。 我有一个iPhone 5和iPhone 5s,都在iOS 7.1.2,完全干净。 我运行下面的代码: NSString *userAgent = @"Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3"; NSURL *url = [NSURL URLWithString: deviceString]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url]; [request setValue:userAgent forHTTPHeaderField:@"User-Agent"]; NSHTTPURLResponse *response = nil; NSError *error = nil; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; […]