Comments:"Routable | Propeller Blog"
URL:http://usepropeller.com/blog/posts/routable
Find Routable on Github for Android and for iOS
Long ago, there was an iOS library known as Three20. Among Three20's many contributions was the TTNavigator, which allowed apps to be organized using URL-based routes. It was a pretty good idea, but got lost in the shuffle as Three20 fell by the wayside. Well, until now.
Routable is a set of libraries for both Android and iOS inspired by Three20's URL navigator. Here's how it works:
1. Define your routes.
-(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions{// ...[[RoutablesharedRouter]map:@"user/:id"toController:[UserControllerclass]];[[RoutablesharedRouter]map:@"messages"toController:[MessagesControllerclass]];}
publicclassPropellerApplicationextendsApplication{@OverridepublicvoidonCreate(){// ...Router.sharedRouter().map("users/:id",UserActivity.class);Router.sharedRouter().map("messsages",MessagesActivity.class);}}
2. Implement the required Routable protocol in either your UIViewController
or Activity
subclass:
@implementationUserController// This is a required method-(id)initWithRouterParams:(NSDictionary*)params{if((self=[selfinitWithNibName:nilbundle:nil])){self.userId=[paramsobjectForKey:@"id"];}returnself;}@end
publicclassUserActivityextendsActivity{@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);// Parameters are passed in the extrasBundleintentExtras=getIntent().getExtras();StringuserId=intentExtras.get("id");}}
3. Open a route, anywhere in your app!
[[RoutablesharedRouter]open:@"users/16"];
Router.sharedRouter().open("users/16");
Why?
The benefit of structuring your app like this, as opposed to a loose collection of UIViewController
and Activity
objects, is that you can open any "screen" from any point in your code without wrangling Intent
s or controller allocations:
-(void)messagesTapped:(id)sender{[[RoutablesharedRouter]open:@"messages"];}
aView.setOnClickListener(newView.OnClickListener(){publicvoidonClick(Viewv){Router.sharedRouter().open("messages");}});
Or, imagine you're receiving push notifications from Apple or Google. Instead of hacking together a pseudo-protocol to determine what to show when a user opens a notification, just pass the appropriate Routable
URL:
-(void)application:(UIApplication*)applicationdidReceiveRemoteNotification:(NSDictionary*)userInfo{NSString*url=[userInfoobjectForKey:@"url"];// => @"messages/123123"[[RoutablesharedRouter]open:url];}
publicclassGCMIntentServiceextendsGCMBaseIntentService{@OverrideprotectedvoidonMessage(Contextcontext,Intentintent){Bundleextras=intent.getExtras();Stringroute=extras.getString("url");// => "messages/123123"Router.sharedRouter().open(route);}}
What else?
Routable has some other neat features, most notably the ability to route anonymous callbacks to URLs:
[[RoutablesharedRouter]map:@"logout"toCallback:^(NSDictionary*params){[Userlogout];}];
Router.sharedRouter().map("logout",newRouter.RouterCallback(){publicvoidrun(Map<String,String>extras){User.logout();}});
Contribute
We'd love to hear how you're using Routable and find out what else you think it can do. Don't hesitate to send pull-requests to the Android or iOS repos!