在手機的瀏覽器上通過連接打開App


Android系統中實現


 

1、在系統系統自帶的瀏覽器中

首先做成HTML的頁面,頁面內容格式如下:

<a href="[scheme]://[host]/[path]?[query]">啟動應用程序</a> 

這一句就可以了。

各個項目含義如下所示:

scheme:判別啟動的App。 ※詳細后述

host:適當記述

path:傳值時必須的key     ※沒有也可以

query:獲取值的Key和Value  ※沒有也可以

 作為測試好好寫了一下,如下:

<a href="myapp://jp.app/openwith?name=zhangsan&age=26">啟動應用程序</a>  

 接下來是Android端。

首先在AndroidManifest.xml的MAIN Activity下追加以下內容。(啟動Activity時給予)

※必須添加項

<intent-filter>  
    <action android:name="android.intent.action.VIEW"/>  
    <category android:name="android.intent.category.DEFAULT" />  
    <category android:name="android.intent.category.BROWSABLE" />  
    <data android:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter>

HTML記述的內容加入<data …/>。
其中必須的內容僅scheme,沒有其他內容app也能啟動。

※注意事項:intent-filter的內容【android.intent.action.MAIN】和 【android.intent.category.LAUNCHER】這2個,不能與這次追加的內容混合。
 所以,如果加入了同一個Activity,請按以下這樣做,否則會導致應用圖標在桌面消失等問題。

<intent-filter>  
    <action android:name="android.intent.action.MAIN"/>  
    <category android:name="android.intent.category.LAUNCHER" />  
</intent-filter>  
<intent-filter>  
    <action android:name="android.intent.action.VIEW"/>  
    <category android:name="android.intent.category.DEFAULT" />  
    <category android:name="android.intent.category.BROWSABLE" />  
    <data android:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter> 

這樣的話,沒有問題。

 接下來在Activity中需要取值的地方添加以下代碼,我是直接寫在OnCreate函數里的:

 
Intent i_getvalue = getIntent();  
String action = i_getvalue.getAction();  
  
if(Intent.ACTION_VIEW.equals(action)){  
    Uri uri = i_getvalue.getData();  
    if(uri != null){  
        String name = uri.getQueryParameter("name");  
        String age= uri.getQueryParameter("age");  
    }  
}

這樣就能獲取到URL傳遞過來的值了。

2、在第三方的瀏覽器中

把一個http服務宿主在本地應用中,本地的服務地址為127.0.0.1:8765中,宿主用於監控服務數據,並打開自身。

3、在微信中打開

在微信開放平台登記應用之后,可以獲得appid,通過這個appid就可以跳轉到你的app。
iOS平台格式如下:appid://openwebview/?ret=0,appid要替換成實際的,后面可以帶參數,在你的app可以接收到。
例如:location.href = wx234ad233ae222://openwebview/?ret=0

IOS系統中實現


 

1、在系統自帶的瀏覽器

 

經常使用Safari瀏覽器瀏覽網頁點擊url會喚醒該網站的手機版app

 

需要在app的工程中設置

 

1、打開工程中的myapp-Info.plist文件

 

2、打開文件中新增URL TYPES的一項

 

3、在工程中實現如下方法

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    if (url) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"你喚醒了您的應用" delegate:selfcancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
        [alertView show];
    }
    return YES;
 }

 

4、在Safari瀏覽器輸入myapp:// ,就可以啟動應用了。

2、在自身瀏覽器上顯示Banner,有就顯示打開,沒有就提示下載

<meta name="apple-itunes-app" content="app-id=432274380">

3、在第三方的瀏覽器中

把一個http服務宿主在本地應用中,本地的服務地址為127.0.0.1:8765中,宿主用於監控服務數據,並打開自身。

4、在微信中打開

在微信開放平台登記應用之后,可以獲得appid,通過這個appid就可以跳轉到你的app。
iOS平台格式如下:appid://openwebview/?ret=0,appid要替換成實際的,后面可以帶參數,在你的app可以接收到。
例如:location.href = wx234ad233ae222://openwebview/?ret=0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM