정적 인 압축 버전의 웹 사이트를 webView의 대상으로 사용할 수있는 사용자 지정 NSURLProtocol을 구현했습니다. 이동 중에도 zip을 열고 필요한 데이터를로드합니다. 하지만 문제는 NSURLProtocol이 상대 경로와 제대로 작동하지 않는 것 같습니다.NSURLProtocol 및 상대 경로
assets/css/main.css
assets/css/style.css
assets/images/sprite.png
index.html
그리고 사용하여 CSS에서 sprite.png 전화 : 그건 내가 다음과 같은 구조를 가지고있는 누락, //host/images/sprite.png : background: url(../images/sprite.png) no-repeat;
하지만, 내 사용자 지정 NSURLProtocol의 requestURL는 방식을 보여줍니다 자산 부분. ..
부분을 assets
으로 바꾸면 제대로 작동하지만 차라리이 작업을 수행하지 않아도됩니다.
동일한 문제가 여기에서 발견되었습니다 : Loading resources from relative paths through NSURLProtocol subclass하지만 답변이 없습니다.
요청이 적절하게 상대 경로를 해결하거나 나중에 경로를 수정하도록이 문제를 해결할 수있는 방법을 찾을 수 없습니다 (하지만 요청의 출처를 알아야하며 운이 없었습니다. 어느 쪽이든)
감사합니다. 사전에 감사드립니다.
사이드 참고 : main.css가에 @import url("style.css");
를 사용 같은 문제
편집 : 내가 원격 서버에서 zip 파일을 다운로드하여 시작
:에서
NSURL * fetchURL = [NSURL URLWithString:zipURLString];
[…]
NSString * filePath = [[self documentsDirectory] stringByAppendingPathComponent:fetchURL.path.lastPathComponent];
[zipData writeToFile:filePath atomically:YES];
그래서 http://host/foo/archive.zip
, 나는 그것을 documentsDirectory/archive.zip
에 저장합니다. 거기에서 , 나는 zip 파일을에 가리 키도록 방식과 URL을 변경 :
NSString * str = [NSString stringWithFormat:@"myzip://%@", zipURL.path.lastPathComponent];
[_webView loadRequest:[NSURLRequest str]];
: //archive.zip, 그리고 이러한 파일이 zip 파일에서 발견되지 않은 경우, 내가 추가/index.html을 현재 경로로 변경하십시오. 따라서 다음과 같은 요청은 내 NSURLProtocol
서브 클래스 - (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id <NSURLProtocolClient>)client
에 도착 :
myzip://archive.zip (Changed to myzip://archive.zip/index.html)
myzip://archive.zip/assets/css/main.css
myzip://archive.zip/styles.css (Problem here)
이 질문에 대한 답변을 https : // stackoverflow .com/questions/44584159/nsurlprotocol-subclass-changes-urls-automatically – Mrug