Allen Chiang

iOS程序猿的blog

© 2014. All rights reserved.

用javascript唤起iOS和Android客户端

大部分pc到客户端引流都采用二维码扫描的方式,扫码这里就不说了,关键说一下如何从扫码到唤起客户端,暂时仅包括android和ios两大平台。

处理逻辑

  1. 扫码到一个http页面
  2. 页面上写javascript通过useragent判断当前平台android/iphone/ipad;
  3. create一个隐藏的frame,设置他的src为应用注册的schema来唤起客户端,比如淘宝主客就是taobao://;
  4. 如果用户没有安装客户端,再通过settimeout引导到客户端下载页;
  5. 这里要特别注意android和ios一个最大的区别是android唤起的时间要比ios慢许多;比如ios只需要200ms就可以判断唤起,但是android起码要600ms,所以引导到客户端下载页的timeout要有所区别;

代码示例

	var noClientCallback = function(){
		window.location = “http://2.taobao.com/m.html”;
	};

	(function(){
		var outTime = (navigator.userAgent.match(/android/i))?600:200;
		var goUrl = “taobao://wnwebview?url=http://www.taobao.com/”;
		var e_frame = document.createElement(‘frame’);
		e_frame.setAttribute(‘id’, ‘J_smartFrame’);
		e_frame.style.cssText = ‘display:none’;
		document.body.appendChild(e_frame);
		e_frame.setAttribute(‘src’,goUrl);
		window.setTimeout(function(){
			if(e_frame){
				document.body.removeChild(e_frame);
			}
			noClientCallback();
		},outTime);
	})();

Android Update

最近发现部分android手机不能正常唤起客户端,主要原因google有解释:https://developer.chrome.com/multidevice/android/intents

也就是说现在Android的chrome下(25及以后的版本),上面的隐藏iframe方式不能正常生效了,唤起客户端需要这么写:

window.location = ‘intent://item/#Intent;scheme=taobao;package=com.taobao;end’;

如果还需要带一些参数的话,可以写在item后面

window.location = ‘intent://item?id=12345678/#Intent;scheme=taobao;package=com.taobao;end’;

另外如果还有唤起客户端的需要,建议看看这个repo:https://github.com/miaojing/redirectToNative

以及他的demo:http://gallery.kissyui.com/redirectToNative/1.2/demo/index.html

comments powered by Disqus