从WKWebview的cordova-ios中的数据目录加载html文件

6bc51xsx  于 12个月前  发布在  iOS
关注(0)|答案(3)|浏览(116)

我正在开发Cordova-iOS应用程序。我的应用程序将从服务器下载zip文件(包含html文件ex:-second_login.html),然后我们将其解压缩到数据目录中。成功解压缩后,应用程序应导航到second_login. html。应用程序在带有cordova-ios 5.1.1的UIWebview中运行良好。但它不工作在cordova-ios-6.1.0与WKWebview。
使用cordova-ios-6.1.0,我下载并解压数据目录成功地到下面的路径。

/var/mobile/Containers/Data/Application/<APPID>/Library/NoCloud/<APP_NAME>//files/www-24862240/second_login.html.

为了加载second_login.html,我使用document.location = "<PATH_MENTIONED_ABOVE>"
最初,我们在下载zip文件时遇到了问题(Cookie同步问题),为此我们使用了cordova-plugin-ios-xhr插件。然后我们成功下载了zip文件。

vx6bjr1n

vx6bjr1n1#

经过两个月的努力,我们找到了解决问题的办法。原生Webkit webview在Swift中运行良好。所以我们发现cordova-ios做了一些限制。所以我们在cordovaLib xcode项目中添加了一些方法。
1.找到cordovalib xcode项目中的WebviewEngine文件夹。(cordovaLib.xcodeproj -> private -> plugins -> CDVWebviewEngine)
1.在CDVWebViewEngine. h文件中的allowsBackForwardNavigationGestures函数后添加新函数
- (void)loadFileURL:(CDVInvokedUrlCommand*)command;
3)在CDVWebViewEngine.m文件中的defaultResourcePolicyForURL函数后添加以下函数

-(void)loadFileURL:(CDVInvokedUrlCommand*)command;
{

NSString* fileurlStr = [command argumentAtIndex:0];
NSString* folderurlStr = [command argumentAtIndex:1];
NSURL *nsURLfile = [NSURL fileURLWithPath:fileurlStr];
NSURL *nsURLfileroot = [NSURL fileURLWithPath:folderurlStr];

NSFileManager* fileManager = [NSFileManager defaultManager];
NSArray* contentOfDirectory = [fileManager contentsOfDirectoryAtPath:folderurlStr error:nil];
int contentCount = [contentOfDirectory count];

int i;
for(i=0;i<contentCount;i++){
NSString* fileName = [contentOfDirectory objectAtIndex:i];
NSString* path = [folderurlStr stringByAppendingFormat:@"%@%@",@"/",fileName];
NSLog(path);
}

if ([fileManager fileExistsAtPath:[nsURLfile path]]){
[(WKWebView*)_engineWebView loadFileURL:nsURLfile allowingReadAccessToURL:nsURLfileroot];
}else{
NSLog(@"file does not exist");
}
}

1.然后在我们的应用程序JavaScript文件中,使用下面的代码导航到下载的捆绑文件。在这里路径是zip下载和提取的数据目录位置与自定义文件夹名称(在我们的情况下)。
public void onDestroy(){

var path = cordova.file.dataDirectory + APP_NAME_FOLDER + "/" + localStorage.zipExtractLocation +"/";

            var wkPath = path.replace("file://", "");

         cordova.exec( null, null, 'CDVWebViewEngine', 'loadFileURL', [wkPath+"index.html", wkPath] );

         }

1.添加以下首选项到xml.xml中

<preference name="deployment-target" value="12" />
<preference name="allowFileAccessFromFileURLs" value="true" />
bttbmeg0

bttbmeg02#

我遵循了这些步骤。重定向成功。
但我得到了“XMLHttpRequest”错误。然后我安装了“https://github.com/globules-io/cordova-plugin-ios-xhr“插件。
我将以下设置添加到sample.xml中:

<preference name="NativeXHRLogging" value="full" />
<preference name="AllowUntrustedCerts" value="true" />
<preference name="InterceptRemoteRequests" value="all" />
<preference name="allowFileAccessFromFileURLs" value="true" />
<preference name="allowUniversalAccessFromFileURLs" value="true" />

一切都恢复正常了。
uyummobile/cordova-ios

ippsafx7

ippsafx73#

cordova plugin add @globules-io/cordova-plugin-ios-xhr不需要添加任何额外的东西就可以解决这个问题。这是一个众所周知的问题已经很长一段时间了(https://github.com/apache/cordova-ios/issues/883),希望这些更改将合并到cordova-ios并最终解决这个问题(https://github.com/apache/cordova-plugin-inappbrowser/pull/778)。

相关问题