Nanfeng

Notes on software development, code, and curious ideas

Detecting Mobile or Desktop in a Telegram Mini App

Run a user-agent check during Mini App initialization:

1
2
3
4
5
document.addEventListener('DOMContentLoaded', () => {
const ua = navigator.userAgent.toLowerCase();
const isMobile = /mobile|android|iphone|ipad|phone/i.test(ua);
window.Device = isMobile ? 'mobile' : 'desktop';
});

For a Cocos Creator Web Mobile export, add this to index.html, then read the global value in TypeScript:

1
2
const device = window['Device'];
console.log('Device:', device);

User-agent detection is heuristic. Prefer Telegram client information when a suitable official field is available for the behavior you need.

+