iOS/Android 운영 체제 검색
제가 조사를 좀 해봤는데, 이 질문이 떠올랐어요. 하지만 제가 의도한 방식은 아니었어요.QR코드 랜딩인 어플리케이션을 다운로드 할 수 있는 고객용 페이지를 만들고 있습니다.QR코드 2개를 한 페이지에 출력할 필요가 없기 때문에 현재 운영체제(Apple/Android/Other[지원하지 않음])를 검출하여 그 값에 따라 요소를 수정하고 싶습니다.
「detect mobile browers」라고 하는 스크립트를 보았습니다만, 유저가 모바일인지 아닌지를 알기 위해서입니다만, 유저가 실행하고 있는 operating system을 특정해, 최적인 애플리케이션 버전을 제안하고 싶습니다.
이 질문과 유사한 답변이 더 오래되었거나 신뢰할 수 없는 것 같아서(Android 태블릿 브라우저가 인식되지 않음) 새로운 답변을 찾고 있습니다.어떻게 하면 좋을까요? (가능하다면 jQuery - Javascript - PHP 순으로 사용하는 것이 좋습니다.)
사용자 에이전트 문자열을 테스트할 수 있습니다.
/**
* Determine the mobile operating system.
* This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'.
*
* @returns {String}
*/
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Windows Phone must come first because its UA also contains "Android"
if (/windows phone/i.test(userAgent)) {
return "Windows Phone";
}
if (/android/i.test(userAgent)) {
return "Android";
}
// iOS detection from: http://stackoverflow.com/a/9039885/177710
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
return "iOS";
}
return "unknown";
}
해결책 1: 사용자 에이전트 스니핑
Android 및 iPhone의 경우:
if( /Android|webOS|iPhone|iPad|iPod|Opera Mini/i.test(navigator.userAgent) ) {
// run your code here
}
Blackberry 및 Windows Phone을 포함한 모든 모바일 장치를 탐지하려면 다음과 같은 포괄적인 버전을 사용할 수 있습니다.
var deviceIsMobile = false; //At the beginning we set this flag as false. If we can detect the device is a mobile device in the next line, then we set it as true.
if(/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|ipad|iris|kindle|Android|Silk|lge |maemo|midp|mmp|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino/i.test(navigator.userAgent)
|| /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(navigator.userAgent.substr(0,4))) {
deviceIsMobile = true;
}
if(deviceIsMobile){
// run your code here
}
단점: 새로운 전화기와 브랜드가 나날이 출시됨에 따라 사용자 에이전트 문자열이 변경되고 업데이트되고 있습니다.따라서 모든 모바일 장치를 지원하려면 이 목록을 최신 상태로 유지해야 합니다.
솔루션 2: 모바일 검출 JS 라이브러리
이를 위해 모바일 탐지 JS 라이브러리를 사용할 수 있습니다.
단점: 이러한 JavaScript 기반 장치 감지 기능은 iPhone, Android 및 Palm WebOS 장치 등 최신 세대의 스마트폰에서만 사용할 수 있습니다.이러한 장치 탐지 기능은 이전 BlackBerry, PalmOS 및 Windows Mobile 장치 등 JavaScript 지원이 부족했던 이전 스마트폰에서는 작동하지 않을 수 있습니다.
navigator.platform을 사용하여 브라우저가 설치된 운영 체제를 가져올 수 있습니다.
function getPlatform() {
var platform = ["Win32", "Android", "iOS"];
for (var i = 0; i < platform.length; i++) {
if (navigator.platform.indexOf(platform[i]) >- 1) {
return platform[i];
}
}
}
getPlatform();
웹사이트에 React Js를 사용하는 경우 https://www.npmjs.com/package/react-device-detect를 사용하십시오.
이 문제는 이미 해결되었습니다.jQuery에서 모바일 디바이스를 검출하는 가장 좋은 방법은 무엇입니까?
받아들여진 답변에서는 기본적으로 아이폰, 아이팟, 안드로이드 기기 또는 무엇이 진실인지 테스트합니다., 당신이 .if( /Android/i.test(navigator.userAgent) ) { // some code.. }안드로이드 ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★」
그러나 사용자 에이전트는 변경될 수 있기 때문에 신뢰할 수 없습니다.가장 좋은 것은 여전히 모든 모바일 플랫폼을 위한 범용적인 것을 개발하는 것입니다.
또한 php의 사용자 에이전트에서도 이 작업을 수행할 수 있습니다.
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
if(stripos($userAgent,'android') !== false) { // && stripos($userAgent,'mobile') !== false) {
header('Location: http://oursite.com/download/yourApp.apk');
exit();
}
여기 간단한 js 스크립트(html 태그)가 있습니다.이 스크립트는 디바이스의 유형을 검출하여 올바른 스토어로 리다이렉트 합니다.
<script>
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Huawei Phone must come first because its UA also contains "Android"
if (userAgent.includes("HUAWEI")) {
window.location.replace("https://appgallery.huawei.com/app/C101184875");
} else if (userAgent.includes("Android")) {
window.location.replace("https://play.google.com/store/apps/details?id=org.telegram.messenger&hl=en&gl=US");
} else if (userAgent.includes("iPhone")) {
window.location.replace("https://apps.apple.com/us/app/telegram-messenger/id686449807");
} else {
//cant be sure about device, so redirect to home page
window.location.replace("https://google.ro");
}
</script>
요건에 따라 동작하는 Firbase Dynamic 링크를 작성할 수도 있습니다.여러 플랫폼을 지원합니다.이 링크는 프로그래밍을 통해 수동으로도 만들 수 있습니다.그런 다음 이 링크를 QR 코드에 삽입할 수 있습니다.
대상 앱이 설치된 경우 링크는 사용자를 앱으로 리디렉션합니다.설치되지 않은 경우 Play Store/App Store/Any any configured website로 수정합니다.
이것과 다른 종류의 클라이언트 탐지에 대해서는, 다음의 js 라이브러리를 추천합니다.http://hictech.github.io/navJs/tester/index.html
구체적인 답변의 경우:
navJS.isIOS() || navJS.isAndroid()
cordova-device-plugin을 사용하면 cordova-device-plugin이
device.platform
안드로이드의 경우 "Android"가 되고 윈도우의 경우 "Windows"가 됩니다.디바이스 및 브라우저에서 시뮬레이션할 때 작동합니다.다음은 디바이스 값을 표시하는 건배사입니다.
window.plugins.toast.showLongTop(
'Cordova: ' + device.cordova + '\n' +
'Model: ' + device.model + '\n' +
'Platform: ' + device.platform + '\n' +
'UUID: ' + '\n' +
device.uuid + '\n' +
'Version: ' + device.version + '\n' +
'Manufacturer ' + device.manufacturer + '\n' +
'isVirtual ' + device.isVirtual + '\n' +
'Serial ' + device.serial);
function getOS() {
var uA = navigator.userAgent || navigator.vendor || window.opera;
if ((/iPad|iPhone|iPod/.test(uA) && !window.MSStream) || (uA.includes('Mac') && 'ontouchend' in document)) return 'iOS';
var i, os = ['Windows', 'Android', 'Unix', 'Mac', 'Linux', 'BlackBerry'];
for (i = 0; i < os.length; i++) if (new RegExp(os[i],'i').test(uA)) return os[i];
}
다음과 같이 사용할 수 있습니다.
alert(getOS());
작업 코드 조각:
function getOS() {
var uA = navigator.userAgent || navigator.vendor || window.opera;
if ((/iPad|iPhone|iPod/.test(uA) && !window.MSStream) || (uA.includes('Mac') && 'ontouchend' in document)) return 'iOS';
var i, os = ['Windows', 'Android', 'Unix', 'Mac', 'Linux', 'BlackBerry'];
for (i = 0; i < os.length; i++) if (new RegExp(os[i],'i').test(uA)) return os[i];
}
console.log(getOS());
언급URL : https://stackoverflow.com/questions/21741841/detecting-ios-android-operating-system
'source' 카테고리의 다른 글
| Python에서 Key 오류가 발생함 (0) | 2022.12.31 |
|---|---|
| Java에서 폴더 삭제 (0) | 2022.12.31 |
| 오류 1396(HY000):'user'@'localhost'에 대한 DROP USER 작업이 실패했습니다. (0) | 2022.12.31 |
| 중복 키 업데이트가 있는 MySQL 로드 데이터 파일 (0) | 2022.12.31 |
| 더블을 Big Decimal로 변환하고 Big Decimal Precision을 설정합니다. (0) | 2022.12.31 |