攔截請求

//

//  TestUrlProtocal.m

//  demo1027

//

//  Created by qitmac000370 on 2019/10/28.

//  Copyright © 2019 Facebook. All rights reserved.

//

 

#import "TestUrlProtocal.h"

#import <UIKit/UIKit.h>

 

@implementation TestUrlProtocal

 

+ (void)load {

    [NSURLProtocol registerClass:[self class]];

}

 

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {

    return YES;

}

 

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {

    return request;

}

 

- (void)startLoading {

 

    UIImage *image = [UIImage imageNamed:@"11"];

    NSData * imageData = UIImageJPEGRepresentation( image , 0.9);

 

    NSURLResponse *response = [[NSURLResponse alloc] initWithURL:self.request.URL MIMEType:@"image/jpeg" expectedContentLength:imageData.length textEncodingName:@"UTF-8"];

    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];

    [self.client URLProtocol:self didLoadData:imageData];

    [self.client URLProtocolDidFinishLoading:self];

}

 

- (void)stopLoading {

    

}

 

@end

 

 

http://3code.info/blog/2015/10/NSURLProtocol.html

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