IOS themeableBrowser 调用Cordova原生

2020-08-19  本文已影响0人  爱写诗的程序员zxp

接上篇本地调用本地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:@"//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也可以采用这种方式处理。当然缺点也存在,就是可能网页完成加载会慢一些,所以能在本地处理的就别远程了。

上一篇下一篇

猜你喜欢

热点阅读