DLDownload 0.4.5

DLDownload 0.4.5

测试已测试
语言语言 Obj-CObjective C
许可 自定义
发布最新发布2014年12月

未声明 维护。



  • Donald Hays

DLDownload是围绕NSURLConnection的一个轻量级块封装。

安装

  • pod "DLDownload" 添加到您的 Podfile,根据需要导入: #import "DLDownload.h"
  • DLDownload.{h,m} 添加到您的项目,根据需要导入: #import "DLDownload.h"

文档

Github Pages上可以找到DLDownload的文档。

贡献

请随意 Fork代码,并提交任何新特性或修复的拉请求。

使用方法

简单的HTTP调用

DLDownload *download = [[DLDownload alloc] init];
download.url = [NSURL URLWithString:@"http://track8.fm/api/track"];
download.parameters = @{@"short_code":@"rA"};
download.callback =  ^(NSData *data, NSError *error) {
    if(error) {
        // handle error.
    } else {
        NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:0];
        NSDictionary *trackData = [jsonData objectForKey:@"track"];
        NSLog(@"%@", [trackData objectForKey:@"name"]);
    }  
};

[download start];

HTTP方法

支持的HTTP方法包括: GETPOSTDELETEPUTPATCH

DLDownload *download = [[DLDownload alloc] init];
download.method = DLDownloadMethodPOST;
download.url = [NSURL URLWithString:@"http://track8.fm/api/track"];
download.parameters = @{@"artist":@"Finch", @"track":@"Ender"};
download.callback =  ^(NSData *data, NSError *error) {
    if(error) {
        // handle error.
    } else {
        NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:0];
        NSDictionary *trackData = [jsonData objectForKey:@"track"];
        NSLog(@"%@ : %@", [trackData objectForKey:@"name"], [trackData objectForKey:@"linkURL"]);
    }
};

[download start];

监控下载进度

DLDownload *download = [[DLDownload alloc] init];
download.method = DLDownloadMethodPOST;
download.url = [NSURL URLWithString:@"http://imgsrc.hubblesite.org/hu/db/images/hs-2006-10-a-hires_jpg.jpg"];
download.updateProgressCallback = ^(NSUInteger bytesReceived, NSUInteger expectedLength, CGFloat percent) {
    NSLog(@"%i%% Complete", (int)(percent * 100));
};
download.callback =  ^(NSData *data, NSError *error) {
    if(error) {
        // handle error.
    } else {
        UIImage *hubbleImage = [UIImage imageWithData:data];
        self.someImageView.image = hubbleImage;
    }
};

[download start];

HTTP头部

NSString *authorizationHeader = [NSString stringWithFormat:@"Bearer %@", @"UserServiceAccessToken"];
NSMutableDictionary *headerFields = @{@"Authorization":authorizationHeader, @"content-type": @"application/json" };

DLDownload *download = [[DLDownload alloc] init];
download.HTTPHeaderFields = headerFields;

// define properties and methods here.

[download start];

HTTP正文

DLDownload *download = [[DLDownload alloc] init];
download.bodyData = [NSJSONSerialization dataWithJSONObject:@{@"text":@"Message to post to a social network."} options:0 error:0];

// define properties and methods here.

[download start];

HTTP认证挑战

DLDownload *download = [[DLDownload alloc] init];
download.credential = [NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];

// define properties and methods here.

[download start];

通知

  • DLDownloadDidBeginNotification
  • DLDownloadDidEndNotification

说明

您不需要对参数进行编码。所有参数在调用之前都由DLDownload正确编码。