[Android] 앱 실행 혹은 스토어 이동

참고 문서

테스트 환경

  • 안드로이드 4.4.2 크롬 Chrome/43.0.2357.93
  • 안드로이드 4.4.2 내장 브라우저 Chrome/30.0.1599.103
  • IOS 8.3
var callApp = {
    isIphone: false,
    isAndroid: false,
    scheme: '',
    appStoreUrl: '',
    init : function(who) {
        this.isIphone = (navigator.userAgent.match('iPhone') != null
                || navigator.userAgent.match('iPad') != null);
        this.isAndroid = (navigator.userAgent.match('Android') != null);

        if (this.isAndroid) {
            this.scheme = 'intent://[안드로이드앱호출문];package=[플레이스토어패키지];end';
        } else if (this.isIphone) {
            this.scheme = 'customescheme://[IOS앱호출문]';
            this.appStoreUrl = 'http://[앱스토어주소]';
        }
    },
    getIframe: function(id, url) {
        var iframe = document.getElementById(id);
        if (iframe !== null) {
            iframe.parentNode.removeChild(iframe);
        }
        iframe = document.createElement('iframe');
        iframe.id = id;
        iframe.style.visibility = 'hidden';
        iframe.style.display = 'none';
        iframe.src = url;
        return iframe;
    },
    exec: function(who, url) {
        this.init(who);
        if (this.isAndroid) { /* 안드로이드 */
            location.href = this.scheme;
        } else if (this.isIphone) { /* IOS */
            setTimeout(function() {
                location.href = callApp.appStoreUrl;
            }, 500);
            var iframe = this.getIframe('callAppIframe', this.scheme);
            document.body.appendChild(iframe);
        } else { /* 그 외 단말기 */
            location.href = url;
        }
    }
}

2015-07-23 기준 정리

안드로이드는 아이프레임을 사용하면 크롬, 내장브라우저 모두 작동하지 않으며, 아이폰은 본창, 아이프레임 둘 다 가능하지만 아이프레임에서 호출하지 않으면 앱이 설치되어 있지 않을때 얼럿 대화창이 뜬다.

이 함수를 사용자 조작없이, 윈도우 로드 이벤트 등으로 자동실행 되도록 해놓으면 크롬에서 차단하는 현상이 있다.

App not launching from Chrome browser - Navigation is blocked · Issue #156 · EddyVerbruggen/Custom-URL-scheme