开发者

iPhone SDK: Loading resources from custom URL scheme

开发者 https://www.devze.com 2022-12-14 10:47 出处:网络
I have HTML string which is created using an xml file by a third party library. The HTML st开发者_如何学Pythonring contains custom URLs for images and videos (Ex:image://). Is there is way by I can ha

I have HTML string which is created using an xml file by a third party library. The HTML st开发者_如何学Pythonring contains custom URLs for images and videos (Ex:image://). Is there is way by I can handle these resource load request and load them correctly in UIWebView?


You should be able to change them to a file:// url that points to a file inside the application bundle.

To get the path you can use:

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyFileInResources" ofType:@".png"];

Note that you will need to escape this using:

NSString *escapedPath = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


We need to create a subclass of NSURLProtocol and re-implement the following methods that can handle the custom URL scheme

+(BOOL)canInitWithRequest:(NSURLRequest*)request
+(NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)request
+(BOOL)requestIsCacheEquivalent:(NSURLRequest*)a toRequest:(NSURLRequest*)b
-(void)startLoading
-(void)stopLoading

We also have to register this custom URL protocol class when the app launches in the following way

[NSURLProtocol registerClass:[CustomURLProtocol class]];
0

精彩评论

暂无评论...
验证码 换一张
取 消