84 lines
3.3 KiB
Objective-C
84 lines
3.3 KiB
Objective-C
//
|
||
// GDWebAssetStorageService.m
|
||
// DESThreeTest
|
||
//
|
||
// Created by Qwerdf on 2025/10/24.
|
||
// Copyright © 2025 zhang alan. All rights reserved.
|
||
//
|
||
|
||
#import "GDWebAssetStorageService.h"
|
||
|
||
@implementation GDWebAssetStorageService
|
||
|
||
#pragma mark - 初始化
|
||
- (instancetype)initWithSchemeTask:(id<WKURLSchemeTask>)task {
|
||
if (self = [super init]) {
|
||
_schemeTask = task;
|
||
}
|
||
return self;
|
||
}
|
||
|
||
#pragma mark - GDWebCustomURLSchemeProtocol
|
||
+ (BOOL)canService:(id<WKURLSchemeTask>)task {
|
||
// 只处理 "asset://" 协议的请求
|
||
NSString *scheme = task.request.URL.scheme.lowercaseString;
|
||
return [scheme isEqualToString:@"asset"];
|
||
}
|
||
|
||
- (void)startLoading:(void (^)(void))completion {
|
||
self.completion = completion;
|
||
NSURLRequest *request = self.schemeTask.request;
|
||
NSURL *url = request.URL;
|
||
|
||
// 1. 解析本地资源路径(如 asset://images/logo.png -> 映射到沙盒中的 images/logo.png)
|
||
NSString *path = [url.path stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"/"]];
|
||
NSString *localPath = [[NSBundle mainBundle] pathForResource:path ofType:nil];
|
||
if (!localPath) {
|
||
// 若主包中没有,检查 Documents 目录
|
||
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject stringByAppendingPathComponent:path];
|
||
localPath = docPath;
|
||
}
|
||
|
||
// 2. 读取本地文件并返回给网页
|
||
if ([[NSFileManager defaultManager] fileExistsAtPath:localPath]) {
|
||
NSData *data = [NSData dataWithContentsOfFile:localPath];
|
||
NSString *mimeType = [self mimeTypeForPath:localPath];
|
||
NSURLResponse *response = [[NSURLResponse alloc] initWithURL:url
|
||
MIMEType:mimeType
|
||
expectedContentLength:data.length
|
||
textEncodingName:@"UTF-8"];
|
||
// 发送响应和数据给网页
|
||
[self.schemeTask didReceiveResponse:response];
|
||
[self.schemeTask didReceiveData:data];
|
||
[self.schemeTask didFinish];
|
||
} else {
|
||
// 资源不存在,返回 404 错误
|
||
NSError *error = [NSError errorWithDomain:NSURLErrorDomain
|
||
code:NSURLErrorFileDoesNotExist
|
||
userInfo:@{NSLocalizedDescriptionKey: @"本地资源不存在"}];
|
||
[self.schemeTask didFailWithError:error];
|
||
}
|
||
|
||
// 调用完成回调(从任务列表中移除)
|
||
if (completion) completion();
|
||
}
|
||
|
||
- (void)stopLoading {
|
||
// 停止加载时清理资源(如取消文件读取)
|
||
self.schemeTask = nil;
|
||
if (self.completion) self.completion();
|
||
}
|
||
|
||
#pragma mark - 辅助方法:根据文件路径获取 MIME 类型
|
||
- (NSString *)mimeTypeForPath:(NSString *)path {
|
||
NSString *extension = [path pathExtension].lowercaseString;
|
||
if ([extension isEqualToString:@"html"]) return @"text/html";
|
||
if ([extension isEqualToString:@"js"]) return @"application/javascript";
|
||
if ([extension isEqualToString:@"css"]) return @"text/css";
|
||
if ([extension isEqualToString:@"png"]) return @"image/png";
|
||
if ([extension isEqualToString:@"jpg"] || [extension isEqualToString:@"jpeg"]) return @"image/jpeg";
|
||
return @"application/octet-stream";
|
||
}
|
||
|
||
@end
|