Git Product home page Git Product logo

routable-ios's Introduction

Routable Build Status FOSSA Status

Routable is an in-app native URL router, for iOS. Also available for Android.

Usage

Set up your app's router and URLs (usually done in application:didFinishLaunchingWithOptions:):

[[Routable sharedRouter] map:@"users/:id" toController:[UserController class]];
// Requires an instance of UINavigationController to open UIViewControllers
[[Routable sharedRouter] setNavigationController:aNavigationController];

Implement initWithRouterParams: in your UIViewController subclass:

@implementation UserController

// params will be non-nil
- (id)initWithRouterParams:(NSDictionary *)params {
  if ((self = [self initWithNibName:nil bundle:nil])) {
    self.userId = [params objectForKey:@"id"];
  }
  return self;
}

Then, anywhere else in your app, open a URL:

NSString *aUrl = @"users/4";
[[Routable sharedRouter] open:aUrl];

If you wish to do custom allocation of a controller, you can use +allocWithRouterParams:

[[Routable sharedRouter] map:@"users/:id" toController:[StoryboardController class]];

@implementation StoryboardController

+ (id)allocWithRouterParams:(NSDictionary *)params {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
    StoryboardController *instance = [storyboard instantiateViewControllerWithIdentifier:@"sbController"];
    instance.userId = [params objectForKey:@"id"];

    return instance;
}

Set ignoresExceptions to YES to NOT throw exceptions (suggested for a Release/Distribution version)

[[Routable sharedRouter] setIgnoresExceptions:YES];

Installation

pod 'Routable', '~> 0.1.1'
#import <Routable/Routable.h>

If you're not able to use CocoaPods, please install Routable as a git submodule and add the files to your Xcode project.

Features

Anonymous Callbacks

You can invoke anonymous callbacks with Routable:

[[Routable sharedRouter] map:@"invalidate/:id" toCallback:^(NSDictionary *params) {
  [Cache invalidate: [params objectForKey:@"id"]]];
}];

[[Routable sharedRouter] open:@"invalidate/5h1b2bs"];

Presentation Options

You can configure if and how a UIViewController is presented modally with UPRouterOptions:

UPRouterOptions *options = [[UPRouterOptions modal] withPresentationStyle: UIModalPresentationFormSheet];
[self.router map:@"info" toController:[InfoController class]
                          withOptions:options];

UPRouterOptions has the following DSL setters:

  • modal
  • withPresentationStyle:
  • withTransitionStyle:
  • forDefaultParams:

Open External URLs

Sometimes you want to open a URL outside of your app, like a YouTube URL or open a web URL in the browser. You can use Routable to do that:

[[Routable sharedRouter] openExternal:@"http://www.youtube.com/watch?v=oHg5SJYRHA0"];

Multiple Routers

If you need to use multiple routers, simply create new instances of Router:

UPRouter *adminRouter = [Routable newRouter];
[adminRouter map:@"profile" toController: [AdminProfile class]];

UPRouter *userRouter = [Routable newRouter];
[userRouter map:@"profile" toController: [UserProfile class]];

Contact

Clay Allsopp (http://clayallsopp.com)

License

Routable for iOS is available under the MIT license. See the LICENSE file for more info.

FOSSA Status

routable-ios's People

Contributors

aelam avatar angelodipaolo avatar clayallsopp avatar croath avatar dlackty avatar fleitz avatar jercik avatar northstar avatar skyebook avatar sweetverbs avatar xizhao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

routable-ios's Issues

Multiple mappings do not consider each other

I am trying to map multiple URLs to the same controller but the router just uses the first one it finds. Consider opening the following URL in the example below: "example/new/test".

[[Routable sharedRouter] map:@"example/new/:param1" toController:[Example class]];
[[Routable sharedRouter] map:@"example/:param1/:param2" toController:[Example class]];

What I would expect to happen is my controller will be initialized with the first mapping and param1 being set to "test" but it actually opens the second mapping with param1 being set to "new" and param2 being set to "test". I believe this is a caching issue and it doesn't consider all scenarios of the mappings.

Any advice? Thanks!

How to use in Swift

  • (id)initWithRouterParams:(NSDictionary *)params {
    if ((self = [self initWithNibName:nil bundle:nil])) {
    self.title = @"Modal";
    }
    return self;
    }

The above code in the Swift how to write

test_basicRouteWithEmptyComponents fails on iOS 8

All tests pass fine when run in iOS 7, but in iOS 8 the above tests fails on the assert

XCTAssertTrue([USER_ID isEqualToString:@" "], @"Should have an empty ID");

It looks like USER_ID is coming through as an empty string. I tried poking around, but couldn't see why this would be happening.

UIKit

UIKit isn't imported in the routable header, which makes it uncompilable when the files are dragged into your project. Is there any reason not to import?

Architecture question ...

Hey Clay,

Nice stuff, this is cool code. Thanks for making it available!

How would you handle thinks like "routing exceptions"? But that I don't mean thrown exceptions, but rather something "show the /users/:id mapped view controller in a modal EXCEPT if some condition holds, then show it in some other way". For example, if something in a nav controller already showing in a modal asks to show the users controller in a modal, maybe the users controller should just be pushed on the stack of the nav controller inside the existing modal, instead of dismissing or presenting another modal ...

Thoughts?

I'd be happy to help with a PR, but I'm just curious as to your thoughts on this because this is such a nicely-architected piece of code.

Thanks.

How use it with a storyboard?

Is possible to use it with a storyboard?

When using a route with params:

- (id)initWithRouterParams:(NSDictionary *)params {

    UIStoryboard *b = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    if ((self = [self initWithNibName:nil bundle:nil])) {
        self.params = params;
    }
    return self;
}

if use instantiateViewControllerWithIdentifier it gets a crash in

controller = [controller performSelector:CONTROLLER_SELECTOR withObject:[params getControllerParams]];

Would Accent Correction be Considered?

There are several reasons for using idiomatic modern Objective-C in Routable, including improved readability and easier maintenance. So I propose that you consider a pull request I am about to send by the end of today.

Many of my concerns are style-related even though the caveats in usage are small. While the changed Routable-iOS obscures the parallelism between its Android counterparts, using alloc to allocate memory and init or custom init is preferred over 'new' or a custom 'new' in modern Objective-C, for instance.

@implementation Routable

static Routable *_sharedRouter = nil;

+ (Routable *)sharedRouter {
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedRouter = [[Routable alloc] init];
    });
    return _sharedRouter;
}

This shows what I'd like to use.

I understand that the current implementation might be more helpful to many. I am in no way a master at this but I think more Objective-C programmers think like me; I can merely propose this change to you with no attached authority.

(Meanwhile, here is my personal motivation: I have changed my fork greatly. It would be hard for me to continue using the library's future updates if the library does not change its style.)

Empty path components are ignored due to use of url.pathComponents

It seems the change in 56c1c0d to use url.pathComponents to split a url into parameters has the unexpected side effect of removing empty components. I'm not sure if empty components are supposed to be supported, but I had been using the library that way. The pathComponents documentation states "Empty components (caused by consecutive path separators) are deleted." (https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/pathComponents)

So if I have a route "test/:param1/:param2" and try to go to url "test//foo", in previous versions, I'd get param1 = "" and param2 = "foo". Now I get a "Route not found" exception because the code thinks I passed in a route with only 2 components.

how to do when using UITabBarController

I'm using UITabBarController,each tabbar item is UINavigationController,so I have 4 tabbar items and 4 UINavigationControllers, how can i setNavigationController?

routes should have the option to open as the root view controller

This is a feature I could use and was wondering if it makes sense to add?

Something like

[self.navigationController setViewControllers:@[controller] animated:YES];

I have a slide-out menu and I would like to open view controllers as the root of my navigation controller instead of pushing new controllers on to the stack whenever a menu item is selected.

open should handle NSURL type

Hi, I am trying to use Routable to handle external schema based links. For example, someone clicks a link to myapp://recipies/42. I am currently handling this in my app delegate by implementing application:handleOpenUrl like this:

-(BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
    NSString *path = [url.absoluteString substringFromIndex:NSMaxRange([url.absoluteString rangeOfString:@"://"])];
    [[Routable sharedRouter] open:[path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    return YES;
}

It would be awesome, if i could simply hand off the NSURL to the router and have it do the right thing.

Example App has essential buttons hidden under nav bar

In the view controllers in the example app:

UIButton *modal = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  [modal setTitle:@"Modal" forState:UIControlStateNormal];
  [modal addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
  [modal sizeToFit];

This makes the frame right under nav bar, and therefore not seen or usable.

LLVM signedness warning

If -Weverything is added to the LLVM flags, a warning will be shown for signed integers being used for unsigned values.

Undeclared selector warnings

0.0.4 generates the following Undeclared selector warnings:

Undeclared selector 'controllerWithRouterParams:' in Routable.m on line 300
Undeclared selector 'controllerWithRouterParams:' in Routable.m on line 301
Undeclared selector 'initWithRouterParams:' in Routable.m on line 305
Undeclared selector 'initWithRouterParams:' in Routable.m on line 306
Undeclared selector 'controllerWithRouterParams:' in Routable.m on line 316
Undeclared selector 'initWithRouterParams:' in Routable.m on line 316

Perhaps make these optional protocol methods for the implementing class?

2 different routers go the same controller

I have a controller that is shared by several routers:

UPRouter *router = [Routable sharedRouter];
UPRouter *routerEdit = [self editRouter];

UPRouterOptions *order = [self paramOrder:@"O"];
UPRouterOptions *invoice = [self paramOrder:@"I"];

[router map:@"invoices" toController:[OrderListViewController class] withOptions:invoice];
[routerEdit map:@"invoices/:id" toController:[POSViewController class] withOptions:invoice];
[router map:@"orders" toController:[OrderListViewController class] withOptions:order];
[routerEdit map:@"orders/:id" toController:[POSViewController class] withOptions:order];

In this order if I go to "invoices/0" the POSViewController is called, but If I go to "invoices" also go to POSViewController!. If I reverse the order:

[routerEdit map:@"invoices/:id" toController:[POSViewController class] 
[router map:@"invoices" toController:[OrderListViewController class] withOptions:invoice];

Then both go to OrderListViewController.

PD: I try with 2 routers (router & routerEdit) but happend equal with only one.

User can pass in extra parameters when opening an url

An extra parameter could be call back or any data. It looks something like:

- (void)open:(NSString *)url 
    extraDefaultParams:(NSDictionary *)extraDefaultParams
    animated:(BOOL)animated;

Use case:

//.... in a viewController.m
   [self updateUserInfo];
   [[Routable sharedRouter] open:@"base/settings" 
                    extraDefaultParams:@{@"newTitle": self.userTitle}
                                     animated: YES];

I will try to be more creative in the sample app.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.