GithubHelp home page GithubHelp logo

runt18 / urlnavigator Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ryanmaxwell/urlnavigator

0.0 2.0 0.0 50 KB

⛵️ Elegant URL Routing for Swift

License: MIT License

Swift 96.22% Objective-C 2.68% Ruby 1.10%

urlnavigator's Introduction

URLNavigator

Swift Build Status CocoaPods Carthage compatible

⛵️ URLNavigator provides an elegant way to navigate through view controllers by URLs. URL patterns can be mapped by using URLNavigator.map(_:_:) function.

URLNavigator can be used for mapping URL patterns with 2 kind of types: URLNavigable and URLOpenHandler. URLNavigable is a type which defines an custom initializer and URLOpenHandler is a closure which can be executed. Both an initializer and a closure receive an URL and placeholder values.

At a Glance

Mapping URL Patterns

URL patterns can contain placeholders. Placeholders will be replaced with matching values from URLs. Use < and > to make placeholders. Placeholders can have types: string(default), int, float, and path.

Here's an example of mapping URL patterns with view controllers and a closure. View controllers should conform a protocol URLNavigable to be mapped wisth URL patterns. See Implementing URLNavigable section for details.

Navigator.map("myapp://user/<int:id>", UserViewController.self)
Navigator.map("myapp://post/<title>", PostViewController.self)

Navigator.map("myapp://alert") { URL, values in
    print(URL.queryParameters["title"])
    print(URL.queryParameters["message"])
    return true
}

Note: Global constant Navigator is a shortcut for URLNavigator.defaultNavigator().

Pushing, Presenting and Opening URLs

URLNavigator can push and present view controllers and execute closures with URLs.

Provide the from parameter to pushURL() to specify the navigation controller which the new view controller will be pushed. Similarly, provide the from parameter to presentURL() to specify the view controller which the new view controller will be presented. If the nil is passed, which is a default value, current application's top most view controller will be used to push or present view controllers.

presentURL() takes an extra parameter: wrap. If true is specified, the new view controller will be wrapped with a UINavigationController. Default value is false.

Navigator.pushURL("myapp://user/123")
Navigator.presentURL("myapp://post/54321", wrap: true)

Navigator.openURL("myapp://alert?title=Hello&message=World")

For full documentation, see URLNavigator Class Reference.

Implementing URLNavigable

View controllers should conform a protocol URLNavigable to be mapped with URLs. A protocol URLNavigable defines an failable initializer with parameter: URL and values.

Parameter URL is an URL that is passed from URLNavigator.pushURL() and URLNavigator.presentURL(). Parameter values is a dictionary that contains URL placeholder keys and values.

final class UserViewController: UIViewController, URLNavigable {

    init(userID: Int) {
        super.init(nibName: nil, bundle: nil)
        // Initialize here...
    }

    convenience init?(URL: URLConvertible, values: [String : AnyObject]) {
        // Let's assume that the user id is required
        guard let userID = values["id"] as? Int else {
            return nil
        }
        self.init(userID: userID)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

Note: URLConvertible is a protocol that NSURL and String conforms.

Installation

  • For iOS 8+ projects with CocoaPods:

    pod 'URLNavigator', '~> 0.6'
  • For iOS 8+ projects with Carthage:

    github "devxoul/URLNavigator" ~> 0.6
    
  • For iOS 7 projects with CocoaSeeds:

    github 'devxoul/URLNavigator', '0.6.0', :files => 'Sources/*.swift'
  • Using Swift Package Manager:

    import PackageDescription
    
    let package = Package(
        name: "MyAwesomeApp",
        dependencies: [
            .Package(url: "https://github.com/devxoul/URLNavigator", "0.6.0"),
        ]
    )

Tips and Tricks

Where to Map URLs

I'd prefer using separated URL map file.

struct URLNavigationMap {

    static func initialize() {
        Navigator.map("myapp://user/<int:id>", UserViewController.self)
        Navigator.map("myapp://post/<title>", PostViewController.self)

        Navigator.map("myapp://alert") { URL, values in
            print(URL.parameters["title"])
            print(URL.parameters["message"])
            self.someUtilityMethod()
            return true
        }
    }

    private static func someUtilityMethod() {
        print("This method is really useful")
    }

}

Then call initialize() at AppDelegate's application:didFinishLaunchingWithOptions:.

@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Navigator
        URLNavigationMap.initialize()
        
        // Do something else...
    }
}

Implementing AppDelegate Launch Option URL

It's available to open your app with URLs if custom schemes are registered. In order to navigate to view controllers with URLs, you'll have to implement application:didFinishLaunchingWithOptions: method.

func application(application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // ...

    if let URL = launchOptions?[UIApplicationLaunchOptionsURLKey] as? NSURL {
        self.window?.rootViewController = Navigator.viewControllerForURL(URL)
    }
    return true
}

Implementing AppDelegate Open URL Method

You'll might want to implement custom URL open handler. Here's an example of using URLNavigator with other URL open handlers.

func application(application: UIApplication,
                 openURL url: NSURL,
                 sourceApplication: String?,
                 annotation: AnyObject) -> Bool {
    // If you're using Facebook SDK
    let fb = FBSDKApplicationDelegate.sharedInstance()
    if fb.application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation) {
        return true
    }

    // URLNavigator Handler
    if Navigator.openURL(url) {
        return true
    }

    // URLNavigator View Controller
    if Navigator.presentURL(url, wrap: true) != nil {
        return true
    }

    return false
}

Using with Storyboard

It's not yet available to initialize view controllers from Storyboard. However, you can map the closures alternatively.

Navigator.map("myapp://post/<int:id>") { URL, values in
    guard let postID = values["id"] as? Int,
          let postViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
          else { return false }
    Navigator.push(postViewController)
    return true
}

Then use Navigator.openURL() instead of Navigator.pushURL():

Navigator.openURL("myapp://post/12345")

Setting Default Scheme

Set scheme property on URLNavigator instance to get rid of schemes in every URLs.

Navigator.scheme = "myapp"
Navigator.map("/user/<int:id>", UserViewController.self)
Navigator.push("/user/10")

This is totally equivalent to:

Navigator.map("myapp://user/<int:id>", UserViewController.self)
Navigator.push("myapp://user/10")

Setting scheme property will not affect other URLs that already have schemes.

Navigator.scheme = "myapp"
Navigator.map("/user/<int:id>", UserViewController.self) // `myapp://user/<int:id>`
Navigator.map("http://<path>", MyWebViewController.self) // `http://<path>`

License

URLNavigator is under MIT license. See the LICENSE file for more info.

urlnavigator's People

Contributors

devxoul avatar

Watchers

 avatar  avatar

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.