之前寫了Android端訪問遠(yuǎn)程鏈接調(diào)用Cordova插件的實現(xiàn)方案洲敢,后來iOS端也碰到這樣的問題姜钳,歷時半個月總算解決了谢鹊,下面說一下是如何處理的。
前端頁面在頁面加載開始時拓萌,加入下面這段js代碼:
var script = document.createElement('script');
script.type = "text/javascript";
script.src="http://injection/cordova.js";
document.getElementsByTagName('body')[0].appendChild(script);
這段代碼就是給頁面注入了一個script標(biāo)簽岁钓,資源為http://injection/cordova.js。那為什么不在index.html里直接加script標(biāo)簽?zāi)匚⑼酰课易畛跏沁@么做的屡限,但是發(fā)現(xiàn)插件調(diào)用并不能一定生效,有時可以有時不行炕倘,所以就采用頁面開始加載以后再往頁面注入钧大,這樣就一定能調(diào)用插件。
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乓土,繼續(xù)執(zhí)行其他方法,若不是溯警,返回NO帐我,不執(zhí)行其他方法
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以后,會執(zhí)行該方法愧膀,完成替換資源并返回給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
添加這個類以后拦键,需要在CDVAppDelegate
里進(jìn)行注冊,在didFinishLaunchingWithOptions
方法里添加如下代碼:
[NSURLProtocol registerClass:[CDVURLProtocolCustom class]];
至此檩淋,iOS訪問遠(yuǎn)程網(wǎng)站也能調(diào)用Cordova插件了芬为,iOS我不是很懂,有什么不對的希望大家評論指教蟀悦。