接上篇本地調用本地cordova。
要想實現遠程的調用,首先想到的是網頁發送請求訪問本地cordova,然后本地攔截請求后把所需資源替換為本地,實現Cordova的遠程調用。
1、遠程網頁注入請求cordova插件的代碼。
var script = document.createElement('script');
script.type = "text/javascript";
script.src="http://injection/cordova.js";
document.getElementsByTagName('body')[0].appendChild(script);
主義的是一定要在當前網頁加載完成后注入,放置在body的最后就好。http://injection/cordova.js 為前端做攔截而準備。
2、重載CDVURLProtocol.h,這個類是用來攔截上邊的http://injection/cordova.js,新建文件CDVURLProtocolCustom.h、CDVURLProtocolCustom.m,注意要把這兩個文件放置在本地插件的ios類文件夾下,然后在cordova工程public文件夾下導入進來,不然會發生與CDVURLProtocol.h沖突的情況。
CDVURLProtocolCustom.m文件內容如下:
#import "CDVURLProtocolCustom.h"
#import <MobileCoreServices/MobileCoreServices.h>
@interface CDVURLProtocolCustom ()
@end
NSString* const kCDVAssetsLibraryPrefixes = @"http://injection";
@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
{
//
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:@"http://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
3、CDVAppdelegate.m下 - (BOOL)application:(UIApplication)application didFinishLaunchingWithOptions:(NSDictionary)launchOptions添加調用CDVURLProtocolCustom的方法。
[NSURLProtocol registerClass:[CDVURLProtocolCustom class]];
4、可能會發生不允許cordova調用外部鏈接的情況。所以config.xml中添加
<allow-navigation href="http://*/*" />
<allow-navigation href="https://*/*" />
編譯運行。完美解決cordova下 themeableBrowser遠程界面調用本地cordova的問題,其他如inappBrowser也可以采用這種方式處理。當然缺點也存在,就是可能網頁完成加載會慢一些,所以能在本地處理的就別遠程了。