Skip to content

Commit b17e182

Browse files
committed
feat(): handle ionic WKWebView better
1 parent db908c4 commit b17e182

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/providers/image-loader.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ export class ImageLoader {
6666
return this.platform.is('ios') && (<any>window).webkit;
6767
}
6868

69+
private get isIonicWKWebView(): boolean {
70+
return this.isWKWebView && location.host === 'localhost:8080';
71+
}
72+
6973
constructor(
7074
private config: ImageLoaderConfig,
7175
private file: File,
@@ -115,7 +119,7 @@ export class ImageLoader {
115119

116120
this.file.removeRecursively(this.file.cacheDirectory, this.config.cacheDirectoryName)
117121
.then(() => {
118-
if (this.isWKWebView) {
122+
if (this.isWKWebView && !this.isIonicWKWebView) {
119123

120124
// also clear the temp files
121125
this.file.removeRecursively(this.file.tempDirectory, this.config.cacheDirectoryName)
@@ -390,7 +394,7 @@ export class ImageLoader {
390394
return this.file
391395
.removeFile(this.file.cacheDirectory + this.config.cacheDirectoryName, file)
392396
.then(() => {
393-
if (this.isWKWebView) {
397+
if (this.isWKWebView && !this.isIonicWKWebView) {
394398
return this.file
395399
.removeFile(this.file.tempDirectory + this.config.cacheDirectoryName, file)
396400
.catch(() => {
@@ -442,7 +446,12 @@ export class ImageLoader {
442446
// now check if iOS device & using WKWebView Engine.
443447
// in this case only the tempDirectory is accessible,
444448
// therefore the file needs to be copied into that directory first!
445-
if (this.isWKWebView) {
449+
if (this.isIonicWKWebView) {
450+
451+
// Ionic WKWebView can access all files, but we just need to replace file:/// with http://localhost:8080/
452+
resolve(fileEntry.nativeURL.replace('file:///', 'http://localhost:8080/'));
453+
454+
} else if (this.isWKWebView) {
446455

447456
// check if file already exists in temp directory
448457
this.file.resolveLocalFilesystemUrl(tempDirPath + '/' + fileName)
@@ -469,6 +478,7 @@ export class ImageLoader {
469478
resolve(fileEntry.nativeURL);
470479

471480
}
481+
472482
}
473483
})
474484
.catch(reject); // file doesn't exist
@@ -527,7 +537,7 @@ export class ImageLoader {
527537
.catch(() => this.file.createDir(this.file.cacheDirectory, this.config.cacheDirectoryName, false));
528538
}
529539

530-
if (this.isWKWebView) {
540+
if (this.isWKWebView && !this.isIonicWKWebView) {
531541
if (replace) {
532542
// create or replace the temp directory
533543
tempDirectoryPromise = this.file.createDir(this.file.tempDirectory, this.config.cacheDirectoryName, replace);

0 commit comments

Comments
 (0)