UI中的网络请求

@interface ViewController ()<NSURLConnectionDataDelegate>

{

CGFloat totleLength;

NSMutableData *filedata;

BOOL isDownload;

CGFloat reciveTotle;

NSString *filePath;

NSURLConnection *_connection;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

totleLength = [[userDefaults objectForKey:@"totleLength"] floatValue];

reciveTotle = [[userDefaults objectForKey:@"reciveTotle"] floatValue];

if (reciveTotle > 0) {

CGFloat progress = reciveTotle / totleLength;

self.progressView.progress = progress;

self.progressLabel.text = [NSString stringWithFormat:@"%.f%%",progress * 100];

}

}

- (IBAction)btnClick:(UIButton *)sender {

if (isDownload) {

return;

}

NSURL *url = [NSURL URLWithString:@"http://free2.macx.cn:8182/game/BombSquadX401.dmg"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

if (reciveTotle > 0) {

NSString *value = [NSString stringWithFormat:@"bytes=%d-",(int)reciveTotle];

[request setValue:value forHTTPHeaderField:@"Range"];

}

_connection = [NSURLConnection connectionWithRequest:request delegate:self];

isDownload = true;

NSString *str = url.absoluteString;

NSString *strName = [str lastPathComponent];

filePath = [NSHomeDirectory() stringByAppendingFormat:@"/Documents/%@",strName];

if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

[[NSFileManager defaultManager]createFileAtPath:filePath contents:nil attributes:nil];

}

}

- (IBAction)pauseAction:(UIButton *)sender {

[_connection cancel];

_connection  = nil;

[self appendFileData:filedata];

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

[userDefaults setObject:@(reciveTotle) forKey:@"reciveTotle"];

[userDefaults setObject:@(totleLength) forKey:@"totleLength"];

[userDefaults synchronize];

isDownload = NO;

}

#pragma mark-NSURLConnectionDataDelegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSHTTPURLResponse *)response{

filedata = [[NSMutableData alloc]init];

NSDictionary *dic = response.allHeaderFields;

NSNumber *number = [dic objectForKey:@"Content-Length"];

totleLength = [number floatValue];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

[filedata appendData:data];

reciveTotle += data.length;

self.progressView.progress = reciveTotle / totleLength;

self.progressLabel.text = [NSString stringWithFormat:@"%.f%%",self.progressView.progress * 100];

if (filedata.length >= 500 * 1000) {

[self appendFileData:filedata];

filedata.data = nil;

}

}

- (void)appendFileData:(NSData *)data

{

if (data.length == 0) {

return;

}

NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];

[fileHandle seekToEndOfFile];

[fileHandle writeData:data];

[fileHandle closeFile];

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

if(filedata.length < 500 * 1000){

[self appendFileData:filedata];

[filedata setData:nil];

[filedata writeToFile:filePath atomically:YES];

}

self.progressLabel.text = @"下载完成";

isDownload = false;

}

				
时间: 2024-07-29 06:19:08

UI中的网络请求的相关文章

iOS开发中的网络请求

今天来说说关于iOS开发过程中的网络请求. 关于网络请求的重要性我想不用多说了吧.对于移动客户端来说,网络的重要性不言而喻.常见的网络请求有同步GET, 同步POST, 异步GET, 异步POST.今天来看一下四种网络请求的实现方式. 一.同步GET // 1.将网址初始化成一个OC字符串对象 NSString *urlStr = [NSString stringWithFormat:@"%@?query=%@&region=%@&output=json&ak=6E823

React Native中的网络请求

React Native中的网络请求fetch使用方法最为简单,但却可以实现大多数的网络请求,需要了解更多的可以访问: https://segmentfault.com/a/1190000003810652 /** * Sample React Native App * https://github.com/facebook/react-native * 周少停 2016-09-28 * fetch请求数据 header 参数 response转json 请求方式 */ import React

ios中封装网络请求类

ios中封装网络请求类 #import "JSNetWork.h" //asiHttpRequest #import "ASIFormDataRequest.h" //xml 的解析 #import "UseXmlParser.h" //判断是否联网 #import "Reachability.h" //sbJson,判断json的解析 #import "JSON.h" @implementation JS

swift中第三方网络请求库Alamofire的安装与使用

swift中第三方网络请求库Alamofire的安装与使用 Alamofire是swift中一个比较流行的网络请求库:https://github.com/Alamofire/Alamofire.下面我们就介绍一个这个库的安装与使用. 一.安装包管理工具CocoaPods 由于国内容易被墙,速度也非常的慢,这里我们使用提供的源 http://ruby.taobao.org/ gem sources --remove https://rubygems.org/ gem sources –a htt

IOS中的网络请求

使用NSURLConnection的网络请求,最好定义一个类方法,在主线程中直接调用类方法获取请求到的网络数据 //构建类方法--请求网络 + (void)requestData:(NSString *)urlStr httpMethod:(NSString *)method params:(NSMutableDictionary *)params comletionHandle:(void (^)(id result))block { //1.构建URL urlStr = [BASE_URL

cchttpclient中停止网络请求的方法

cchttpclient是异步网络连接,在网速慢的时候,应用退出了该页面,未执行的网络请求还是按照队列请求下去,这导致重新进入页面时候,新的网络请求的得不到快速响应.而cchttpclient类中并没有提供移除请求的方法,查看源码,发现所有的请求都放在s_requestQueue数组中,那么要做的就是在退出界面时候,清空这个请求队列.如下是在httpclient.cpp中添加的禁止所有请求的方法: void CCHttpClient::stopAllResquest(){ CCHttpReque

Android中解析网络请求的URL

最近正在做Android网络应用的开发,使用了android网络请求方面的知识,现在向大家介绍网络请求方面的知识,我们知道android中向服务器端发送一个请求,(这就是我们通常所说的POST请求),我们要发送一个完整的URL,然后服务器端接收到这个URL,对这个URL进行特定的解析,就是对URL进行解析,转化为JSON数据,然后,我们只要处理这个JSON数据就可以了. 我现在就用我的项目实例来体现解析URL的过程: 1.组装URL的过程: private String getOrderPayU

关于Xcode 7中 HTTP网络请求失败的解决办法

最近在用NSURLSession请求网络数据时,会出现如下错误, 查询后得知iOS9之后,新特性App Transport Security (ATS)要求App内访问的网络必须使用HTTPS协议,解决方法如下:

在iOS9中 xcode7 网络请求 如图片请求不显示等

Application Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.bao这样的问题则 编辑 info.plist,加入如下设置: .... NSAppTransportSecurity NSAllowsArbitr