iOS: remoteResource support headers setting

This commit is contained in:
pengfei.zhou 2023-09-22 14:24:51 +08:00 committed by jingpeng
parent 2c52403d5f
commit fd1e626bf2
3 changed files with 35 additions and 5 deletions

View File

@ -21,4 +21,5 @@
#import "DoricResource.h" #import "DoricResource.h"
@interface DoricRemoteResource : DoricResource @interface DoricRemoteResource : DoricResource
- (void)setHeaderWithKey:(NSString *)key withValue:(NSString *)value;
@end @end

View File

@ -19,15 +19,35 @@
#import "DoricRemoteResource.h" #import "DoricRemoteResource.h"
@interface DoricRemoteResource ()
@property(nonatomic, strong) NSMutableDictionary *headers;
@end
@implementation DoricRemoteResource @implementation DoricRemoteResource
- (void)setHeaderWithKey:(NSString *)key withValue:(NSString *)value {
if (!self.headers) {
self.headers = [NSMutableDictionary new];
}
self.headers[key] = value;
}
- (DoricAsyncResult <NSData *> *)fetchRaw { - (DoricAsyncResult <NSData *> *)fetchRaw {
DoricAsyncResult *result = [DoricAsyncResult new]; DoricAsyncResult *result = [DoricAsyncResult new];
NSURL *url = [NSURL URLWithString:self.identifier]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.identifier]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ if (self.headers) {
NSData *urlData = [NSData dataWithContentsOfURL:url]; [self.headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
[result setupResult:urlData]; [request setValue:obj forHTTPHeaderField:key];
}); }];
}
[[[NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]
dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
[result setupResult:data];
} else {
[result setupError:(id) error.description];
}
}] resume];
return result; return result;
} }
@end @end

View File

@ -20,6 +20,7 @@
#import "DoricResourceManager.h" #import "DoricResourceManager.h"
#import "DoricContext.h" #import "DoricContext.h"
#import "DoricArrayBufferResource.h" #import "DoricArrayBufferResource.h"
#import "DoricRemoteResource.h"
@interface DoricResourceManager () @interface DoricResourceManager ()
@property(nonatomic, strong) NSMutableDictionary <NSString *, id <DoricResourceLoader>> *loaders; @property(nonatomic, strong) NSMutableDictionary <NSString *, id <DoricResourceLoader>> *loaders;
@ -60,6 +61,14 @@ - (__kindof DoricResource *)load:(NSDictionary *)resource
doricResource = [loader load:identifier withContext:context]; doricResource = [loader load:identifier withContext:context];
if ([doricResource isKindOfClass:DoricArrayBufferResource.class]) { if ([doricResource isKindOfClass:DoricArrayBufferResource.class]) {
((DoricArrayBufferResource *) doricResource).data = resource[@"data"]; ((DoricArrayBufferResource *) doricResource).data = resource[@"data"];
} else if ([doricResource isKindOfClass:DoricRemoteResource.class]) {
NSDictionary *headers = resource[@"headers"];
if (headers) {
[headers enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *value, BOOL *stop) {
[(DoricRemoteResource *) doricResource setHeaderWithKey:key
withValue:value];
}];
}
} }
[context.cachedResources setObject:doricResource forKey:resId]; [context.cachedResources setObject:doricResource forKey:resId];
} }