0

The title says it all: Suppose I have a PhoneGap app, built for both Android and iOS. A user runs my app on his/her mobile phone. At some point I want to give a different message if it's Android or iPhone... How can I detect this in my JavaScript code?

Also: Can I do it without an additional PhoneGap plugin that will ask the user for extra permissions?

Free Bud
  • 746
  • 10
  • 30

2 Answers2

1

If you want to device platform you can go with below code.

var deviceType = (navigator.userAgent.match(/iPad/i))  == "iPad" ? "iPad" : (navigator.userAgent.match(/iPhone/i))  == "iPhone" ? "iPhone" : (navigator.userAgent.match(/Android/i)) == "Android" ? "Android" : (navigator.userAgent.match(/BlackBerry/i)) == "BlackBerry" ? "BlackBerry" : "null";

alert(deviceType);

Or as per new changes you can use

// Depending on the device, a few examples are:
//   - "Android"
//   - "BlackBerry"
//   - "iOS"
//   - "webOS"
//   - "WinCE"
//   - "Tizen"
//   - "browser"
var devicePlatform = device.platform;

devicePlatform will return you mentioned device type string. It will not require any kind of plugin or permission.

Flutterian
  • 1,761
  • 1
  • 21
  • 47
  • "as per new changes" ?? – Free Bud Nov 06 '17 at 06:45
  • Above one is pure javascript code. Below one has been introduced by phonegap. You can use anyone. Please do let me know is that what you was looking for? – Flutterian Nov 06 '17 at 06:48
  • It does give 'Android' on my Samsung Galaxy. Haven't tested in iPhone yet. In my PC (using PhoneGap Desktop with a browser) I get "Uncaught ReferenceError: device is not defined" – Free Bud Nov 06 '17 at 12:29
  • Did you tested the code on iphone? If its working, then kindly upvote for the answer so others will also get solution easily. – Flutterian Nov 08 '17 at 09:31
1

use like this to detected device type

if (/(android)/i.test(navigator.userAgent)) {  // for android & amazon-fireos

   alert("Android ");
} else if (/(ipod|iphone|ipad)/i.test(navigator.userAgent)) {  // for ios

   alert("IOS");
}
Younes Zaidi
  • 1,180
  • 1
  • 8
  • 25