URL Schemes 實(shí)現(xiàn)APP之間的跳轉(zhuǎn)

簡述

通過對比網(wǎng)頁鏈接來理解iOS上的URL Schemes,應(yīng)該就容易多了
URL Schemes 有兩個(gè)單詞:

  • URL:我們都很清楚,http://www.apple.com 就是個(gè)URL,我們也叫他鏈接或網(wǎng)址
  • Schemes:表示的是一個(gè)URL中的一個(gè)位置——最初始的位置,即://之前的那段字符。比如http://www.apple.com 這個(gè)網(wǎng)址的Schemes是http。
    根據(jù)我們上面對URL Schemes的使用,我們可以很輕易地理解,在以本地應(yīng)用為主的iOS上,我們可以像定位一個(gè)網(wǎng)頁一樣,用一種特殊的URL來定位一個(gè)應(yīng)用甚至應(yīng)用里某個(gè)具體的功能。而定位這個(gè)應(yīng)用的,就應(yīng)該這個(gè)應(yīng)用的URL的Schemes部分,也就是開頭兒那部分。比如短信,就是 sms:
    你可以完全按照理解一個(gè)網(wǎng)頁的 URL ——也就是它的網(wǎng)址——的方式來理解一個(gè) iOS 應(yīng)用的 URL,拿蘋果的網(wǎng)站和 iOS 上的微信來做個(gè)簡單對比:
    通過對比網(wǎng)頁鏈接來理解 iOS 上的 URL Schemes,應(yīng)該就容易多了。
網(wǎng)頁(蘋果) iOS 應(yīng)用(微信)
網(wǎng)站首頁/打開應(yīng)用 http://www.apple.com weixin://
子頁面/具體功能 http://www.apple.com/mac/(Mac頁面) weixin://dl/moments(朋友圈)

實(shí)例:

  • 首先創(chuàng)建A,B兩個(gè)app,通過A的點(diǎn)擊事件跳轉(zhuǎn)到B
    項(xiàng)目中URL Schemes配置(被跳轉(zhuǎn)端B)


    URL Schemes配置
  • 說明:
    1. URL Type:表示可以被這些帶前綴URL打開,而不是可以打開這些帶前綴URL的app。
    2. identifier:表示一個(gè)標(biāo)識(shí)符,自己隨便寫(我通常寫公司的反轉(zhuǎn)域名)
    3. URL Schemes:用來定位一個(gè)應(yīng)用

prefs:實(shí)現(xiàn)跳轉(zhuǎn)至蘋果內(nèi)置程序的url scheme,不可改變
appA:標(biāo)識(shí)該應(yīng)用程序的url scheme,盡量保持它的唯一性,可以被appA打開

或者直接在info.plist中設(shè)置Information Property List:


info設(shè)置
  • 說明:
    URL Schemes:是一個(gè)數(shù)組,可以定義多個(gè)應(yīng)用的URL Schemes
  1. 在應(yīng)用A的跳轉(zhuǎn)按鈕中設(shè)置跳轉(zhuǎn)代碼:
    - (void)jumpB:(id)sender
    {
    //無參數(shù)
    NSURL *url1 = [NSURL URLWithString:@"appB:"];
    [UIApplication shareApplication] openURL:url1];
    //有參數(shù)
    NSString *paramStr =[NSString stringWithFormat:@"appB://username=%@&password=%@", @"小劉", @"123"];
    NSURL *url2 = [NSURL URLWithString:[paramStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    [UIApplication shareApplication] openURL:url2];
    }
    點(diǎn)擊之后就可以實(shí)現(xiàn)跳轉(zhuǎn)應(yīng)用B
  2. 在打開應(yīng)用B的過程中,此時(shí)應(yīng)用B有兩種狀態(tài):
    • 第一個(gè)狀態(tài): 應(yīng)用B還沒有啟動(dòng),打開過程中就會(huì)去啟動(dòng),觸發(fā)下面的方法
      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
      NSLog(@"didFinishLaunchingWithOptions---B");
      return YES;
      }
    • 第二個(gè)狀態(tài): 應(yīng)用B已經(jīng)啟動(dòng)了,但是在后臺(tái)運(yùn)行或者掛起,這個(gè)時(shí)候不會(huì)調(diào)用該方法。
  3. 如果一個(gè)應(yīng)用被另外一個(gè)應(yīng)用打開, 在代理方法- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation中判斷喚起的來源source app,根據(jù)Url所攜帶的參數(shù)進(jìn)行不同的操作。比如跳轉(zhuǎn)到制定的頁面,相關(guān)的邏輯處理等等.
    //當(dāng)一個(gè)應(yīng)用程序被其他程序打開的時(shí)候會(huì)調(diào)用這個(gè)方法,在該方法中可以實(shí)現(xiàn)兩個(gè)應(yīng)用程序間的數(shù)據(jù)局傳遞
    //當(dāng)一個(gè)應(yīng)用程序被其他程序打開的時(shí)候會(huì)調(diào)用這個(gè)方法,在該方法中可以實(shí)現(xiàn)兩個(gè)應(yīng)用程序間的數(shù)據(jù)局傳遞
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    {
    NSString *urlStr = [url absoluteString];
    if ([urlStr hasPrefix:@"appB://"]) {
    NSLog(@"appA request params: %@", urlStr);
    urlStr = [urlStr stringByReplacingOccurrencesOfString:@"appB://" 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;
    }

注意:在iOS9之后,還需要在info.plist中添加LSApplicationQueriesSchemes設(shè)置為Array,以此來提高app的安全性,需要設(shè)置URL Schemes白名單,在白名單里面的才能打開app

白名單

附:

  • 常見的調(diào)用系統(tǒng)應(yīng)用url
    // 關(guān)于本機(jī)
    About — prefs:root=General&path=About
    // 輔助功能
    Accessibility — prefs:root=General&path=ACCESSIBILITY
    // 飛行模式
    Airplane Mode On — prefs:root=AIRPLANE_MODE
    // 自動(dòng)鎖屏
    Auto-Lock — prefs:root=General&path=AUTOLOCKB
    // 調(diào)節(jié)設(shè)備背景亮度
    rightness — prefs:root=Brightness
    // 藍(lán)牙
    Bluetooth — prefs:root=General&path=Bluetooth
    // 時(shí)間與日期
    Date & Time — prefs:root=General&path=DATE_AND_TIME
    // FaceTime
    FaceTime — prefs:root=FACETIME
    // 通用
    General — prefs:root=General
    // 鍵盤
    Keyboard — prefs:root=General&path=Keyboard
    // iCloud
    iCloud — prefs:root=CASTLE
    // iCloud存儲(chǔ)空間
    iCloud Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP
    // 語言與區(qū)域
    International — prefs:root=General&path=INTERNATIONAL
    // 定位服務(wù)
    Location Services — prefs:root=LOCATION_SERVICES
    // 音樂
    Music — prefs:root=MUSICMusic
    Equalizer — prefs:root=MUSIC&path=EQ
    Music Volume Limit — prefs:root=MUSIC&path=VolumeLimit
    // 網(wǎng)絡(luò)
    Network — prefs:root=General&path=Network
    Nike + iPod — prefs:root=NIKE_PLUS_IPOD
    // 備忘錄
    Notes — prefs:root=NOTES
    // 通知
    Notification — prefs:root=NOTIFICATIONS_ID
    // 電話
    Phone — prefs:root=Phone
    // 照片
    Photos — prefs:root=Photos
    // 描述文件
    Profile — prefs:root=General&path=ManagedConfigurationList
    // 還原
    Reset — prefs:root=General&path=Reset
    // Safari
    Safari — prefs:root=SafariSiri — prefs:root=General&path=Assistant
    // 聲音
    Sounds — prefs:root=Sounds
    // 軟件更新
    Software Update — prefs:root=General&path=SOFTWARE_UPDATE_LINK
    // iTunes Store 與 APP Store
    Store — prefs:root=STORE
    // Twitter
    Twitter — prefs:root=TWITTER
    // Usage
    Usage — prefs:root=General&path=USAGE
    // VPN
    VPN — prefs:root=General&path=Network/VPN
    // 墻紙
    Wallpaper — prefs:root=Wallpaper
    // WIFI
    Wi-Fi — prefs:root=WIFI
    、、、

  • 當(dāng)你的應(yīng)用在iOS 9中需要使用 QQ/QQ空間/支付寶/微信SDK 的相關(guān)能力(分享、收藏、支付、登錄等)時(shí),需要在“Info.plist”里增加如下代碼:
    <key>LSApplicationQueriesSchemes</key>
    <array>

    <string>wechat</string>
    <string>weixin</string>

        <!-- 新浪微博 URL Scheme 白名單-->
        <string>sinaweibohd</string>
        <string>sinaweibo</string>
        <string>sinaweibosso</string>
        <string>weibosdk</string>
        <string>weibosdk2.5</string>
    
        <!-- QQ、Qzone URL Scheme 白名單-->
        <string>mqqapi</string>
        <string>mqq</string>
        <string>mqqOpensdkSSoLogin</string>
        <string>mqqconnect</string>
        <string>mqqopensdkdataline</string>
        <string>mqqopensdkgrouptribeshare</string>
        <string>mqqopensdkfriend</string>
        <string>mqqopensdkapi</string>
        <string>mqqopensdkapiV2</string>
        <string>mqqopensdkapiV3</string>
        <string>mqzoneopensdk</string>
        <string>wtloginmqq</string>
        <string>wtloginmqq2</string>
        <string>mqqwpa</string>
        <string>mqzone</string>
        <string>mqzonev2</string>
        <string>mqzoneshare</string>
        <string>wtloginqzone</string>
         <string>mqzonewx</string>
        <string>mqzoneopensdkapiV2</string>
        <string>mqzoneopensdkapi19</string>
        <string>mqzoneopensdkapi</string>
        <string>mqzoneopensdk</string>
    
        <!-- 支付寶  URL Scheme 白名單-->
        <string>alipay</string>
        <string>alipayshare</string>
    
    </array>
    

友情參考:
phzean
ZeroIG

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容