[sample coding] javascript 앱 버전 분기

SeoYng·2022년 1월 13일
0
post-thumbnail

javscript로 앱 버전에 따라 다르게 동작하는 로직을 짜야할때가 있다
navigater의 ugerAgent값으로 user의 플랫폼을 확인하여 분기해보자

👀 참고 MDN

👀 예제 코드

코드 📃.js

const isApp = (/iphone|ipad|ipod|android/i.test(navigator.userAgent.toLowerCase()));
const isNewAppVer = isApp ? checkNewAppVer(curAppVer) : false;

const checkNewAppVer = (curAppVer) => {
    
    const uaString = navigator.userAgent;
    const isAndroid = uaString.match(/Android/i);
    const isIos = uaString.match(/iPhone|iPad|iPod/i);
    const SUPPORT_VER = { AOS: '200', IOS: '201' } // ex) vc 200, 2.0.1 

    // 앱 분기
    const supportVer = isAndroid ? SUPPORT_VER[AOS] : SUPPORT_VER[IOS];
    const currentVer = isAndroid ? curAppVer : curAppVer.replace(/\./g, '');
    
    return currentAppVer >= supportAppVer;

}

if (isNewAppVer) {
   // 새로운 내용 적용
} else {
   // 구버전
}
  
profile
Junior Web FE Developer

0개의 댓글