Skip to content

Commit 37b0447

Browse files
authored
Merge pull request #2521 from crazyserver/MOBILE-3534
MOBILE-3534 ionic: Fix check device during loading
2 parents 50b390d + 8844abd commit 37b0447

File tree

4 files changed

+57
-49
lines changed

4 files changed

+57
-49
lines changed

src/core/sharedfiles/sharedfiles.module.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,33 @@ export class CoreSharedFilesModule {
4949
// Register the handler.
5050
delegate.registerHandler(handler);
5151

52-
if (appsProvider.isIOS()) {
53-
let lastCheck = 0;
52+
platform.ready().then(() => {
53+
if (appsProvider.isIOS()) {
54+
let lastCheck = 0;
5455

55-
// Check if there are new files at app start and when the app is resumed.
56-
helper.searchIOSNewSharedFiles();
57-
platform.resume.subscribe(() => {
58-
// Wait a bit to make sure that APP_LAUNCHED_URL is treated before this callback.
59-
setTimeout(() => {
60-
if (Date.now() - lastCheck < 1000) {
61-
// Last check less than 1s ago, don't do anything.
62-
return;
63-
}
56+
// Check if there are new files at app start and when the app is resumed.
57+
helper.searchIOSNewSharedFiles();
58+
platform.resume.subscribe(() => {
59+
// Wait a bit to make sure that APP_LAUNCHED_URL is treated before this callback.
60+
setTimeout(() => {
61+
if (Date.now() - lastCheck < 1000) {
62+
// Last check less than 1s ago, don't do anything.
63+
return;
64+
}
6465

65-
lastCheck = Date.now();
66-
helper.searchIOSNewSharedFiles();
67-
}, 200);
68-
});
66+
lastCheck = Date.now();
67+
helper.searchIOSNewSharedFiles();
68+
}, 200);
69+
});
6970

70-
eventsProvider.on(CoreEventsProvider.APP_LAUNCHED_URL, (url) => {
71-
if (url && url.indexOf('file://') === 0) {
72-
// We received a file in iOS, it's probably a shared file. Treat it.
73-
lastCheck = Date.now();
74-
helper.searchIOSNewSharedFiles(url);
75-
}
76-
});
77-
}
71+
eventsProvider.on(CoreEventsProvider.APP_LAUNCHED_URL, (url) => {
72+
if (url && url.indexOf('file://') === 0) {
73+
// We received a file in iOS, it's probably a shared file. Treat it.
74+
lastCheck = Date.now();
75+
helper.searchIOSNewSharedFiles(url);
76+
}
77+
});
78+
}
79+
});
7880
}
7981
}

src/providers/app.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,8 @@ export class CoreAppProvider {
392392
* @return Whether the app is running in an Android mobile or tablet device.
393393
*/
394394
isAndroid(): boolean {
395-
return this.isMobile() && this.device.platform.toLowerCase() == 'android';
395+
return this.isMobile() &&
396+
((this.device.platform && this.device.platform.toLowerCase() == 'android') || this.platform.is('android'));
396397
}
397398

398399
/**
@@ -412,7 +413,8 @@ export class CoreAppProvider {
412413
* @return Whether the app is running in an iOS mobile or tablet device.
413414
*/
414415
isIOS(): boolean {
415-
return this.isMobile() && this.device.platform.toLowerCase() == 'ios';
416+
return this.isMobile() &&
417+
((this.device.platform && this.device.platform.toLowerCase() == 'ios') || this.platform.is('ios'));
416418
}
417419

418420
/**

src/providers/file.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,31 @@ export class CoreFileProvider {
8181

8282
this.logger = logger.getInstance('CoreFileProvider');
8383

84-
if (appProvider.isAndroid() && !Object.getOwnPropertyDescriptor(FileReader.prototype, 'onloadend')) {
85-
// Cordova File plugin creates some getters and setter for FileReader, but Ionic's polyfills override them in Android.
86-
// Create the getters and setters again. This code comes from FileReader.js in cordova-plugin-file.
87-
this.defineGetterSetter(FileReader.prototype, 'readyState', function(): any {
88-
return this._localURL ? this._readyState : this._realReader.readyState;
89-
});
84+
platform.ready().then(() => {
85+
if (appProvider.isAndroid() && !Object.getOwnPropertyDescriptor(FileReader.prototype, 'onloadend')) {
86+
// Cordova File plugin creates some getters and setter for FileReader, but
87+
// Ionic's polyfills override them in Android.
88+
// Create the getters and setters again. This code comes from FileReader.js in cordova-plugin-file.
89+
this.defineGetterSetter(FileReader.prototype, 'readyState', function(): any {
90+
return this._localURL ? this._readyState : this._realReader.readyState;
91+
});
9092

91-
this.defineGetterSetter(FileReader.prototype, 'error', function(): any {
92-
return this._localURL ? this._error : this._realReader.error;
93-
});
93+
this.defineGetterSetter(FileReader.prototype, 'error', function(): any {
94+
return this._localURL ? this._error : this._realReader.error;
95+
});
9496

95-
this.defineGetterSetter(FileReader.prototype, 'result', function(): any {
96-
return this._localURL ? this._result : this._realReader.result;
97-
});
97+
this.defineGetterSetter(FileReader.prototype, 'result', function(): any {
98+
return this._localURL ? this._result : this._realReader.result;
99+
});
98100

99-
this.defineEvent('onloadstart');
100-
this.defineEvent('onprogress');
101-
this.defineEvent('onload');
102-
this.defineEvent('onerror');
103-
this.defineEvent('onloadend');
104-
this.defineEvent('onabort');
105-
}
101+
this.defineEvent('onloadstart');
102+
this.defineEvent('onprogress');
103+
this.defineEvent('onload');
104+
this.defineEvent('onerror');
105+
this.defineEvent('onloadend');
106+
this.defineEvent('onabort');
107+
}
108+
});
106109
}
107110

108111
/**

src/providers/utils/iframe.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { Injectable, NgZone } from '@angular/core';
1616
import { Config, Platform, NavController } from 'ionic-angular';
1717
import { TranslateService } from '@ngx-translate/core';
1818
import { Network } from '@ionic-native/network';
19-
import { CoreApp } from '../app';
19+
import { CoreApp, CoreAppProvider } from '../app';
2020
import { CoreFileProvider } from '../file';
2121
import { CoreLoggerProvider } from '../logger';
2222
import { CoreSitesProvider } from '../sites';
@@ -47,6 +47,7 @@ export class CoreIframeUtilsProvider {
4747
private utils: CoreUtilsProvider,
4848
private domUtils: CoreDomUtilsProvider,
4949
platform: Platform,
50+
appProvider: CoreAppProvider,
5051
private translate: TranslateService,
5152
private network: Network, private zone: NgZone,
5253
private config: Config,
@@ -56,8 +57,8 @@ export class CoreIframeUtilsProvider {
5657

5758
const win = <WKUserScriptWindow> window;
5859

59-
if (CoreApp.instance.isIOS() && win.WKUserScript) {
60-
platform.ready().then(() => {
60+
platform.ready().then(() => {
61+
if (appProvider.isIOS() && win.WKUserScript) {
6162
// Inject code to the iframes because we cannot access the online ones.
6263
const wwwPath = fileProvider.getWWWAbsolutePath();
6364
const linksPath = textUtils.concatenatePaths(wwwPath, 'assets/js/iframe-treat-links.js');
@@ -72,8 +73,8 @@ export class CoreIframeUtilsProvider {
7273

7374
// Handle post messages received by iframes.
7475
window.addEventListener('message', this.handleIframeMessage.bind(this));
75-
});
76-
}
76+
}
77+
});
7778
}
7879

7980
/**

0 commit comments

Comments
 (0)