ionic-team / ionic-team/cordova-plugin-ionic-webview
iOS platform doesn't reload in 2 kinds of situation
- Dominant language
- Objective-C
- Stars
- 493
- Forks
- 410
- PR merge metrics
- No merged PRs in 30d
Description
Hi There,
I find there are 2 kinds of situation which should reload page in iOS, but plugin doesn't do it correctly.
**Ionic info:**
> @ionic/cli-utils : 1.19.2
> ionic (Ionic CLI) : 3.20.0
>
> global packages:
>
> cordova (Cordova CLI) : 7.1.0
>
> local packages:
>
> @ionic/app-scripts : 3.1.8
> Cordova Platforms : android 6.3.0 ios 4.5.3
> Ionic Framework : ionic-angular 3.9.2
>
> System:
>
> Android SDK Tools : 26.1.1
> ios-deploy : 1.9.4
> ios-sim : 6.1.2
> Node : v8.9.1
> npm : 5.5.1
> OS : macOS
> Xcode : Xcode 10.2 Build version 10E125
>
> Plugin Version:
>
> cordova-plugin-ionic-webview@2.4.1
**Steps to reproduce**
1.Setup ionic app
2.Set WKBackgroundMode to false
3.Run app on an iPhone 6 from XCode
4.Quickly press home key to make app inactive
**Root Cause**
1.Index HTML file doesn't load
```
2019-05-05 14:05:20.261866+0800 XXX[468:34732] Failed to load webpage with error: Could not connect to the server.
2019-05-05 14:05:45.299923+0800 XXX[468:34732] CDVWKWebViewEngine shouldReloadWebView::
2019-05-05 14:05:45.300099+0800 XXX[468:34732] CDVWKWebViewEngine shouldReloadWebView title:
2019-05-05 14:05:45.300125+0800 XXX[468:34732] CDVWKWebViewEngine shouldReloadWebView location: (null)
2019-05-05 14:05:45.300148+0800 XXX[468:34732] CDVWKWebViewEngine shouldReloadWebView reload: 0
```
because the URL becomes null when error occurs, so even manually reload the webview, it also displays an empty page.
2.Index HTML file loads, but cordova js or some other js file doesn't.
```
2019-05-05 14:39:10.298316+0800 XXX[531:41312] CDVWKWebViewEngine shouldReloadWebView::
2019-05-05 14:39:10.298437+0800 XXX[531:41312] CDVWKWebViewEngine shouldReloadWebView title: Ionic App
2019-05-05 14:39:10.298468+0800 XXX[531:41312] CDVWKWebViewEngine shouldReloadWebView location: http://localhost:8080/var/containers/Bundle/Application/BAD5ED64-9BE6-4C69-9FA8-4DBDB466736E/XXX.app/www/index.html
2019-05-05 14:39:10.298492+0800 XXX[531:41312] CDVWKWebViewEngine shouldReloadWebView reload: 0
```
It seems every thing goes ok, but you can't do anything with the cordova plugins.
For someone can do iOS native coding, below is my hacking solution:
In somewhere can access the CDVViewController instance, add
```
#import
@interface CDVViewController ()
- (NSURL *)appUrl;
@end
//Insert this method to somewhere you wan't to trigger reloading, I copy some code from CDVViewController.m viewDidLoad method, which won't damage the cordova framework files.
+ (void)reload {
if (![(WKWebView*)cdvInstance.webView isLoading]) {
NSURL* appURL = [cdvInstance appUrl];
[CDVUserAgentUtil acquireLock:^(NSInteger lockToken) {
if (appURL) {
NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
[cdvInstance.webViewEngine loadRequest:appReq];
}
}];
} else {
NSLog(@"WKWebView is reloading, please wait for a few seconds.");
}
}
```
Contributor guide
Assessment
This issue has not been assessed yet.