之前写了Android端访问远程链接调用Cordova插件的实现方案,后来iOS端也碰到这样的问题,历时半个月总算解决了,下面说一下是如何处理的。
前端页面在页面加载开始时,加入下面这段js代码:
var script = document.createElement('script');
script.type = "text/javascript";
script.src="http://injection/cordova.js";
document.getElementsByTagName('body')[0].appendChild(script);
这段代码就是给页面注入了一个script标签,资源为http://injection/cordova.js。那为什么不在index.html里直接加script标签呢?我最初是这么做的,但是发现插件调用并不能一定生效,有时可以有时不行,所以就采用页面开始加载以后再往页面注入,这样就一定能调用插件。
iOS提供了NSURLProtocol
用来拦截H5页面请求,我们需要自定义一个类继承它,拦截前端script里声明的http://injection/cordova.js,然后替换为本地的cordova.js文件,再返回给页面,具体操作如下:
@interface CDVURLProtocolCustom : NSURLProtocol {}
@end
NSString* const kCDVAssetsLibraryPrefixes = @"http://injection/cordova.js";
@implementation CDVURLProtocolCustom
// 这个方法用来拦截H5页面请求
+ (BOOL)canInitWithRequest:(NSURLRequest*)theRequest
{
NSURL* theUrl = [theRequest URL];
// 判断是否是我们定义的url,若是,返回YES,继续执行其他方法,若不是,返回NO,不执行其他方法
if ([[theUrl absoluteString] hasPrefix:kCDVAssetsLibraryPrefixes]) {
return YES;
}
return NO;
}
+ (NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)request
{
// NSLog(@"%@ received %@", self, NSStringFromSelector(_cmd));
return request;
}
// 获取本地文件路径
- (NSString*)pathForResource:(NSString*)resourcepath
{
NSBundle* mainBundle = [NSBundle mainBundle];
NSMutableArray* directoryParts = [NSMutableArray arrayWithArray:[resourcepath componentsSeparatedByString:@"/"]];
NSString* filename = [directoryParts lastObject];
[directoryParts removeLastObject];
NSString* directoryPartsJoined = [directoryParts componentsJoinedByString:@"/"];
NSString* directoryStr = @"www";
if ([directoryPartsJoined length] > 0) {
directoryStr = [NSString stringWithFormat:@"%@/%@", directoryStr, [directoryParts componentsJoinedByString:@"/"]];
}
return [mainBundle pathForResource:filename ofType:@"" inDirectory:directoryStr];
}
// 在canInitWithRequest方法返回YES以后,会执行该方法,完成替换资源并返回给H5页面
- (void)startLoading
{
// NSLog(@"%@ received %@ - start", self, NSStringFromSelector(_cmd));
NSString* url=super.request.URL.resourceSpecifier;
NSString* cordova = [url stringByReplacingOccurrencesOfString:@"//injection/" withString:@""];
NSURL* startURL = [NSURL URLWithString:cordova];
NSString* cordovaFilePath =[self pathForResource:[startURL path]];
if (!cordovaFilePath) {
[self sendResponseWithResponseCode:401 data:nil mimeType:nil];//重要
return;
}
CFStringRef pathExtension = (__bridge_retained CFStringRef)[cordovaFilePath pathExtension];
CFStringRef type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);
CFRelease(pathExtension);
NSString *mimeType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
if (type != NULL)
CFRelease(type);
// NSURLResponse *response = [[NSURLResponse alloc] initWithURL:super.request.URL MIMEType:mimeType expectedContentLength:-1 textEncodingName:nil];
NSData* data = [NSData dataWithContentsOfFile:cordovaFilePath];
[self sendResponseWithResponseCode:200 data:data mimeType:mimeType];
}
- (void)stopLoading
{
// do any cleanup here
}
+ (BOOL)requestIsCacheEquivalent:(NSURLRequest*)requestA toRequest:(NSURLRequest*)requestB
{
return NO;
}
// 将本地资源返回给H5页面
- (void)sendResponseWithResponseCode:(NSInteger)statusCode data:(NSData*)data mimeType:(NSString*)mimeType
{
if (mimeType == nil) {
mimeType = @"text/plain";
}
NSHTTPURLResponse* response = [[NSHTTPURLResponse alloc] initWithURL:[[self request] URL] statusCode:statusCode HTTPVersion:@"HTTP/1.1" headerFields:@{@"Content-Type" : mimeType}];
[[self client] URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
if (data != nil) {
[[self client] URLProtocol:self didLoadData:data];
}
[[self client] URLProtocolDidFinishLoading:self];
}
@end
添加这个类以后,需要在CDVAppDelegate
里进行注册,在didFinishLaunchingWithOptions
方法里添加如下代码:
[NSURLProtocol registerClass:[CDVURLProtocolCustom class]];
至此,iOS访问远程网站也能调用Cordova插件了,iOS我不是很懂,有什么不对的希望大家评论指教。
网友评论