> php教程 > PHP开发 > 본문

QR 코드를 스캔하여 앱의 구현 코드를 엽니다.

高洛峰
풀어 주다: 2016-12-07 10:49:17
원래의
2735명이 탐색했습니다.

최근 친구가 저에게 이런 질문을 했습니다. 먼저 프로젝트 요구사항을 알려드리겠습니다. QR 코드를 스캔하여 앱을 열면 앱이 실행되지 않습니다.

웹 페이지를 사용하여 앱을 직접 호출하는 것은 불가능합니다. 먼저 네이티브 측에서 일부 구성을 수행해야 합니다.

먼저 안드로이드와 애플은 통화방식이 다릅니다.

그래서 단말기를 먼저 판단해야 합니다.

var u = navigator.userAgent,
app = navigator.appVersion;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //android终端或者uc浏览器
var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
로그인 후 복사

그 후에는 Apple이 헤더에 app-id< Meta name='apple-itunes-app' content='app-id=1115968341'>

다음은 Apple의 코드입니다

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name=&#39;apple-itunes-app&#39; content=&#39;app-id=1115968341&#39;>
<title></title>
</head>
<body>
<a href="https://itunes.apple.com/cn/app/yi-fang-kuai-le-xue-tang/id1115968341?mt=8" id="openApp">点击打开</a>
<script type="text/javascript">
//苹果
document.getElementById(&#39;openApp&#39;).onclick = function(e){
// 通过iframe的方式试图打开APP,如果能正常打开,会直接切换到APP,并自动阻止a标签的默认行为
// 否则打开a标签的href链接
var ifr = document.createElement(&#39;iframe&#39;);
ifr.src = &#39;iosefunbox://&#39;;
ifr.style.display = &#39;none&#39;;
document.body.appendChild(ifr);
window.setTimeout(function(){
document.body.removeChild(ifr);
},3000)
};
</script>
</body>
</html>
로그인 후 복사

여기의 ifr.src는 앱을 열 수 있는 프로토콜 경로입니다. Android와 Apple은 다릅니다.


안드로이드 기기라면 간단할 것입니다

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
</head>
<body>
<a href="http://a.app.qq.com/o/simple.jsp?pkgname=com.edufound.mobile" id="openApp">点击打开</a>
<script type="text/javascript">
//安卓
// /**/window.location.href = "http://a.app.qq.com/o/simple.jsp?pkgname=com.edufound.mobile";
// 通过iframe的方式试图打开APP,如果能正常打开,会直接切换到APP,并自动阻止a标签的默认行为
// 否则打开a标签的href链接
var ifr = document.createElement(&#39;iframe&#39;);
ifr.src = &#39;efunbox://efunbox.app/efunbox/open&#39;;
ifr.style.display = &#39;none&#39;;
document.body.appendChild(ifr);
window.setTimeout(function(){
document.body.removeChild(ifr);
},3000);
</script>
</body>
</html>
로그인 후 복사

네, 애플이 앱스토어로 바로 점프할 수 있다고 하더군요. 직접 열어야 합니다. Android의 경우 직접 열어야 합니다.

이렇게 하면 하나의 웹페이지에 정리할 수 있습니다.

위의 링크는 Tencent App Store로 바로 이동합니다.

웹페이지를 스캔해서 접속하면 문제 없습니다.

그런데 무슨 일이 일어날 것 같은 느낌이 들어서 위챗으로 스캔했는데 속았습니다. Android는 링크만 열며 WeChat이 이를 가로채기 때문에 앱으로 이동할 수 없습니다. Apple의 경우에도 마찬가지입니다. 유일한 해결책은 사용자에게 브라우저에서 링크를 열라는 메시지를 표시하는 것입니다. 문제없어요. 이 방법은 무력한 조치였지만 나중에 Apple 컴퓨터에 대한 해결책이 발견되었습니다. 링크 A의 점프는 Tencent App Store에 나열된 링크로 직접 점프한 다음 WeChat이 이를 내부적으로 처리하고 자동으로 앱 스토어로 점프합니다.

마지막은 통합코드입니다.










로그인 후 복사

보충:

QR 코드를 스캔하여 앱으로 이동

1. 설치됨

<html>
<head>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<h2><a id="applink1" href="mtcmtc://profile/116201417">Open scheme(mtcmtc) defined in iPhone with parameters </a></h2>
<h2><a id="applink2" href="unknown://nowhere">open unknown with fallback to appstore</a></h2>
<p><i>Only works on iPhone!</i></p>
<script type="text/javascript">
// To avoid the "protocol not supported" alert, fail must open another app.
var appstore = "itms://itunes.apple.com/us/app/facebook/id284882215?mt=8&uo=6";
function applink(fail){
return function(){
var clickedAt = +new Date;
// During tests on 3g/3gs this timeout fires immediately if less than 500ms.
setTimeout(function(){
// To avoid failing on return to MobileSafari, ensure freshness!
if (+new Date - clickedAt < 2000){
window.location = fail;
}
}, 500);
};
}
document.getElementById("applink1").onclick = applink(appstore);
document.getElementById("applink2").onclick = applink(appstore);
</script>
</body>
</html>
로그인 후 복사

2. 프로젝트 대상에서 schemurl을 열고 //

을 추가합니다.

관련 라벨:
app
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!