文件下載 監聽網絡

#import "ViewController.h"
#import "WebImageView.h"
#import "ASIHTTPRequest.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadView
{
    [super loadView];
    
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//    button.frame = CGRectMake(100, 100, 80, 40);
//    [button setTitle:@"button" forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(onclick) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button];

    UIButton *download = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    download.frame = CGRectMake(100, 140, 80, 40);
    [download setTitle:@"download" forState:UIControlStateNormal];
    [download addTarget:self action:@selector(downloadAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:download]; 
}

- (void)onclick
{
    for (int i = 0; i < 10; i ++){
        //        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, i * 50, 50, 50)];
        //        [imageview setImageWithURL:[NSURL URLWithString:@"http://a.hiphotos.baidu.com/image/w%3D2048/sign=0ef89c67cf1b9d168ac79d61c7e6b58f/a71ea8d3fd1f4134ef340bc6241f95cad1c85e02.jpg"]];
        //        [self.view addSubview:imageview];
        //        [imageview release];
        
        WebImageView *imageview = [[WebImageView alloc]initWithFrame:CGRectMake(0, i * 50, 50, 50)];
        [imageview setImageURL:[NSURL URLWithString:@"http://a.hiphotos.baidu.com/image/w%3D2048/sign=0ef89c67cf1b9d168ac79d61c7e6b58f/a71ea8d3fd1f4134ef340bc6241f95cad1c85e02.jpg"]];
        [self.view addSubview:imageview];
        [imageview release];
    }
}

- (void)downloadAction
{
    UIProgressView *progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
    progressView.frame = CGRectMake(50, 100, 200, 20);
    //keypath 是屬性
    //監聽UIProgressView的值變化,通過kvo
    [progressView addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil];
    [self.view addSubview:progressView];
    [progressView release];
    
    //文件下載
    NSString *urlString = @"http://a.hiphotos.baidu.com/image/w%3D2048/sign=0ef89c67cf1b9d168ac79d61c7e6b58f/a71ea8d3fd1f4134ef340bc6241f95cad1c85e02.jpg";
    //文件存放路徑
    NSString *document = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *fileName = [urlString lastPathComponent];//文件名
    NSString *path = [document stringByAppendingPathComponent:fileName];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString]];
    
    [request setHeadersReceivedBlock:^(NSDictionary *responseHeader){
       // NSDictionary *dictory = request.responseHeaders;
        NSLog(@"%@", responseHeader);
        //文件大小
        NSNumber *contentsize = [responseHeader objectForKey:@"Content-Length"];
        NSLog(@"文件大小:%@", contentsize);
    }];
    [request setDownloadDestinationPath:path];
    request.downloadProgressDelegate = progressView;//設置下載的進度條
    // 使用異步下載
    [request startAsynchronous];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //NSLog(@"%@", change);
    NSNumber *value = [change objectForKey:@"new"];
    float progress = [value floatValue];
    NSLog(@"......%.1f%%", progress * 100);
}
@end


監聽網絡


    //監聽網絡
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changNet:)
                                                 name:kReachabilityChangedNotification object:nil];
    //獲取監聽對象
    self.reachability = [Reachability reachabilityForInternetConnection];
    //開始監聽
    [self.reachability startNotifier];
    NetworkStatus status = self.reachability.currentReachabilityStatus;
    [self checkNetWork:status];
}

- (void)changNet:(NSNotification *)notification
{
    NetworkStatus status = self.reachability.currentReachabilityStatus;
    [self checkNetWork:status];
}

- (void)checkNetWork:(NetworkStatus)statu
{
    if (statu == kNotReachable){
        NSLog(@"no net");
    }else if (statu == kReachableViaWWAN){
        NSLog(@"3G/2G");
    }else if (statu == kReachableViaWiFi){
        NSLog(@"wifi");
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章