diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 6897feeac..50afe4835 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -19,16 +19,14 @@ import { BackButtonEvent, ScrollDetail } from '@ionic/core'; import { CoreLang } from '@services/lang'; import { CoreLoginHelper } from '@features/login/services/login-helper'; import { CoreEvents } from '@singletons/events'; -import { NgZone, SplashScreen, Translate } from '@singletons'; +import { NgZone, SplashScreen } from '@singletons'; import { CoreNetwork } from '@services/network'; import { CoreApp } from '@services/app'; import { CoreSites } from '@services/sites'; import { CoreNavigator } from '@services/navigator'; import { CoreSubscriptions } from '@singletons/subscriptions'; import { CoreWindow } from '@singletons/window'; -import { CoreCustomURLSchemes } from '@services/urlschemes'; import { CoreUtils } from '@services/utils/utils'; -import { CoreUrlUtils } from '@services/utils/url'; import { CoreConstants } from '@/core/constants'; import { CoreSitePlugins } from '@features/siteplugins/services/siteplugins'; import { CoreDomUtils } from '@services/utils/dom'; @@ -46,8 +44,6 @@ export class AppComponent implements OnInit, AfterViewInit { @ViewChild(IonRouterOutlet) outlet?: IonRouterOutlet; - protected lastInAppUrl?: string; - /** * Component being initialized. */ @@ -91,60 +87,6 @@ export class AppComponent implements OnInit, AfterViewInit { content.classList.toggle('core-footer-shadow', !CoreDom.scrollIsBottom(scrollElement)); }); - // Check URLs loaded in any InAppBrowser. - CoreEvents.on(CoreEvents.IAB_LOAD_START, (event) => { - // URLs with a custom scheme can be prefixed with "http://" or "https://", we need to remove this. - const protocol = CoreUrlUtils.getUrlProtocol(event.url); - const url = event.url.replace(/^https?:\/\//, ''); - const urlScheme = CoreUrlUtils.getUrlProtocol(url); - const isExternalApp = urlScheme && urlScheme !== 'file' && urlScheme !== 'cdvfile'; - - if (CoreCustomURLSchemes.isCustomURL(url)) { - // Close the browser if it's a valid SSO URL. - CoreCustomURLSchemes.handleCustomURL(url).catch((error) => { - CoreCustomURLSchemes.treatHandleCustomURLError(error); - }); - CoreUtils.closeInAppBrowser(); - - } else if (isExternalApp && url.includes('://token=')) { - // It's an SSO token for another app. Close the IAB and show an error. - CoreUtils.closeInAppBrowser(); - CoreDomUtils.showErrorModal(Translate.instant('core.login.contactyouradministratorissue', { - $a: '

' + Translate.instant('core.errorurlschemeinvalidscheme', { - $a: urlScheme, - }), - })); - - } else if (CoreApp.isAndroid()) { - // Check if the URL has a custom URL scheme. In Android they need to be opened manually. - if (isExternalApp) { - // Open in browser should launch the right app if found and do nothing if not found. - CoreUtils.openInBrowser(url, { showBrowserWarning: false }); - - // At this point the InAppBrowser is showing a "Webpage not available" error message. - // Try to navigate to last loaded URL so this error message isn't found. - if (this.lastInAppUrl) { - CoreUtils.openInApp(this.lastInAppUrl); - } else { - // No last URL loaded, close the InAppBrowser. - CoreUtils.closeInAppBrowser(); - } - } else { - this.lastInAppUrl = protocol ? `${protocol}://${url}` : url; - } - } - }); - - // Check InAppBrowser closed. - CoreEvents.on(CoreEvents.IAB_EXIT, () => { - this.lastInAppUrl = ''; - - if (CoreLoginHelper.isWaitingForBrowser()) { - CoreLoginHelper.stopWaitingForBrowser(); - CoreLoginHelper.checkLogout(); - } - }); - CorePlatform.resume.subscribe(() => { // Wait a second before setting it to false since in iOS there could be some frozen WS calls. setTimeout(() => { diff --git a/src/core/initializers/prepare-inapp-browser.ts b/src/core/initializers/prepare-inapp-browser.ts new file mode 100644 index 000000000..6c8593883 --- /dev/null +++ b/src/core/initializers/prepare-inapp-browser.ts @@ -0,0 +1,90 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { CoreLoginHelper } from '@features/login/services/login-helper'; +import { CoreApp } from '@services/app'; +import { CoreCustomURLSchemes } from '@services/urlschemes'; +import { CoreDomUtils } from '@services/utils/dom'; +import { CoreUrlUtils } from '@services/utils/url'; +import { CoreUtils } from '@services/utils/utils'; +import { Translate } from '@singletons'; +import { CoreEvents } from '@singletons/events'; + +let lastInAppUrl: string | null = null; + +export default function(): void { + // Check URLs loaded in any InAppBrowser. + CoreEvents.on(CoreEvents.IAB_LOAD_START, async (event) => { + // URLs with a custom scheme can be prefixed with "http://" or "https://", we need to remove this. + const protocol = CoreUrlUtils.getUrlProtocol(event.url); + const url = event.url.replace(/^https?:\/\//, ''); + const urlScheme = CoreUrlUtils.getUrlProtocol(url); + const isExternalApp = urlScheme && urlScheme !== 'file' && urlScheme !== 'cdvfile'; + + if (CoreCustomURLSchemes.isCustomURL(url)) { + // Close the browser if it's a valid SSO URL. + CoreCustomURLSchemes.handleCustomURL(url).catch((error) => { + CoreCustomURLSchemes.treatHandleCustomURLError(error); + }); + CoreUtils.closeInAppBrowser(); + + return; + } + + if (isExternalApp && url.includes('://token=')) { + // It's an SSO token for another app. Close the IAB and show an error. + CoreUtils.closeInAppBrowser(); + CoreDomUtils.showErrorModal(Translate.instant('core.login.contactyouradministratorissue', { + $a: '

' + Translate.instant('core.errorurlschemeinvalidscheme', { + $a: urlScheme, + }), + })); + + return; + } + + if (!CoreApp.isAndroid()) { + return; + } + + // Check if the URL has a custom URL scheme. In Android they need to be opened manually. + if (!isExternalApp) { + lastInAppUrl = protocol ? `${protocol}://${url}` : url; + + return; + } + + // Open in browser should launch the right app if found and do nothing if not found. + CoreUtils.openInBrowser(url, { showBrowserWarning: false }); + + // At this point the InAppBrowser is showing a "Webpage not available" error message. + // Try to navigate to last loaded URL so this error message isn't found. + if (lastInAppUrl) { + CoreUtils.openInApp(lastInAppUrl); + } else { + // No last URL loaded, close the InAppBrowser. + CoreUtils.closeInAppBrowser(); + } + }); + + // Check InAppBrowser closed. + CoreEvents.on(CoreEvents.IAB_EXIT, () => { + lastInAppUrl = null; + + if (CoreLoginHelper.isWaitingForBrowser()) { + CoreLoginHelper.stopWaitingForBrowser(); + CoreLoginHelper.checkLogout(); + } + }); +}