iOS - URL Scheme 操做

推薦JLRoutes路由跳轉
NSScannerhtml

在尋找更加靈活的頁面跳轉和通知,我碰見了JLRoutes,從而學習使用URL Scheme來定義界面入口。之前歷來沒有使用過,不過不少大廠和流行的框架都廣泛使用URL Scheme,一直單槍匹馬的我必需要與時俱進了不然會Out~ios

1.預熱

在info.plist中增長一個URL Schemes,如:lvSch。
在Safari中輸入:lvSch:// 便可跳進App內。
注:Schemes須要設計完整,其實我更喜歡叫它某個App的別名。微信

應用場景:
1.在A的App內的H5中連接,點擊連接,跳入B的App內指定界面。
如:在微信App瀏覽知乎H5,點擊特定圖標進入知乎App查看。app

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"*跳轉到B內部的Scheme,次Scheme須要B內部已經填寫好*"]];

2.如何跳到B的App內指定的界面呢?框架

能夠拼接Scheme的格式,例如:Scheme: com.bApp.www://NewsDetail.id122(NewsDetail.id122表明新聞詳情頁,請求id是122)。學習

2.爲什麼我使用JLRoutes?

能夠註冊不少Pattern,方便~this

3.JLRoutes學習使用的一些總結

3.1 給App起一個專屬名字

例如:Pin軟件的Schemes爲Pin
其中有一個動做 - 抓圖到微信,Schemes爲:Pin://gifsender?src=auto&dst=wechatspa

info.plist中起的Schemes我形容爲入口(別名)。至於進入入口乾啥就看後面參數和格式如何規範定義了。.net

3.2 使用JLRoutes在App內註冊各類各樣的Scheme

首先,咱們須要明白一個問題,在Info.plist裏面咱們能夠定義不少Scheme,例如:bAppScheme1,bAppScheme2,...設計

其次,咱們須要獲取相似URL的protocol那樣的一個Route頭:

/// Returns the global routing scheme (this is used by the +addRoute methods by default)
+ (instancetype)globalRoutes;(對routesForScheme:的封裝,Scheme爲自定義靜態字符串對象:JLRoutesGlobalRoutesScheme)

/// Returns a routing namespace for the given scheme
+ (instancetype)routesForScheme:(NSString *)scheme;(這個咱們使用,scheme參數能夠是bAppScheme1,bAppScheme2,...)

而後,咱們能夠在Route中註冊咱們設計好的格式:

/// 註冊一個優先級默認爲0的routePattern給一個Scheme
- (void)addRoute:(NSString *)routePattern handler:(BOOL (^__nullable)(NSDictionary<NSString *, id> *parameters))handlerBlock;

/// 註冊多個優先級默認爲0的routePattern給一個Scheme
- (void)addRoutes:(NSArray<NSString *> *)routePatterns handler:(BOOL (^__nullable)(NSDictionary<NSString *, id> *parameters))handlerBlock;

/// 註冊一個優先級爲priority的routePattern給一個Scheme(上兩個API的priority參數默認爲0)
- (void)addRoute:(NSString *)routePattern priority:(NSUInteger)priority handler:(BOOL (^__nullable)(NSDictionary<NSString *, id> *parameters))handlerBlock;

3.3 代碼

相關文章
相關標籤/搜索