1.檢測(cè)網(wǎng)絡(luò)狀態(tài)
+ (void)netWorkStatus
{
? ? /**
? ? AFNetworkReachabilityStatusUnknown? ? ? ? ? = -1,? // 未知
? ? AFNetworkReachabilityStatusNotReachable? ? = 0,? // 無連接
? ? AFNetworkReachabilityStatusReachableViaWWAN = 1,? // 3G 花錢
? ? AFNetworkReachabilityStatusReachableViaWiFi = 2,? // WiFi
? ? */
? ? // 如果要檢測(cè)網(wǎng)絡(luò)狀態(tài)的變化,必須用檢測(cè)管理器的單例的startMonitoring
? ? [[AFNetworkReachabilityManager sharedManager] startMonitoring];
? ? // 檢測(cè)網(wǎng)絡(luò)連接的單例,網(wǎng)絡(luò)變化時(shí)的回調(diào)方法
? ? [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
? ? ? ? NSLog(@"%ld", status);
? ? }];
}
2.JSON方式獲取數(shù)據(jù)
+ (void)JSONDataWithUrl:(NSString *)url success:(void (^)(id json))success fail:(void (^)())fail;
{
? ? AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
? ? NSDictionary *dict = @{@"format": @"json"};
? ? // 網(wǎng)絡(luò)訪問是異步的,回調(diào)是主線程的,因此程序員不用管在主線程更新UI的事情
? ? [manager GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
? ? ? ? if (success) {
? ? ? ? ? ? success(responseObject);
? ? ? ? }
? ? } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
? ? ? ? NSLog(@"%@", error);
? ? ? ? if (fail) {
? ? ? ? ? ? fail();
? ? ? ? }
? ? }];
}
3.XML方式獲取數(shù)據(jù)
+ (void)XMLDataWithUrl:(NSString *)urlStr success:(void (^)(id xml))success fail:(void (^)())fail
{
? ? AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
? ? // 返回的數(shù)據(jù)格式是XML
? ? manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
? ? NSDictionary *dict = @{@"format": @"xml"};
? ? // 網(wǎng)絡(luò)訪問是異步的,回調(diào)是主線程的,因此程序員不用管在主線程更新UI的事情
? ? [manager GET:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
? ? ? ? if (success) {
? ? ? ? ? ? success(responseObject);
? ? ? ? }
? ? } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
? ? ? ? NSLog(@"%@", error);
? ? ? ? if (fail) {
? ? ? ? ? ? fail();
? ? ? ? }
? ? }];
}
4.post提交json數(shù)據(jù)
+ (void)postJSONWithUrl:(NSString *)urlStr parameters:(id)parameters success:(void (^)(id responseObject))success fail:(void (^)())fail
{
? ? AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
? ? // 設(shè)置請(qǐng)求格式
? ? manager.requestSerializer = [AFJSONRequestSerializer serializer];
? ? // 設(shè)置返回格式
? ? manager.responseSerializer = [AFHTTPResponseSerializer serializer];
? ? [manager POST:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//? ? ? ? NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
? ? ? ? if (success) {
? ? ? ? ? ? success(responseObject);
? ? ? ? }
? ? } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
? ? ? ? NSLog(@"%@", error);
? ? ? ? if (fail) {
? ? ? ? ? ? fail();
? ? ? ? }
? ? }];
}
5.下載文件
+ (void)sessionDownloadWithUrl:(NSString *)urlStr success:(void (^)(NSURL *fileURL))success fail:(void (^)())fail
{
? ? NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
? ? AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:config];
? ? NSString *urlString = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
? ? NSURL *url = [NSURL URLWithString:urlString];
? ? NSURLRequest *request = [NSURLRequest requestWithURL:url];
? ? NSURLSessionDownloadTask *task = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
? ? ? ? // 指定下載文件保存的路徑
? ? ? ? //? ? ? ? NSLog(@"%@ %@", targetPath, response.suggestedFilename);
? ? ? ? // 將下載文件保存在緩存路徑中
? ? ? ? NSString *cacheDir = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
? ? ? ? NSString *path = [cacheDir stringByAppendingPathComponent:response.suggestedFilename];
? ? ? ? // URLWithString返回的是網(wǎng)絡(luò)的URL,如果使用本地URL,需要注意
//? ? ? ? NSURL *fileURL1 = [NSURL URLWithString:path];
? ? ? ? NSURL *fileURL = [NSURL fileURLWithPath:path];
//? ? ? ? NSLog(@"== %@ |||| %@", fileURL1, fileURL);
? ? ? ? if (success) {
? ? ? ? ? ? success(fileURL);
? ? ? ? }
? ? ? ? return fileURL;
? ? } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
? ? ? ? NSLog(@"%@ %@", filePath, error);
? ? ? ? if (fail) {
? ? ? ? ? ? fail();
? ? ? ? }
? ? }];
? ? [task resume];
}
6.文件上傳-自定義上傳文件名
+ (void)postUploadWithUrl:(NSString *)urlStr fileUrl:(NSURL *)fileURL fileName:(NSString *)fileName fileType:(NSString *)fileTye success:(void (^)(id responseObject))success fail:(void (^)())fail
{
? ? // 本地上傳給服務(wù)器時(shí),沒有確定的URL,不好用MD5的方式處理
? ? AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
? ? manager.responseSerializer = [AFHTTPResponseSerializer serializer];
? ? //@"http://localhost/demo/upload.php"
? ? [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//? ? ? ? NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"頭像1.png" withExtension:nil];
? ? ? ? // 要上傳保存在服務(wù)器中的名稱
? ? ? ? // 使用時(shí)間來作為文件名 2014-04-30 14:20:57.png
? ? ? ? // 讓不同的用戶信息,保存在不同目錄中
//? ? ? ? NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//? ? ? ? // 設(shè)置日期格式
//? ? ? ? formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
//? ? ? ? NSString *fileName = [formatter stringFromDate:[NSDate date]];
? ? ? ? //@"image/png"
? ? ? ? [formData appendPartWithFileURL:fileURL name:@"uploadFile" fileName:fileName mimeType:fileTye error:NULL];
? ? } success:^(AFHTTPRequestOperation *operation, id responseObject) {
? ? ? ? if (success) {
? ? ? ? ? ? success(responseObject);
? ? ? ? }
? ? } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
? ? ? ? if (fail) {
? ? ? ? ? ? fail();
? ? ? ? }
? ? }];
}
7.文件上傳-隨機(jī)生成文件名
+ (void)postUploadWithUrl:(NSString *)urlStr fileUrl:(NSURL *)fileURL success:(void (^)(id responseObject))success fail:(void (^)())fail
{
? ? AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
? ? // AFHTTPResponseSerializer就是正常的HTTP請(qǐng)求響應(yīng)結(jié)果:NSData
? ? // 當(dāng)請(qǐng)求的返回?cái)?shù)據(jù)不是JSON,XML,PList,UIImage之外,使用AFHTTPResponseSerializer
? ? // 例如返回一個(gè)html,text...
? ? //
? ? // 實(shí)際上就是AFN沒有對(duì)響應(yīng)數(shù)據(jù)做任何處理的情況
? ? manager.responseSerializer = [AFHTTPResponseSerializer serializer];
? ? // formData是遵守了AFMultipartFormData的對(duì)象
? ? [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
? ? ? ? // 將本地的文件上傳至服務(wù)器
//? ? ? ? NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"頭像1.png" withExtension:nil];
? ? ? ? [formData appendPartWithFileURL:fileURL name:@"uploadFile" error:NULL];
? ? } success:^(AFHTTPRequestOperation *operation, id responseObject) {
//? ? ? ? NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
//? ? ? ?
//? ? ? ? NSLog(@"完成 %@", result);
? ? ? ? if (success) {
? ? ? ? ? ? success(responseObject);
? ? ? ? }
? ? } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
? ? ? ? NSLog(@"錯(cuò)誤 %@", error.localizedDescription);
? ? ? ? if (fail) {
? ? ? ? ? ? fail();
? ? ? ? }
? ? }];
}