天天看點

iOS兩個App應用之間的跳轉

開發iOS項目的時候,有可能會遇到兩個APP應用互相調用的需求,比如說:支付寶支付......等等。

下面來詳細介紹實作的步驟:

1,添加URL Types項

a,打開項目中info.plist檔案,在infomation property list項下面增加一項URL Typs

iOS兩個App應用之間的跳轉

2,配置URL Scheme

a,展開URL types,再展開Item1,将Item1下的URL identifier修改為URL Scheme

b,展開URL Scheme,将Item1的内容修改為myapp

(其他應用可通過”myapp://“來通路此自定義URL的應用程式)

iOS兩個App應用之間的跳轉

3,其他應用的跳轉

作為調用者的我,需要通過:

?

1 2 3

NSString *paramStr = [NSString stringWithFormat:@

"myAppTest://username=%@&age=%@&address=%@"

, @

"test123"

, @

"100"

, @

"上海市"

];

NSURL *url = [NSURL URLWithString:[paramStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

[[UIApplication sharedApplication] openURL:url];

這段代碼來跳轉目标應用并傳遞參數。

4,參數的接收

那麼作為一個Provider怎麼去接收Customer傳遞過來的參數呢?

首先,在找到項目中的AppDelegate.m檔案,然後找到openURL方法(如果沒有就去實作它)。OK,到這裡你已經完成了90%了,接着繼續

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

- (

BOOL

)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

NSString *urlStr = [url absoluteString];

if

([urlStr hasPrefix:@

"myAppTest://"

]) {

NSLog(@

"TestAppDemo1 request params: %@"

, urlStr);

urlStr = [urlStr stringByReplacingOccurrencesOfString:@

"myAppTest://"

withString:@

""

];

NSArray *paramArray = [urlStr componentsSeparatedByString:@

"&"

];

NSLog(@

"paramArray: %@"

, paramArray);

NSMutableDictionary *paramsDic = [[NSMutableDictionary alloc] initWithCapacity:0];

for

(

int

i = 0; i < paramArray.count; i++) {

NSString *str = paramArray[i];

NSArray *keyArray = [str componentsSeparatedByString:@

"="

];

NSString *key = keyArray[0];

NSString *value = keyArray[1];

[paramsDic setObject:value forKey:key];

NSLog(@

"key:%@ ==== value:%@"

, key, value);

}

}

return

NO;

}

通過本身自定的參數拼接規則,來解析參數。

到這裡已經完成了應用之間的跳轉,怎麼樣是不是很簡單?

附錄:常用iOS URL Scheme

iOS預設:

名稱 URL Scheme
Safari: http://
maps: http://maps.google.com
Phone: tel:
SMS: sms:
Mail: mailto:
iBooks: ibooks://
App Store: http://itunes.apple.com
iTunes: http:http://phobos.apple.com
Music / iPod: music:
Videos: videos:

常用第三方軟體:

名稱 URL Scheme Bundle identifier
QQ: mqq://
微信: weixin://
騰訊微網誌: TencentWeibo://
淘寶: taobao://
支付寶: alipay://
微網誌: sinaweibo://
weico微網誌: weico://
QQ浏覽器: mqqbrowser:// com.tencent.mttlite
uc浏覽器: ucbrowser://

com.ucweb.iphone.uc 

com.ucweb.iphone.lowversion

海豚浏覽器: dolphin:// com.dolphin.browser.iphone.chinese
歐朋浏覽器: ohttp://(後要接位址,不然出錯) com.oupeng.mini
搜狗浏覽器: SogouMSE:// com.sogou.SogouExplorerMobile
百度地圖:

baidumap:// 

bdmap://

com.baidu.map
百度地圖: bdmap:// com.baidu.map
Chrome: googlechrome://
優酷: youku://
京東: openapp.jdmoble://
人人: renren://
美團: imeituan://
1号店: wccbyihaodian://
我查查: wcc://
有道詞典: yddictproapp://
知乎: zhihu://
點評:

dianping:// 

dianping://search

微盤: sinavdisk://
豆瓣fm: doubanradio://
網易公開課: ntesopen://
名片全能王: camcard://
淘寶寶貝搜尋 taobao://http://s.taobao.com/?q=[prompt]
淘寶店鋪搜尋 taobao://http://shopsearch.taobao.com/browse/shop_search.htm?q=[prompt]