你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS開發中對於NSURLRequest的封裝

iOS開發中對於NSURLRequest的封裝

編輯:IOS開發綜合

在ios開發中經常用到NSURLRequest類來進行url請求,通常有以下步驟

1.實例化NSURL;

2.實例化NSURLRequest;

3.連接[NSURLConnection connectionWithRequest:request delegate:self];

4.實現NSURLConnectionDataDelegate協議;

這樣的話比較繁瑣,可以對其進行封裝

1.新建一個MyUrlRequest類

MyUrlRequest.h文件

@interface MyUrlRequest : NSObject  {
    NSMutableData *_mData;//接收數據
}  

@property (nonatomic, copy) NSString *urlStr;
@property (nonatomic, copy) void (^finishBlock)(NSData *data);//請求成功回調block
@property (nonatomic, copy) void (^failedBlick)();//請求失敗回調block
@property (nonatomic, assign) BOOL isCache;//是否緩存

- (void)startRequest;//開始請求

@end

MyUrlRequest.m文件

#import "MyUrlRequest.h"
#import "NSString+Hashing.h"

@implementation QFURLRequest

@synthesize urlStr;
@synthesize finishBlock;
@synthesize failedBlick;
@synthesize isCache;

- (id)init {
    if (self = [super init]) {
        _mData = [[NSMutableData alloc] init];
    }
    return self;
}

- (void)startRequest {
    //MyRequestManager裡邊autorelease掉,在這裡可能出錯,所以需要retain
    [self retain];
    if (self.isCache) {
        //如果有緩存,則使用緩存
        NSString *path = [NSHomeDirectory() stringByAppendingFormat:@"/tmp/%@", [self.urlStr MD5Hash]];
        NSFileManager *manager = [NSFileManager defaultManager];
        if ([manager fileExistsAtPath:path]) {
            NSData *data = [NSData dataWithContentsOfFile:path];
            self.finishBlock(data);
            return;
        }
    }
    
    NSURL *url = [NSURL URLWithString:self.urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request delegate:self];
}
//實現NSURLConnectionDataDelegate協議
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [_mData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [self release];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    //寫緩存
    if (self.isCache) {
        NSString *path = [NSHomeDirectory() stringByAppendingFormat:@"/tmp/%@", [self.urlStr MD5Hash]];
        [_mData writeToFile:path atomically:YES];
    }
    
    self.finishBlock(_mData);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [self release];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    self.failedBlick();
}

- (void)dealloc {
    [_mData release];
    self.finishBlock = nil;
    self.failedBlick = nil;
    [super dealloc];
}

@end

2.新建一個MyRequestManager類

.h文件

#import 
#import "MyUrlRequest.h"

@interface MyRequestManager : NSObject

+ (void)requestWithUrl:(NSString *)urlString andIsCache:(BOOL)isCache finish:(void(^)(NSData *data))finishBlock failed:(void(^)())failedBlock;

@end

.m文件

#import "MyRequestManager.h"

@implementation MyRequestManager

+ (void)requestWithUrl:(NSString *)urlString andIsCache:(BOOL)isCache finish:(void(^)(NSData *data))finishBlock failed:(void(^)())failedBlock
{
    MyUrlRequest *request = [[[MyUrlRequest alloc] init] autorelease];
    request.urlStr = urlString;
    request.isCache = isCache;
    request.finishBlock = finishBlock;
    request.failedBlick =  failedBlock;
    //在發送startRequest消息之後,該類方法結束,可能會對request發送autorelease消息,這樣的話在request的startRequest方法中就可能會產生錯誤,所以要在startRequest方法中retain一下
    [request startRequest];
}

@end

這樣的話,以後再發送請求就可以直接調用
[MyRequestManager requestWithUrl:(NSString *)urlString andIsCache:(BOOL)isCache finish:(void(^)(NSData *data))finishBlock failed:(void(^)())failedBlock];

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved