想唤醒淘宝app,如何在超链接中加入唤醒淘宝app代码,实现超链接直接唤醒淘宝app并且打开淘宝链接

JavaScript014

想唤醒淘宝app,如何在超链接中加入唤醒淘宝app代码,实现超链接直接唤醒淘宝app并且打开淘宝链接,第1张

我使用的是js唤醒,代码来源于网络,代码如下:var browser = {versions: function() { var u = navigator.userAgent,app = navigator.appVersion return {trident: u.indexOf('Trident') >-1,presto: u.indexOf('Presto') >-1, webKit: u.indexOf('AppleWebKit') >-1, gecko: u.indexOf('Gecko') >-1 &&u.indexOf('KHTML') == -1, mobile: !!u.match(/AppleWebKit.*Mobile.*/), ios: !!u.match(/\(i[^]+( U)? CPU.+Mac OS X/), android: u.indexOf('Android') >-1 || u.indexOf('Linux') >-1, iPhone: u.indexOf('iPhone') >-1, iPad: u.indexOf('iPad') >-1, webApp: u.indexOf('Safari') == -1, souyue: u.indexOf('souyue') >-1, superapp: u.indexOf('superapp') >-1, weixin:u.toLowerCase().indexOf('micromessenger') >-1, Safari:u.indexOf('Safari') >-1 } }(), language: (navigator.browserLanguage || navigator.language).toLowerCase()}if (browser.versions.ios) {//判断是不是苹果系统 window.location.href = "taobao://"//打开taobao协议,唤醒淘宝app setTimeout(function(){window.location.href = "<?php echo $gourl ?>"//把$gourl 变成你的链接地址window.location.href = "<?php echo $gourl ?>" //把$gourl 变成你的链接地址 },2000)}else if (browser.versions.android){//判断是不是安卓系统 window.location.href = "taobao://"//打开taobao协议,唤醒淘宝app setTimeout(function(){ window.location.href = "<?php echo $gourl ?>"//把$gourl 变成你的链接地址window.location.href = "<?php echo $gourl ?>" //把$gourl 变成你的链接地址 },2000)}

先看一下Web中,我们给h1标签添加一个onclick事件,让它在被点击之后,修改当前的url。

Web中的HTML代码:

<html>

<head>

<script>

function getInfo(name)

{

window.location = "/getInfo/"+name

}

</script>

</head>

<body>

<h1 onclick="getInfo('why')">Name</h1>

</body>

</html>

iOS中,先拖拽WebView,访问localhost,然后通过WebView的委托事件监听url跳转操作,并且把跳转截取下来。

也就是说,在onclick的时候,普通浏览器灰跳转到那个url,但是在iOS的这个WebView里面,这个跳转会被拦截,

用这种方式可以巧妙地实现JS调用iOS的原生代码:

//

// DWViewController.m

// DareWayApp

//

// Created by why on 14-6-3.

// Copyright (c) 2014年 DareWay. All rights reserved.

//

#import "DWViewController.h"

@interface DWViewController ()

@property (weak, nonatomic) IBOutlet UIWebView *myWebview // 主页面

@end

@implementation DWViewController

- (void)viewDidLoad

{

[super viewDidLoad]

// Do any additional setup after loading the view, typically from a nib.

// 适配iOS6的状态栏

if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7) {

_myWebview.frame = CGRectMake(0,20,self.view.frame.size.width,self.view.frame.size.height-20)

}

// 加载制定的URL

NSURL *url =[NSURL URLWithString:@"http://localhost"]

NSURLRequest *request =[NSURLRequest requestWithURL:url]

[_myWebview setDelegate:self]

[_myWebview loadRequest:request]

}

// 网页中的每一个请求都会被触发

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

{

// 每次跳转时候判断URL

if([request.mainDocumentURL.relativePath isEqualToString:@"/getInfo/why"])

{

NSLog(@"why")

return NO

}

return YES

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning]

// Dispose of any resources that can be recreated.

}

1、用户第一次访问宣传页面

a、点击Banner,进入到APP Store中对应的APP下载页

b、APP下载页中提示:安装;用户点击安装

c、安装完成后,APP下载页中提示:打开;用户继续点击打开

d、用户正常使用APP

2、用户第二次访问宣传页面

a、点击Banner,进入到APP Store中对应的APP下载页

b、APP下载页中提示:打开;用户直接点击打开

c、用户正常使用APP

3、用户第三次、第四次、...、第N次访问,操作步骤同2