最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there a correctrecommended way of detecting my UWP app I'm running on a Phone? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question edited May 9, 2015 at 13:29 sebagomez asked May 5, 2015 at 14:07 sebagomezsebagomez 9,6097 gold badges53 silver badges90 bronze badges 2
  • possible duplicate of Detecting a mobile browser – user177800 Commented May 5, 2015 at 19:25
  • 1 it is not a duplicate... I extended my question so every body understands what I'm asking... please read the whole question before proposing an answer – sebagomez Commented May 6, 2015 at 14:08
Add a ment  | 

3 Answers 3

Reset to default 12

Caveat: Any form of device detection is fragile due to the dynamic nature of hardware - a new device could e 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.

You can also check out the following links http://www.abeautifulsite/detecting-mobile-devices-with-javascript/ http://www.sitepoint./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./winjs/winjs/issues/601#issuement-87137485

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

I personally love using this lib: https://github./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, mon 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

发布评论

评论列表(0)

  1. 暂无评论