好久好久沒寫過文章了,一是最近項目太忙了,沒時間寫。二是也沒有時間學習新的東西,想寫點什么卻又無從下筆。一味的去寫這個API怎么用,那個新技術怎么用,又顯的沒意思。沒有項目經驗總結的技術知識講解,總感覺有些蒼白。最近在做混合App開發這塊,從開始的ionic 框架,到后來的mui框架,讓我在混合開發這塊有了更深的理解,如果在這塊要寫點什么無非漫天蓋地的這個指令怎么用,那個模版怎么用,數據怎么進行雙向綁定,等等,但是這些網上已經很多資料了,等不太忙了,我想我會總結一篇這些框架的使用心得吧。但是我今天不講這個,我們來談一談在原生app中(iOS android)如何使用動態路由機制來搭建整個app的框架。
路由機制在web開發中是比較常見的,app開發中還是很少聽到這種概念的,目前有些大公司采用的組件化開發(手淘,攜程,蘑菇街等),倒是跟我們講的有很多相同之處,不過它們的比較復雜,而且網上很多組件化開發,路由機制,沒有一個能給出完整代碼示例的,看著讓人云里霧里的。索性自己就借鑒它們的思想,加上一點個人的理解,搞出了一個簡單實用的可行性demo出來。我們主要介紹以下三方面內容:
1 什么是動態路由
2 它能解決我們什么問題
3 如何在項目中實現
一 什么是動態路由
原生開發沒這概念,我們借助angular路由機制來解釋這一概念,所謂路由,就是一套路徑跳轉機制,事先通過配置文件定義好一個路徑映射文件,跳轉時根據key去找到具體頁面,當然angular會做一些緩存,頁面棧的管理等等一些操作,它大致的定義是這樣的
angular.module('app',[]) .config('$routeProvider',function($routeProvider) { $routeProvider .when('/',{ templateUrl:'view/home.html', controller:'homeCtrl' } ) .when('/',{ templateUrl:'view/home.html', controller:'homeCtrl' } ) .when('/',{ templateUrl:'view/home.html', controller:'homeCtrl' } ) .ontherwise({ redirective:'/' }) })
config函數是一個配置函數。在使用
$routeProvider這樣的一個服務。when:代表當你訪問這個“/”根目錄的時候 去訪問 templateUrl中的那個模板。 controller可想已知,就是我們配套的controller,就是應用于根目錄的這個 模板時的controller。
ontherwise 就是當你路徑訪問錯誤時,找不到。最后跳到這個默認的 頁面。
為此我們可以總結一下幾個特點:
1 一個映射配置文件
2 路徑出錯處理機制
這就是路由的基本意思,我們看看,在原生開發中,采用此種方式,他能解決我們什么問題。
二 它能解決我們什么問題
首先我們來比較一下我們以前的結構模式以及與 加入路由機制后的項目結構,實現路由機制,不僅需要一個映射文件,還需要一套路由管理機制,那么采用路由機制,我們的項目架構就跟原來不一樣了,如下圖:
原先業務之間的調用關系.png
接下來我們看一下平時我們采用的頁面跳轉方法:
iOS 下
[selfpresentViewController:controlleranimated:YEScompletion:nil]; [self.navigationControllerpushViewController:controlleranimated:YES];
android 下
Intentintent = new Intent(this, A.class); startActivity(intent); startActivityForResult(Intentintent, Int requestCode)
我們看一下它有哪些缺點:
(1)都要在當前頁面引入要跳轉頁面的class 類。這就造成了頁面的耦合性很高。
(2)遇到重大bug,不能夠及時的修復問題,需要等待更新發版后才能解決。
(3)推送消息,如果入口沒有關與頁面的引入處理,則不能跳轉到指定頁面。
引入路由機制后我們能否解決這些問題呢?
試想一下,如果我們通過一個配置文件來映射頁面跳轉關系,而且通過反射機制來取消頭文件的引入問題,是不是我們就可以解決以上那些弊端了呢,比如,我們線上應用出現bug, 導致某個頁面一打開,app就跪了,那我們是不是就可以通過更新路由配置文件,把它映射到另一個頁面去:一個錯誤提示文件,或者一個線上H5能實現相同功能的頁面。這樣的話,原生app也具有了一定的動態更新能力,其實想想還有很多好處,比如項目功能太多原生開發要很長時間,但是領導又急著要上線,那么我們是不是就可以先開發一個網頁版的模塊,app路由映射到這個web頁面,讓用戶先用著,等我們原生開發完了,然后再改一下映射文件,沒升級的依舊用H5的路由,升級的就用原生的路由,如果H5頁面我們要廢棄了,那我們整體就可以路由到一個升級提升的頁面去了。
總結一下路由機制能解決我們哪些問題:
1 避免引入頭文件,是頁面之間的依賴大大變少了(通過反射動態生成頁面實例)。
2 線上出現重大bug,給我們提供了一個及時修補的入口
3 網頁和原生切換更方便,更自由。
4 可以跳轉任意頁面 例如我們常用的推送,要打開指定的頁面,以前我們怎么做的,各種啟動判斷,現在呢,我們只要給發送消息配個路由路徑就行了,打開消息,就能夠跳轉到我們指定的頁面。
等等,其它好處自行發掘。
三 如何在項目中實現
說了這么多概念性問題,下面我們就用代碼來實現我們的構想, 下面先以IOS平臺為例:
我們先看一下demo結構
說明:WXRouter 路由管理文件
demo 路由使用示例
urlMap.plist 路由配置文件
我們主要講解一下 WXRouter里面的幾個文件,以及ViewController文件,還有urlmap.plist文件,其他請下載示例demo,文末我會給出demo地址。
#import #import @interface WXRouter : NSObject (id)sharedInstance; -(UIViewController *)getViewController:(NSString *)stringVCName; -(UIViewController *)getViewController:(NSString *)stringVCNamewithParam:(NSDictionary *)paramdic; @end
#import quot;WXRouter.hquot; #import quot;webView.hquot; #import quot;RouterError.hquot; #import quot;PlistReadUtil.hquot; #define SuppressPerformSelectorLeakWarning(Stuff) \ do { _Pragma(quot;clang diagnostic pushquot;) \ _Pragma(quot;clang diagnostic ignored \quot;-Warc-performSelector-leaks\quot;quot;) \ Stuff; \ _Pragma(quot;clang diagnostic popquot;) \ } while (0) @implementationWXRouter (id)sharedInstance { static dispatch_once_tonceToken; static WXRouter * router; dispatch_once(amp;onceToken,^{ router = [[WXRouteralloc] init]; }); return router; } -(UIViewController *)controller:(UIViewController *)controllerwithParam:(NSDictionary *)paramdicandVcname:(NSString *)vcName { SELselector = NSSelectorFromString(@quot;iniViewControllerParam:quot;); if(![controllerrespondsToSelector: selector]){//如果沒定義初始化參數方法,直接返回,沒必要在往下做設置參數的方法 NSLog(@quot;目標類:%@未定義:%@方法quot;,controller,@quot;iniViewControllerParam:quot;); return controller; } if(paramdic == nil) { //如果參數為空 URLKEY 頁面唯一路徑標識別 paramdic = [[NSMutableDictionaryalloc] init]; [paramdicsetValue: vcNameforKey:@quot;URLKEYquot;]; SuppressPerformSelectorLeakWarning([controllerperformSelector: selectorwithObject:paramdic]); } else { [paramdicsetValue: vcNameforKey:@quot;URLKEYquot;]; } SuppressPerformSelectorLeakWarning( [controllerperformSelector:selectorwithObject:paramdic]); return controller; } -(UIViewController *)getViewController:(NSString *)stringVCName { NSString *viewControllerName = [PlistReadUtilplistValueForKey: stringVCName]; Class class = NSClassFromString(viewControllerName); UIViewController *controller = [[class alloc] init]; if(controller == nil){//此處可以跳轉到一個錯誤提示頁面 NSLog(@quot;未定義此類:%@quot;,viewControllerName); return nil; } return controller; } -(UIViewController *)getViewController:(NSString *)stringVCNamewithParam:(NSDictionary *)paramdic { UIViewController *controller = [self getViewController: stringVCName]; if(controller != nil){ controller = [self controller: controllerwithParam:paramdicandVcname:stringVCName]; } else { //異常處理可以跳轉指定的錯誤頁面 controller = [[RouterErrorsharedInstance] getErrorController]; } return controller; } @end
說明:通過反射機制根據傳入的string來獲取 viewcontroller實例,實現了兩個方法,一個是不需要傳入參數的,一個是需要傳入參數的,當跳轉到第二個頁面需要傳值 就使用第二個帶參數的方法,所傳的值通過NSDictionary來進行封裝,跳轉后的頁面通過實現
-(void)iniViewControllerParam:(NSDictionary *)dic
方法來獲取傳過來的參數。
#import @interface PlistReadUtil : NSObject @property(nonatomic,strong) NSMutableDictionary *plistdata; (id)sharedInstanceWithFileName:(NSString *)plistfileName; (NSString *)plistValueForKey:(NSString *)key; @end
#import quot;PlistReadUtil.hquot; @implementationPlistReadUtil (id)sharedInstanceWithFileName:(NSString *)plistfileName { static dispatch_once_tonceToken; static PlistReadUtil * plistUtil; dispatch_once(amp;onceToken,^{ plistUtil = [[PlistReadUtilalloc] init]; NSString *plistPath = [[NSBundlemainBundle] pathForResource: plistfileNameofType:@quot;plistquot;]; plistUtil.plistdata = http://www.tuicool.com/articles/[[NSMutableDictionaryalloc] initWithContentsOfFile: plistPath]; }); return plistUtil; } (NSString *)plistValueForKey:(NSString *)key { PlistReadUtil *plist =[PlistReadUtilsharedInstanceWithFileName: @quot;urlMapquot;]; return [plist.plistdataobjectForKey: key]; } @end
說明:路由配置文件讀取工具類,我這里讀取的是plist 文件,我這里也可以讀取json,或則訪問網絡獲取后臺服務器上的路由配置文件,這個根據我們業務需求的不同,可以添加不同的讀取方法。
#import #import @interface RouterError : NSObject (id)sharedInstance; -(UIViewController *)getErrorController; @end
#import quot;RouterError.hquot; #import quot;WXRouter.hquot; @implementationRouterError (id)sharedInstance { static dispatch_once_tonceToken; static RouterError * routerError; dispatch_once(amp;onceToken,^{ routerError = [[RouterErroralloc] init]; }); return routerError; } #pragma mark自定義錯誤頁面 此頁面一定確保能夠找到,否則會進入死循環 -(UIViewController *)getErrorController { NSDictionary *diction = [[NSMutableDictionaryalloc] init]; [dictionsetValue: @quot;https://themeforest.net/item/octopus-error-template/2562783quot; forKey:@quot;urlquot;]; UIViewController *errorController = [[WXRoutersharedInstance] getViewController: @quot;MSG003quot; withParam:diction]; return errorController; } @end
說明:在讀取配置文件時如果沒有讀到相應的路徑,或者未定義相應的class,我們可以在這里處理,我這邊處理的是如果出現錯誤,就返回一個webview頁面,我們可以在項目里寫一個統一的錯誤處理webview頁面,其實每個頁面默認都添加了一個參數 [paramdic setValue:vcName forKey:@quot;URLKEYquot;];
就是這個URLKEY,這個key標示配置文件中每個跳轉動作的key,這個key是唯一的,我們可以根據不同的URLKEY然后通過后臺統一的一個接口來判斷跳轉到不同的錯誤處理H5頁面。
#import quot;ViewController.hquot; #import quot;view2.hquot; #import quot;WXRouter.hquot; #import quot;PlistReadUtil.hquot; @interface ViewController () @end @implementationViewController -(void)viewDidLoad { [super viewDidLoad]; UILabel *lable = [[UILabelalloc] initWithFrame: CGRectMake(0, 0, 100, 50)]; lable.textColor = [UIColorblueColor]; lable.text =@quot;hello wordquot;; [self.viewaddSubview: lable]; UIButton *button = [[UIButtonalloc] initWithFrame: CGRectMake(0, 50, 200, 50)]; [buttonsetTitle: @quot;訪問view1quot; forState:UIControlStateNormal]; [buttonsetTitleColor: [UIColorblackColor] forState:UIControlStateNormal]; button.tag = 1; [buttonaddTarget: self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; [self.viewaddSubview: button]; UIButton *button2 = [[UIButtonalloc] initWithFrame: CGRectMake(0, 110, 200, 50)]; [button2setTitle: @quot;訪問view3quot; forState:UIControlStateNormal]; [button2setTitleColor: [UIColorblackColor] forState:UIControlStateNormal]; button2.tag = 2; [button2addTarget: self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; [self.viewaddSubview: button2]; UIButton *butto3 = [[UIButtonalloc] initWithFrame: CGRectMake(0, 170, 200, 50)]; [butto3setTitle: @quot;訪問webviewquot; forState:UIControlStateNormal]; [butto3setTitleColor: [UIColorblackColor] forState:UIControlStateNormal]; butto3.tag = 3; [butto3addTarget: self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; [self.viewaddSubview: butto3]; UIButton *button4 = [[UIButtonalloc] initWithFrame: CGRectMake(0, 230, 200, 50)]; [button4setTitle: @quot;訪問wei定義的頁面quot; forState:UIControlStateNormal]; [button4setTitleColor: [UIColorblackColor] forState:UIControlStateNormal]; button4.tag = 4; [button4addTarget: self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside]; [self.viewaddSubview: button4]; } -(void)back:(UIButton *)btn { switch (btn.tag) { case 1: { NSMutableDictionary *dic = [[NSMutableDictionaryalloc] init]; [dicsetValue: @quot;nihao shijiequot; forKey:@quot;titlequot;]; UIViewController *controller = [[WXRoutersharedInstance] getViewController: @quot;MSG001quot; withParam:dic]; [self presentViewController: controlleranimated:YEScompletion:nil]; } break; case 2: { NSMutableDictionary *dic = [[NSMutableDictionaryalloc] init]; [dicsetValue: @quot;nihao shijiequot; forKey:@quot;titlequot;]; UIViewController *controller = [[WXRoutersharedInstance] getViewController: @quot;MSG002quot;withParam:dic]; [self presentViewController: controlleranimated:YEScompletion:nil]; } break; case 3: { NSMutableDictionary *dic = [[NSMutableDictionaryalloc] init]; [dicsetValue: @quot;https://www.baidu.comquot; forKey:@quot;urlquot;]; UIViewController *controller = [[WXRoutersharedInstance] getViewController: @quot;MSG003quot; withParam:dic]; [self presentViewController: controlleranimated:YEScompletion:nil]; } break; case 4: { UIViewController *controller = [[WXRoutersharedInstance] getViewController: @quot;MSG005quot;withParam:nil]; [self presentViewController: controlleranimated:YEScompletion:nil]; } default: break; } } -(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
說明:這個是使用示例,為了獲取最大的靈活性,這里我并沒有把跳轉動作presentViewcontroller,pushViewController,以及參數的組裝封裝在路由管理類里。看過很多大神寫的路由庫,有些也通過url schema的方式。類似于:xml:id/123/name/xu,這樣的路徑方式,但是個人感覺,如果界面之間傳遞圖片對象,或者傳嵌套的類對象,就有點麻煩了。因為怕麻煩,所以就先寫個簡單的吧。
#import quot;view3.hquot; @interface view3 () @end @implementationview3 - (void)viewDidLoad { [super viewDidLoad]; UILabel *lable = [[UILabelalloc] initWithFrame: CGRectMake(0, 0, 100, 50)]; lable.textColor = [UIColorblueColor]; lable.text =@quot;我是view3quot;; [self.viewaddSubview: lable]; UIButton *button = [[UIButtonalloc] initWithFrame: CGRectMake(200, 200, 200, 200)]; [buttonsetTitle: @quot;backquot; forState:UIControlStateNormal]; [buttonsetTitleColor: [UIColorblackColor] forState:UIControlStateNormal]; [buttonaddTarget: self action:@selector(back) forControlEvents:UIControlEventTouchUpInside]; [self.viewaddSubview: button]; } -(void) back { [self dismissViewControllerAnimated: YEScompletion:nil]; } -(void)iniViewControllerParam:(NSDictionary *)dic { self.title = [dicobjectForKey: @quot;titlequot;]; }
說明:這個是要跳轉的頁面我們可以通過 iniViewControllerParam:(NSDictionary *)dic
方法獲取上一個界面傳過來的參數。
urlMap.plist
說明:路由配置文件,key:value的形式,頁面里的每個跳轉動作都會對應一個唯一的key,這里如果兩個頁面都跳轉到同一個頁面,就會產生不同的key 對應相同的value,感覺是有點冗余了,如果有更好的優化,我會更新下文章的,這里的配置文件我們可以怎么玩,由于我在android的這塊的描述已經很詳細了,所以這里就不再贅述。只是android的配置有點坑,類前需要加上包名,這點就沒有iOS方便靈活了,至此iOS示例我就講完了。
下面讓我們來看下Android下的示例
Android平臺示例:
我們先看一下demo的結構
Paste_Image.png
說明:example.mixu.wxrouter 路由使用示例
wx.router 路由管理文件
assets 路由配置文件
接下來,我們主要講解一下,wx.router里面的文件,以及assets配置文件結構,還有MainActivity 文件,其他的請下載示例demo,文末我會給出demo地址。
package com.example.mlxu.wxrouter; importandroid.app.Activity; importandroid.content.Intent; importandroid.os.Bundle; importandroid.view.View; importcom.wx.router.Router; public class MainActivity extends Activity { @Override protected void onCreate(BundlesavedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.v(quot;activityquot;,quot;activity:quot; RouterUtil.getActivity(this, quot;MSG001quot;)); Log.v(quot;activityquot;, quot;activity:quot; RouterUtil.getActivity(this, quot;MSG002quot;)); Log.v(quot;activityquot;, quot;activity:quot; RouterUtil.getActivity(this, quot;MSG003quot;)); } public void buttonClick(View v) { int id = v.getId(); switch (id) { case R.id.btn1: { startActivity(Router.initIntentWithActivityKey(this, quot;MSG001quot;)); } break; case R.id.btn2: { startActivity(Router.initIntentWithActivityKey(this, quot;MSG002quot;)); } break; case R.id.btn3: { Intentintent = Router.initIntentWithActivityKey(this, quot;MSG003quot;); intent.putExtra(quot;urlquot;, quot;http://www.baidu.comquot;); startActivity(intent); } break; case R.id.btn4: { Intentintent = Router.initIntentWithActivityKey(this, quot;MSG005quot;); intent.putExtra(quot;urlquot;, quot;http://www.baidu.comquot;); startActivity(intent); } break; } } }
說明: 主要是調用 Router.initIntentWithActivityKey(Context context, String key)
方法來獲取一個intent,這個intent里面會根據你傳入的key去urlmap.json文件中查找對應的Activity,并設置好Intent要跳轉的class,獲取這個intent 后你只需要再設置一些跳轉需要傳的參數就可以了,為了提供最大的靈活性,我并沒有把跳轉參數,以及跳轉動作統一封裝在一塊,我們現在只做一個最簡單的路由跳轉,雖然簡單但我相信已經能夠實現我們絕大部分需求了,此處也參考過一些大神的路由,他們都是機遇url schema 的方式。有時還是感覺不太靈活,比如我要傳圖片對象,基與url 路徑的就不太好傳。有空再深入研究他們的實現細節吧,呵呵。
package com.wx.router; importandroid.content.Context; importandroid.content.Intent; public class Router { public static Class getActivityClassForName(Contextcontext, String name) { Class clazz; try { clazz = Class.forName(name); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } return clazz; } public static IntentinitIntentWithActivityKey(Contextcontext, String key) { Class clazz; String activityName = RouterUtil.getActivity(context, key); try { if ((activityName == quot;quot;) || (activityName == null)) { clazz = RouterError.errorClass(context); } else { clazz = Class.forName(activityName); } } catch (ClassNotFoundException e) { //throw new RuntimeException(e); clazz = RouterError.errorClass(context); } Intentintent = new Intent(context, clazz); intent.putExtra(quot;URLKEYquot;, key); //??key?????????? return intent; } }
說明:此處很簡單,就是通過把字符串路徑轉換成class對象,然后通過 Intent setClass(Context packageContext, Classgt; cls)
設置好要跳轉的 Activity.注意這行代碼 intent.putExtra(quot;URLKEYquot;, key);
這個我把每個跳轉頁面都傳入也一個參數,URLKey,就是我們配置文件(urlmap.json)中的key,這個key是唯一的,為什么設置這個參數,主要就是為了識別唯一的跳轉動作,假如我們這個頁面跳轉出錯了,我們讓它跳轉到一個統一的錯誤頁面,那么我們根據URLKEY這個參數就能知道是哪個頁面跳轉出錯,該做什么操作,我可以展示一個相應的錯誤頁面,或者跳轉到一個好的有相同功能的H5頁面。
package com.wx.router; importandroid.content.Context; importandroid.content.res.AssetManager; importandroid.util.Log; importorg.json.JSONArray; importorg.json.JSONException; importorg.json.JSONObject; importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStreamReader; importjava.util.HashMap; importjava.util.Iterator; importjava.util.Map; public class RouterUtil { private final static String fileName = quot;urlmap.jsonquot;; private static Mapmaplist; public staticvoid initMaplistOfFile(Contextcontext, String fileName) { if(maplist == null){ synchronized (RouterUtil.class){ if(maplist == null){ maplist = new HashMap(); StringBuffersb = new StringBuffer(); AssetManageram = context.getAssets(); try { BufferedReaderbf = new BufferedReader(new InputStreamReader(am.open(fileName))); String next = quot;quot;; while (null != (next = bf.readLine())){ sb.append(next); } }catch (IOException e){ e.printStackTrace(); sb.delete(0,sb.length()); } try { JSONArrayjsonArray = new JSONArray(sb.toString().trim()); for(int i = 0; ikeyIter= jsonObject.keys(); Mapmap = new HashMap(); String key; Object value; while (keyIter.hasNext()) { key = keyIter.next(); value = http://www.tuicool.com/articles/jsonObject.get(key); maplist.put(key, (String)value); } } } catch (JSONException e) { e.printStackTrace(); } } } } } public static String getActivity(Contextcontext,String urlkey){ initMaplistOfFile(context, fileName); String value = quot;quot;; try { value = maplist.get(urlkey); }catch (Exception e){ value = quot;quot;; Log.w(quot;UrlErrorquot;,urlkey quot;值不存在quot;); } return value; } }
說明:路由讀取工具類,就是讀取urlmap.json文件,并轉換成map,然后又提供一個根據key獲取value的方法,在獲取value時因為我們的配置文件也不能保證格式都是正確的,難免開發中會寫錯,我們要做一些異常捕獲,并給出一些提示,我們有可能沒配置key,也有可能沒配置value,或者都沒配置,一方配置出錯,我們都無法實例化要跳轉的Activity.
package com.wx.router; importandroid.content.Context; public class RouterError { public static Class errorClass(Contextcontext){ return Router.getActivityClassForName(context,RouterUtil.getActivity(context,quot;MSG003quot;)); } }
說明:路由錯誤類,如果配置文件中沒配置相應的antivity,我們可以引導跳轉到一個統一的錯誤頁面。
[{quot;MSG001quot;:quot;com.example.mlxu.wxrouter.View1quot;},{quot;MSG002quot;:quot;com.example.mlxu.wxrouter.View2quot;},{quot;MSG003quot;:quot;com.example.mlxu.wxrouter.webviewquot;},{quot;MSG004quot;:quot;com.example.mlxu.wxrouter.webview1quot;}]
說明:路由配置文件,是一個json文件,里面都是鍵值對 MSG001是key,后面就是相應的activity,key是唯一的,每一個頁面跳轉動作都對應一個Key,當然了這邊也有一個問題,當兩個頁面都跳轉到同一個頁面時,會出現重復的value.這個有時間再想下有沒有好的解決方法, 當然了這個配置文件我們可以打包在app內,也可以從服務器上拉取,或者兩者結合,配合版本控制,我們就能夠動態指定頁面跳轉路徑,比如說MSG001對應的頁面是個支付頁面,但是突然出現了大bug,支付不了了。那么我們可以把MSG001改成一個我們統一的webview頁面,這個頁面中,我們可以讓它跳轉到我們線上臨時的H5頁面。這里面我們可以讓所有發生錯誤的頁面,都跳轉到統一的webview,然后訪問同一個后臺接口,后臺根據我們傳的參數不同,然后引導跳轉到不同的線上H5頁面。
其實這里面還有個可以改進的,我們可以優化[ {“MSG001″:”Native:com.example.mlxu.wxrouter.View1”}]
{“MSG001”:”web: http://www.baidu.com”}]
加個前綴來識別自動跳轉原生頁面還是web頁面,等等,想想的空間還很多,好了android 的demo 到這里也基本介紹完了。
總結:代碼是簡陋的,只是簡單的實現了自己的構想,還有很多值得細細琢磨的地方,關鍵是架構思路,通過中間路由根據下發的路由配置文件來動態跳轉頁面,解決原生開發的遇到的一些問題,不同的項目有不同的業務邏輯,這種思路有什么缺陷,或者解決不了什么問題,大家一起討論分享。基于這種思路搭建架子的話,對于將來的組件化開發,應該也會很方便轉換吧。
demo地址
Tags: 軟件架構 iOS開發
文章來源:http://ios.jobbole.com/92420/