推荐JLRoutes路由跳转
NSScannerhtml
在寻找更加灵活的页面跳转和通知,我碰见了JLRoutes
,从而学习使用URL Scheme来定义界面入口。之前历来没有使用过,不过不少大厂和流行的框架都广泛使用URL Scheme,一直单枪匹马的我必需要与时俱进了不然会Out~ios
在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)。学习
能够注册不少Pattern,方便~this
例如:Pin软件
的Schemes为Pin
其中有一个动做 - 抓图到微信,Schemes为:Pin://gifsender?src=auto&dst=wechat
spa
info.plist中起的Schemes我形容为入口(别名)。至于进入入口干啥就看后面参数和格式如何规范定义了。.net
首先,咱们须要明白一个问题,在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;