Is there a correct/recommended way of detecting my

2019-04-06 19:12发布

问题:

Trying out Windows Universal apps with JavaScript I noticed the WinJS.Utilities.isPhone property is no longer available, which makes sense since there would be no reason to ask for that at runtime.

I do want to know just for testing purposes if there is a proper way of detecting the device my app is running in.

EDIT: My question is NOT about detecting a mobile browser. I'm talking about about a brand new Universal Windows App for Window 10 that can run on phones, desktops, tablets, Xbox, HoloLEns, IoT devices et all. WinJS had a property that would tell me whether I was running on the phone or not. That property is now gone. Please don't close this question due to duplicate with "detecting mobile browser". That is NOT what I need.

回答1:

Caveat: Any form of device detection is fragile due to the dynamic nature of hardware - a new device could come along tomorrow that breaks your app's logic. It is best to use these APIs only for telemetry / analytics rather than to trigger runtime behaviour.

More often than not, what you really want to know is some attribute of the device or the app that is not inherently tied to the device family (does this device support SystemTray API? Is there a keyboard attached? Is the window more than 500px wide? etc.).

That said, in Windows 10 you can query the DeviceFamily via AnalyticsInfo.VersionInfo.DeviceFamily and it will tell you things like "Mobile" or "Desktop" or "Xbox" etc. (where "Mobile" could be any class of device - phone, tablet, etc.). There is also a property DeviceForm that might be helpful, but again you can't really rely on it at runtime to deterministically say you're running on "a phone."

So basically the answer is "you can use these APIs for telemetry but don't hard-code any values into your app lest it break when a new device arrives on the market." At the very least, always make sure you handle the case where the returned value isn't one you know about a-priori.



回答2:

You can also check out the following links http://www.abeautifulsite.net/detecting-mobile-devices-with-javascript/ http://www.sitepoint.com/detect-mobile-devices-jquery/

and of course a similar post here on stackoverflow with a good answer

Detecting a mobile browser

And talking about Windows 10, extracting from Winjs Github repo, here is the answer.

https://github.com/winjs/winjs/issues/601#issuecomment-87137485



回答3:

There are numerous JS libs to detect which platform/device is used.

I personally love using this lib: https://github.com/kaimallea/isMobile

You will then be able to detect mobile device in such a way:

isMobile.apple.tablet
isMobile.android.phone

and so on.

In case you have an idea to implement such lib yourself, keep in mind that it takes some efforts to keep it up-to-date, because ways of detecting mobile device may change over time.

In general, common way of detecting user device is checking query headers.

Keep in mind, though, that you can't absolutely rely on this information - headers may be easily modified. Google for User Agent for more info.

So "omitting auth process for users with phones" is extremely bad idea