iPhone SDK: Loading resources from custom URL scheme

喜夏-厌秋 提交于 2019-12-11 15:48:32

问题


I have HTML string which is created using an xml file by a third party library. The HTML string 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?


回答1:


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];



回答2:


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]];


来源:https://stackoverflow.com/questions/1900309/iphone-sdk-loading-resources-from-custom-url-scheme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!