From f47b65393268b735e0b8b34e19039382015bc54d Mon Sep 17 00:00:00 2001 From: Robert Long Date: Wed, 4 Aug 2021 17:40:25 -0700 Subject: [PATCH] Use pagehide event on iOS --- src/ConferenceCallManagerHooks.js | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/ConferenceCallManagerHooks.js b/src/ConferenceCallManagerHooks.js index 1584493..4c330c1 100644 --- a/src/ConferenceCallManagerHooks.js +++ b/src/ConferenceCallManagerHooks.js @@ -17,6 +17,22 @@ limitations under the License. import { useCallback, useEffect, useState } from "react"; import { ConferenceCallManager } from "./ConferenceCallManager"; +// https://stackoverflow.com/a/9039885 +function isIOS() { + return ( + [ + "iPad Simulator", + "iPhone Simulator", + "iPod Simulator", + "iPad", + "iPhone", + "iPod", + ].includes(navigator.platform) || + // iPad on iOS 13 detection + (navigator.userAgent.includes("Mac") && "ontouchend" in document) + ); +} + export function useConferenceCallManager(homeserverUrl) { const [{ loading, authenticated, manager, error }, setState] = useState({ loading: true, @@ -128,10 +144,13 @@ export function useVideoRoom(manager, roomId, timeout = 5000) { manager.leaveCall(); } - window.addEventListener("beforeunload", onBeforeUnload); + // iOS doesn't fire beforeunload event, so leave the call when you hide the page. + const unloadEvent = isIOS() ? "pagehide" : "beforeunload"; + + window.addEventListener(unloadEvent, onBeforeUnload); return () => { - window.removeEventListener("beforeunload", onBeforeUnload); + window.removeEventListener(unloadEvent, onBeforeUnload); manager.leaveCall(); }; }, [manager]);