From cdf292687a9a91264955ee52b8ee15e4b3f91652 Mon Sep 17 00:00:00 2001 From: Alex Maimescu Date: Tue, 16 Feb 2016 23:00:29 +0000 Subject: [PATCH] Add possibility to call a method with given params on AppDelegate instance. --- .swiftlint.yml | 10 ++++++++ Insider.xcodeproj/project.pbxproj | 10 ++++---- Insider/Insider.h | 3 ++- Insider/Insider.swift | 38 +++++++++++++++++++++++++++---- InsiderDemo/AppDelegate.m | 5 ++++ 5 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 .swiftlint.yml diff --git a/.swiftlint.yml b/.swiftlint.yml new file mode 100644 index 0000000..f83cc0a --- /dev/null +++ b/.swiftlint.yml @@ -0,0 +1,10 @@ +disabled_rules: + - trailing_whitespace + - trailing_newline + - line_length + - type_name + +excluded: + - Carthage + - Pods + - Libs diff --git a/Insider.xcodeproj/project.pbxproj b/Insider.xcodeproj/project.pbxproj index 82921e1..d39fe4c 100644 --- a/Insider.xcodeproj/project.pbxproj +++ b/Insider.xcodeproj/project.pbxproj @@ -34,13 +34,13 @@ CD3305BC1C737BB000494BB9 /* GCDWebServerRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CD33059E1C737BB000494BB9 /* GCDWebServerRequest.m */; }; CD3305BD1C737BB000494BB9 /* GCDWebServerResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = CD33059F1C737BB000494BB9 /* GCDWebServerResponse.h */; settings = {ATTRIBUTES = (Public, ); }; }; CD3305BE1C737BB000494BB9 /* GCDWebServerResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305A01C737BB000494BB9 /* GCDWebServerResponse.m */; }; - CD3305BF1C737BB000494BB9 /* GCDWebServerDataRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305A21C737BB000494BB9 /* GCDWebServerDataRequest.h */; }; + CD3305BF1C737BB000494BB9 /* GCDWebServerDataRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305A21C737BB000494BB9 /* GCDWebServerDataRequest.h */; settings = {ATTRIBUTES = (Public, ); }; }; CD3305C01C737BB000494BB9 /* GCDWebServerDataRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305A31C737BB000494BB9 /* GCDWebServerDataRequest.m */; }; CD3305C11C737BB000494BB9 /* GCDWebServerFileRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305A41C737BB000494BB9 /* GCDWebServerFileRequest.h */; }; CD3305C21C737BB000494BB9 /* GCDWebServerFileRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305A51C737BB000494BB9 /* GCDWebServerFileRequest.m */; }; CD3305C31C737BB000494BB9 /* GCDWebServerMultiPartFormRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305A61C737BB000494BB9 /* GCDWebServerMultiPartFormRequest.h */; }; CD3305C41C737BB000494BB9 /* GCDWebServerMultiPartFormRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305A71C737BB000494BB9 /* GCDWebServerMultiPartFormRequest.m */; }; - CD3305C51C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305A81C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.h */; }; + CD3305C51C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305A81C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.h */; settings = {ATTRIBUTES = (Public, ); }; }; CD3305C61C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305A91C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.m */; }; CD3305C71C737BB000494BB9 /* GCDWebServerDataResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305AB1C737BB000494BB9 /* GCDWebServerDataResponse.h */; }; CD3305C81C737BB000494BB9 /* GCDWebServerDataResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305AC1C737BB000494BB9 /* GCDWebServerDataResponse.m */; }; @@ -291,18 +291,18 @@ CD33058B1C737B3800494BB9 /* Insider.h in Headers */, CD3305B31C737BB000494BB9 /* GCDWebServer.h in Headers */, CD3305BD1C737BB000494BB9 /* GCDWebServerResponse.h in Headers */, + CD3305C51C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.h in Headers */, + CD3305BF1C737BB000494BB9 /* GCDWebServerDataRequest.h in Headers */, CD3305BB1C737BB000494BB9 /* GCDWebServerRequest.h in Headers */, CD3305C11C737BB000494BB9 /* GCDWebServerFileRequest.h in Headers */, CD3305C71C737BB000494BB9 /* GCDWebServerDataResponse.h in Headers */, CD3305B71C737BB000494BB9 /* GCDWebServerFunctions.h in Headers */, - CD3305BF1C737BB000494BB9 /* GCDWebServerDataRequest.h in Headers */, CD3305B51C737BB000494BB9 /* GCDWebServerConnection.h in Headers */, CD3305BA1C737BB000494BB9 /* GCDWebServerPrivate.h in Headers */, CD3305B91C737BB000494BB9 /* GCDWebServerHTTPStatusCodes.h in Headers */, CD3305CB1C737BB000494BB9 /* GCDWebServerFileResponse.h in Headers */, CD3305C31C737BB000494BB9 /* GCDWebServerMultiPartFormRequest.h in Headers */, CD3305CD1C737BB000494BB9 /* GCDWebServerStreamedResponse.h in Headers */, - CD3305C51C737BB000494BB9 /* GCDWebServerURLEncodedFormRequest.h in Headers */, CD3305C91C737BB000494BB9 /* GCDWebServerErrorResponse.h in Headers */, ); runOnlyForDeploymentPostprocessing = 0; @@ -684,6 +684,7 @@ CD3305F21C738D6B00494BB9 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; CD3305F71C738E6800494BB9 /* Build configuration list for PBXAggregateTarget "SwiftLint" */ = { isa = XCConfigurationList; @@ -692,6 +693,7 @@ CD3305F91C738E6800494BB9 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/Insider/Insider.h b/Insider/Insider.h index b1067fb..f526886 100644 --- a/Insider/Insider.h +++ b/Insider/Insider.h @@ -14,4 +14,5 @@ FOUNDATION_EXPORT double InsiderVersionNumber; //! Project version string for Insider. FOUNDATION_EXPORT const unsigned char InsiderVersionString[]; -#import "GCDWebServer.h" \ No newline at end of file +#import "GCDWebServer.h" +#import "GCDWebServerURLEncodedFormRequest.h" \ No newline at end of file diff --git a/Insider/Insider.swift b/Insider/Insider.swift index e5badb2..7bc25cf 100644 --- a/Insider/Insider.swift +++ b/Insider/Insider.swift @@ -7,28 +7,56 @@ // import Foundation +import UIKit @objc final public class Insider: NSObject { - + struct Constants { static let defaultPort: UInt = 8080 + static let defaultInsiderMethodSelector = Selector("insider:") } public static let sharedInstance = Insider() - private lazy var localWebServer: GCDWebServer = { - let server = GCDWebServer() - return server; - }() + public var appDelegateInsiderSelector: Selector? + + private let localWebServer = GCDWebServer() private override init() {} + private func addHandlersForServer(server: GCDWebServer, withAppDelegateInsiderSelector selector: Selector) { + + // Add POST handler for x-www-form-urlencoded requests + server.addDefaultHandlerForMethod("POST", requestClass: GCDWebServerURLEncodedFormRequest.self, processBlock: { request in + + if let request = request as? GCDWebServerURLEncodedFormRequest { + if let json = request.jsonObject { + self.processRequestParams(json, selector: selector) + } else if let params = request.arguments { + self.processRequestParams(params, selector: selector) + } + } + + return nil + }) + } + + private func processRequestParams(params: AnyObject, selector: Selector) { + dispatch_sync(dispatch_get_main_queue()) { () -> Void in + UIApplication.sharedApplication().delegate?.performSelector(selector, withObject: params) + } + } + public func start() { startWithPort(Constants.defaultPort) } public func startWithPort(port: UInt) { + addHandlersForServer(localWebServer, + withAppDelegateInsiderSelector: appDelegateInsiderSelector ?? Constants.defaultInsiderMethodSelector + ) + localWebServer.startWithPort(port, bonjourName: nil) } diff --git a/InsiderDemo/AppDelegate.m b/InsiderDemo/AppDelegate.m index 8f315e1..a5d96c1 100644 --- a/InsiderDemo/AppDelegate.m +++ b/InsiderDemo/AppDelegate.m @@ -42,4 +42,9 @@ - (void)applicationWillTerminate:(UIApplication *)application { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } +- (void)insider:(id)object +{ + NSLog(@"Insider object: %@", object); +} + @end