From 1bb872c28ac2cffdcd16ce9f75b589c63f472687 Mon Sep 17 00:00:00 2001 From: lincode Date: Sun, 16 Oct 2016 16:09:43 +0800 Subject: [PATCH 1/2] FRDURLRoutes add regiter(plistFile:) method --- FRDIntent.xcodeproj/project.pbxproj | 4 +++ .../Source/Intent/FRDControllerManager.swift | 5 ++- FRDIntent/Source/URLRoutes/FRDURLRoutes.swift | 30 ++++++++++++++++ FRDIntentDemo/AppDelegate.m | 17 +++++++--- FRDIntentDemo/FRDURLRoutesRegisters.plist | 10 ++++++ FRDIntentDemo/MainViewController.m | 4 ++- README.md | 34 ++++++++++++++----- 7 files changed, 88 insertions(+), 16 deletions(-) create mode 100644 FRDIntentDemo/FRDURLRoutesRegisters.plist diff --git a/FRDIntent.xcodeproj/project.pbxproj b/FRDIntent.xcodeproj/project.pbxproj index 63a8403..6a7384f 100644 --- a/FRDIntent.xcodeproj/project.pbxproj +++ b/FRDIntent.xcodeproj/project.pbxproj @@ -33,6 +33,7 @@ A36FCC2A1D87F96700059523 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A36FCC281D87F96700059523 /* LaunchScreen.storyboard */; }; A382FE421D951C96007099DE /* FRDRouteParameters.swift in Sources */ = {isa = PBXBuildFile; fileRef = A382FE411D951C96007099DE /* FRDRouteParameters.swift */; }; A384541D1DA807B100835B70 /* FRDIntentRegisters.plist in Resources */ = {isa = PBXBuildFile; fileRef = A384541C1DA807B100835B70 /* FRDIntentRegisters.plist */; }; + A3EBF8B31DB35D0100C7ED83 /* FRDURLRoutesRegisters.plist in Resources */ = {isa = PBXBuildFile; fileRef = A3EBF8B21DB35D0100C7ED83 /* FRDURLRoutesRegisters.plist */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -101,6 +102,7 @@ A36FCC2B1D87F96700059523 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; A382FE411D951C96007099DE /* FRDRouteParameters.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FRDRouteParameters.swift; sourceTree = ""; }; A384541C1DA807B100835B70 /* FRDIntentRegisters.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = FRDIntentRegisters.plist; sourceTree = ""; }; + A3EBF8B21DB35D0100C7ED83 /* FRDURLRoutesRegisters.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = FRDURLRoutesRegisters.plist; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -226,6 +228,7 @@ A35919521D87FFB100EBFD1E /* ThirdViewController.h */, A35919531D87FFB100EBFD1E /* ThirdViewController.m */, A384541C1DA807B100835B70 /* FRDIntentRegisters.plist */, + A3EBF8B21DB35D0100C7ED83 /* FRDURLRoutesRegisters.plist */, A36FCC261D87F96700059523 /* Assets.xcassets */, A36FCC281D87F96700059523 /* LaunchScreen.storyboard */, A36FCC2B1D87F96700059523 /* Info.plist */, @@ -375,6 +378,7 @@ files = ( A384541D1DA807B100835B70 /* FRDIntentRegisters.plist in Resources */, A36FCC2A1D87F96700059523 /* LaunchScreen.storyboard in Resources */, + A3EBF8B31DB35D0100C7ED83 /* FRDURLRoutesRegisters.plist in Resources */, A36FCC271D87F96700059523 /* Assets.xcassets in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/FRDIntent/Source/Intent/FRDControllerManager.swift b/FRDIntent/Source/Intent/FRDControllerManager.swift index 324500e..400416f 100644 --- a/FRDIntent/Source/Intent/FRDControllerManager.swift +++ b/FRDIntent/Source/Intent/FRDControllerManager.swift @@ -24,7 +24,7 @@ public class FRDControllerManager: NSObject { - parameter url: The url to be registered. - parameter clazz: The clazz to be registered, and the clazz's view controller object will be launched while routed. - - returns: True if register success. + - returns: True if it registers successfully. */ public func register(url: URL, clazz: AnyClass) -> Bool { return routeManager.register(url: url, clazz: clazz as! FRDIntentReceivable.Type) @@ -35,7 +35,7 @@ public class FRDControllerManager: NSObject { - parameter plistFile: The plistFile path. - - returns: True if register success. + - returns: True if it registers successfully. */ public func registers(plistFile: String) -> Bool { @@ -134,7 +134,6 @@ public class FRDControllerManager: NSObject { if let destinationController = destination as? UIViewController { display.displayViewController(source: source, destination: destinationController) } - } } diff --git a/FRDIntent/Source/URLRoutes/FRDURLRoutes.swift b/FRDIntent/Source/URLRoutes/FRDURLRoutes.swift index 0c6979c..4ac8b66 100644 --- a/FRDIntent/Source/URLRoutes/FRDURLRoutes.swift +++ b/FRDIntent/Source/URLRoutes/FRDURLRoutes.swift @@ -74,6 +74,36 @@ public extension FRDURLRoutes { return (resultForRoute && resultForIntent) } + /** + Registers with a plist file. + + - parameter plistFile: The plistFile path. + + - returns: True if it registers successfully. + */ + public func registers(plistFile: String) -> Bool { + + guard let registers: NSDictionary = NSDictionary(contentsOfFile: plistFile) else { + return false + } + + for (url, className) in registers { + + guard let url = url as? String, let className = className as? String else { + return false + } + + if let clazz = NSClassFromString(className) as? FRDIntentReceivable.Type { + let result = register(url: URL(string: url)!, clazz: clazz) + if !result { + return false + } + } + + } + return true + } + } private extension UIApplication { diff --git a/FRDIntentDemo/AppDelegate.m b/FRDIntentDemo/AppDelegate.m index 8966a5a..87fb220 100644 --- a/FRDIntentDemo/AppDelegate.m +++ b/FRDIntentDemo/AppDelegate.m @@ -105,15 +105,18 @@ - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDiction - (void)configurationRoutes { - // Internal call + // Internal call register //[[FRDControllerManager sharedInstance] registerWithUrl: [NSURL URLWithString: @"/user/:userId"] clazz: [FirstViewController class]]; //[[FRDControllerManager sharedInstance] registerWithUrl: [NSURL URLWithString: @"/story/:storyId"] clazz: [SecondViewController class]]; //[[FRDControllerManager sharedInstance] registerWithUrl: [NSURL URLWithString: @"/user/:userId/story/:storyId"] clazz: [ThirdViewController class]]; + // Internal call register by plist NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"FRDIntentRegisters" ofType:@"plist"]; [[FRDControllerManager sharedInstance] registersWithPlistFile:plistPath]; - // External call + + + // External call register [[FRDURLRoutes sharedInstance] registerWithUrl:[NSURL URLWithString:@"/user/:userId/story/:storyId"] handler:^(NSDictionary *params) { @@ -126,8 +129,14 @@ - (void)configurationRoutes { } }]; - [[FRDURLRoutes sharedInstance] registerWithUrl:[NSURL URLWithString: @"/story/:storyId"] - clazz:[SecondViewController self]]; + + // External call register + //[[FRDURLRoutes sharedInstance] registerWithUrl:[NSURL URLWithString: @"/story/:storyId"] + // clazz:[SecondViewController self]]; + + // External call register by plist + NSString* routesPlistPath = [[NSBundle mainBundle] pathForResource:@"FRDURLRoutesRegisters" ofType:@"plist"]; + [[FRDURLRoutes sharedInstance] registersWithPlistFile:routesPlistPath]; } @end diff --git a/FRDIntentDemo/FRDURLRoutesRegisters.plist b/FRDIntentDemo/FRDURLRoutesRegisters.plist new file mode 100644 index 0000000..58201c9 --- /dev/null +++ b/FRDIntentDemo/FRDURLRoutesRegisters.plist @@ -0,0 +1,10 @@ + + + + + /user/:userId + FirstViewController + /story/:storyId + SecondViewController + + diff --git a/FRDIntentDemo/MainViewController.m b/FRDIntentDemo/MainViewController.m index 73d8c46..7bb84cf 100644 --- a/FRDIntentDemo/MainViewController.m +++ b/FRDIntentDemo/MainViewController.m @@ -75,7 +75,9 @@ - (void)gotoThirdViewController #pragma mark - FRDIntentForResultSendable -- (void)onControllerResultWithRequestCode:(NSInteger)requestCode resultCode:(enum FRDResultCode)code data:(FRDIntent *)intent +- (void)onControllerResultWithRequestCode:(NSInteger)requestCode + resultCode:(enum FRDResultCode)code + data:(FRDIntent *)intent { if (requestCode == 1){ if (code == FRDResultCodeOk) { diff --git a/README.md b/README.md index 9ff7f24..60c0e2f 100644 --- a/README.md +++ b/README.md @@ -65,11 +65,21 @@ FRDIntent/Intent 有如下优势: #### 注册 +通过代码注册: + ```Swift let controllerManager = FRDControllerManager.sharedInstance controllerManager.register(URL(string: "/frodo/firstview")!, clazz: FirstViewController.self) ``` +通过 plist 文件批量注册: + +```Swift + let plistPath = Bundle.main.path(forResource: "FRDIntentRegisters", ofType: "plist") + let controllerManager = FRDControllerManager.sharedInstance + controllerManager.register(plistFile: plistPath) +``` + #### 通过指定类名启动 view controller ```Swift @@ -93,12 +103,12 @@ FRDIntent/Intent 有如下优势: ```Swift extension ViewController: FRDIntentForResultSendable { - func onControllerResult(_ requestCode: Int, resultCode: FRDResultCode, data: Intent) { + func onControllerResult(requestCode: Int, resultCode: FRDResultCode, data: Intent) { if (requestCode == RequestText) { - if (resultCode == .Ok) { + if (resultCode == .ok) { let text = data.extra["text"] print("Successful confirm get from destination : \(text)") - } else if (resultCode == .Canceled) { + } else if (resultCode == .canceled) { let text = data.extra["text"] print("Canceled get from destination : \(text)") } @@ -163,11 +173,19 @@ FRDIntent/URLRoutes 是为了使得 iOS 系统中这种基于 URL 的应用间 #### 注册 -注册一个 ViewControler。在第三方应用调起该 URL 时,会该启动该 view controller。该 view controller 的进入动画为 Push 横滑进入方式。 +通过代码注册一个 view controler。在第三方应用调起该 URL 时,会该启动该 view controller。该 view controller 的进入动画为 Push 横滑进入方式。 ```Swift - let router = FRDURLRoutes.sharedInstance - router.register(url: URL(string: "/story/:storyId")!, clazz: SecondViewController.self) + let routes = FRDURLRoutes.sharedInstance + routes.register(url: URL(string: "/story/:storyId")!, clazz: SecondViewController.self) +``` + +通过 plist 文件批量注册,效果和上面通过代码注册一样。注册的 view controller 进入动画都为 Push 横滑进入方式。 + +```Swift + let plistPath = Bundle.main.path(forResource: "FRDURLRoutesRegisters", ofType: "plist") + let routes = FRDURLRoutes.sharedInstance + routes.register(plistFile: plistPath) ``` 注册一个 block handler。下面例子中的 block handler 中,用注册时的 URL 构造了一个 Intent,并将该 Intent 送出。FRDControllerManager 会处理这个 Intent。看是否有合适的 view controller 可以被启动。 @@ -207,9 +225,9 @@ iOS 系统提供的通过 URL 调用另外一个应用功能本身就是使用 ## FRDIntentDemo -FRDIntentDemo 对 FRDIntent 各种使用方法都做了演示。 +FRDIntentDemo 对 FRDIntent 各种使用方法都做了演示。FRDIntentDemo 使用 Objective-C 实现,这是为了演示 FRDIntent 虽然使用 Swift 完成,但是对 Objective-C 有良好的兼容。 -对于外部调用的演示,可以在模拟器的 Safari 的地址栏中输入 `frdintent://frdintent.com/user/123`。正常情况下,访问该 URL 将会启动 FRDIntentDemo,并进入 Firstview controller。 +对于外部调用的演示,可以在模拟器的 Safari 的地址栏中输入 `frdintent://frdintent.com/user/123`。正常情况下,访问该 URL 将会启动 FRDIntentDemo,并进入 FirstViewController。 ## 单元测试 From 7259284e7fa89cb26aa4a6b0fe62e51e684e68d9 Mon Sep 17 00:00:00 2001 From: lincode Date: Sun, 16 Oct 2016 16:10:36 +0800 Subject: [PATCH 2/2] Update FRDIntent.podspec --- FRDIntent.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FRDIntent.podspec b/FRDIntent.podspec index e26a97f..3c6ebbb 100644 --- a/FRDIntent.podspec +++ b/FRDIntent.podspec @@ -1,7 +1,7 @@ Pod::Spec.new do |s| s.name = "FRDIntent" - s.version = "0.10.0" + s.version = "0.9.1" s.summary = "FRDIntent can handle the call between view controller" s.description = "FRDIntent has two components URLRoutes and Intent, using for calling view controllers inner app or outer app."