- 在 Podfile 中添加
pod 'FBRouter'
。 - 执行
pod install
或pod update
。 - 导入 import FBRouter。
public func registURLMapping(urlmappings:Dictionary<String,String>)
public static func registURLMapping(urlmappings:Dictionary<String,String>)
public static func setScheme(scheme: String)
public func setScheme(scheme: String)
public static func setWrapNavgClass(className: AnyClass)
//init
public convenience init(url:URL)
public convenience init(urlString:String)
public convenience init(host:String)
public convenience init(httpUrl:String?)
// set params
public func setInteger(_ key:String, value:NSInteger)
public func setInt(_ key:String, value:Int) }
public func setDouble(_ key:String, value:Double)
public func setString(_ key:String, value:String)
public func setBool(_ key:String, value:Bool)
public func setAny(_ key:String, value:Any)
public func addEntriesFromDictonary(entries:Dictionary<String, Any>)
public func addParamsFromURLAction(urlAction:FBURLAction)
// query param
public func integer(_ key:String) -> NSInteger?
public func int(_ key:String) -> Int?
public func double(_ key:String) -> Double?
public func string(_ key:String) -> String?
public func bool(_ key:String) -> Bool?
public func anyObject(_ key:String) -> Any?
@discardableResult
func openURLAction(_ urlAction:FBURLAction) -> UIViewController?
@discardableResult
func openHostString(host:String) -> UIViewController?
@discardableResult
func openURL(url:URL) -> UIViewController?
@discardableResult
func openURLString(urlString:String) -> UIViewController?
@discardableResult
func openHttpURLString(httpUrl:String) -> UIViewController?
@discardableResult
public func openURL(url:URL,from:UIViewController) -> UIViewController? {
return openURLAction(FBURLAction.init(url: url), from: from)
}
@discardableResult
public func openURLString(urlString:String,from:UIViewController) -> UIViewController? {
return openURLAction(FBURLAction.init(urlString: urlString), from: from)
}
@discardableResult
public func openHttpURLString(httpUrl:String,from:UIViewController) -> UIViewController? {
return openURLAction(FBURLAction.init(httpUrl: httpUrl), from: from)
}
@discardableResult
public func openHost(host:String,from:UIViewController) -> UIViewController? {
return openURLAction(FBURLAction.init(host: host),from: from)
}
@discardableResult
public func openHost(host:String) -> UIViewController? {
return openURLAction(FBURLAction.init(host: host))
}
@discardableResult
public func openURL(url:URL) -> UIViewController? {
return openURLAction(FBURLAction.init(url: url))
}
@discardableResult
public func openURLString(urlString:String) -> UIViewController? {
return openURLAction(FBURLAction.init(urlString: urlString))
}
@discardableResult
public func openHttpURLString(httpUrl:String) -> UIViewController? {
return openURLAction(FBURLAction.init(httpUrl: httpUrl))
}
@discardableResult
public static func openURLAction(_ urlAction:FBURLAction) -> UIViewController? {
return FBRouter.router().openURLAction(urlAction)
}
@discardableResult
public static func openURLAction(_ urlAction:FBURLAction,from:UIViewController?) -> UIViewController? {
return FBRouter.router().openURLAction(urlAction,from: from)
}
@discardableResult
public static func openURL(url:URL,from:UIViewController) -> UIViewController? {
return openURLAction(FBURLAction.init(url: url), from: from)
}
@discardableResult
public static func openURLString(urlString:String,from:UIViewController) -> UIViewController? {
return openURLAction(FBURLAction.init(urlString: urlString), from: from)
}
@discardableResult
public static func openHttpURLString(httpUrl:String,from:UIViewController) -> UIViewController? {
return openURLAction(FBURLAction.init(httpUrl: httpUrl), from: from)
}
@discardableResult
public static func openHost(host:String,from:UIViewController) -> UIViewController? {
return openURLAction(FBURLAction.init(host: host),from: from)
}
@discardableResult
public static func openHost(host:String) -> UIViewController? {
return openURLAction(FBURLAction.init(host: host))
}
@discardableResult
public static func openURL(url:URL) -> UIViewController? {
return openURLAction(FBURLAction.init(url: url))
}
@discardableResult
public static func openURLString(urlString:String) -> UIViewController? {
return openURLAction(FBURLAction.init(urlString: urlString))
}
@discardableResult
public static func openHttpURLString(httpUrl:String) -> UIViewController? {
return openURLAction(FBURLAction.init(httpUrl: httpUrl))
}
//将要打开链接,可以用来校验权限
func shouldOpenURLAction(_ urlAction:FBURLAction) -> Bool
//将要打开外部跳转链接,可以用来校验权限
func willOpenExternal(_ urlAction:FBURLAction) -> Bool
//已经打开外部跳转回调
func didOpenExternal(_ urlAction:FBURLAction,success:Bool)
//将要路由到某一个页面
func willOpenURLAction(_ urlAction:FBURLAction)
//未能处理的路由跳转
func onMatchUnhandledURLAction(_ urlAction:FBURLAction)
//正常的路由跳转
func onMatchViewController(_ controller:UIViewController,urlAction:FBURLAction)
//实现打开外部跳转处理方法
func openExternal(_ urlAction:FBURLAction, completionHandler completion: ((Bool) -> Void)?)
//登录判断方法回调
func handleLoginAction(_ urlAction:FBURLAction,controller:UIViewController) -> Bool
///
class AppDelegate: UIResponder, UIApplicationDelegate,FBRouterDelegate{
.....
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//delegate
FBRouter.router().deleage = self
registerURLMappings()
}
}
//注册路由方法
func registerURLMappings() {
let urlMappings = ["home":"ListViewController",
"vc00":"ViewController",
"vc01":"ViewController01",
"vc02":"ViewController02",
"vc03":"ViewController03",
"vc04":"ViewController04",
"vc05":"ViewController05",
"demo":"ViewControllerDemo"]
FBRouter.router().registURLMapping(urlmappings: urlMappings)
FBRouter.router().wrapNavgClass = BaseNavigationController.self as UINavigationController.Type
}
//FBRouterDelegate
func shouldOpenURLAction(_ urlAction: FBURLAction) -> Bool {
return true
}
func willOpenExternal(_ urlAction: FBURLAction) -> Bool {
return true
}
func didOpenExternal(_ urlAction: FBURLAction, success: Bool) {
}
func willOpenURLAction(_ urlAction: FBURLAction) {
}
func onMatchUnhandledURLAction(_ urlAction: FBURLAction) {
}
func onMatchViewController(_ controller: UIViewController, urlAction: FBURLAction) {
}
func openExternal(_ urlAction: FBURLAction, completionHandler completion: ((Bool) -> Void)?) {
UIApplication.shared.open(urlAction.url!, options: [:], completionHandler:{
(success) in
guard let complete = urlAction.completeBlock else{
return
}
complete(success)
})
}
func handleLoginAction(_ urlAction: FBURLAction, controller: UIViewController) -> Bool {
return true
}
let urlMappings = ["home":"ListViewController",
"vc00":"ViewController",
"vc01":"ViewController01",
"vc02":"ViewController02",
"vc03":"ViewController03",
"vc04":"ViewController04",
"vc05":"ViewController05",
"demo":"ViewControllerDemo"]
FBRouter.router().registURLMapping(urlmappings: urlMappings)
FBRouter.router().wrapNavgClass = BaseNavigationController.self as UINavigationController.Type
//use FBURLAction transform data
let urlAction = FBURLAction.init(host: "vc01?a=b")
urlAction.completeBlock = {
(succes) in
print("succes:",urlAction.url!)
}
urlAction.isSingleton = true
urlAction.setBool("Bool", value: true)
urlAction.setString("String", value: "String")
urlAction.setDouble("Double", value: 2.0202)
urlAction.setInteger("Integer", value: 101)
let person:Person = Person.init()
person.age = 18
person.name = "nini"
urlAction.setAny("person", value: person)
FBRouter.router().openURLAction(urlAction,from: self)
-----------------
//use UIViewController Extension
let urlAction = FBURLAction.init(host: "vc02?a=b")
urlAction.completeBlock = {
(succes) in
print("succes:",urlAction.url!)
}
openURLAction(urlAction)
swift 5.0
该项目最低支持 iOS 10.0
和 Xcode 11.0
。
FBRouter 使用 MIT 许可证,详情见 LICENSE 文件。