diff --git a/admin/class-twp-admin.php b/admin/class-twp-admin.php index 8643b44..7c278ba 100644 --- a/admin/class-twp-admin.php +++ b/admin/class-twp-admin.php @@ -8253,20 +8253,40 @@ class TWP_Admin { // Check if SDK loaded and initialize debugLog('jQuery ready'); + + // Don't wait for window.load - it may not fire on mobile + // Instead, poll for Twilio SDK availability + var sdkCheckAttempts = 0; + var maxSdkCheckAttempts = 50; // 5 seconds max + + function checkAndInitialize() { + sdkCheckAttempts++; + debugLog('SDK check #' + sdkCheckAttempts + ': ' + (typeof Twilio)); + + if (typeof Twilio !== 'undefined' && Twilio.Device) { + console.log('Twilio SDK loaded successfully'); + debugLog('SDK OK, initializing...'); + initializeBrowserPhone(); + } else if (sdkCheckAttempts < maxSdkCheckAttempts) { + // Keep checking every 100ms + setTimeout(checkAndInitialize, 100); + } else { + showError('Twilio Voice SDK failed to load. Please check your internet connection and try refreshing the page.'); + console.error('Twilio SDK not found after ' + sdkCheckAttempts + ' attempts.'); + debugLog('SDK FAILED after ' + sdkCheckAttempts + ' attempts'); + } + } + + // Start checking after a brief delay + setTimeout(checkAndInitialize, 500); + + // Also keep the window.load as backup for desktop $(window).on('load', function() { - debugLog('Window loaded'); - setTimeout(function() { - debugLog('Checking Twilio: ' + (typeof Twilio)); - if (typeof Twilio === 'undefined') { - showError('Twilio Voice SDK failed to load. Please check your internet connection and try refreshing the page.'); - console.error('Twilio SDK not found. Script may be blocked or failed to load.'); - debugLog('SDK FAILED'); - } else { - console.log('Twilio SDK loaded successfully'); - debugLog('SDK OK, initializing...'); - initializeBrowserPhone(); - } - }, 1000); + debugLog('Window loaded (backup)'); + if (typeof Twilio !== 'undefined' && !device) { + debugLog('Backup init triggered'); + initializeBrowserPhone(); + } }); // Clean up on page unload