diff --git a/Insider.xcodeproj/project.pbxproj b/Insider.xcodeproj/project.pbxproj index d39fe4c..a507c4b 100644 --- a/Insider.xcodeproj/project.pbxproj +++ b/Insider.xcodeproj/project.pbxproj @@ -42,7 +42,7 @@ CD3305C41C737BB000494BB9 /* GCDWebServerMultiPartFormRequest.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305A71C737BB000494BB9 /* GCDWebServerMultiPartFormRequest.m */; }; 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 */; }; + CD3305C71C737BB000494BB9 /* GCDWebServerDataResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305AB1C737BB000494BB9 /* GCDWebServerDataResponse.h */; settings = {ATTRIBUTES = (Public, ); }; }; CD3305C81C737BB000494BB9 /* GCDWebServerDataResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305AC1C737BB000494BB9 /* GCDWebServerDataResponse.m */; }; CD3305C91C737BB000494BB9 /* GCDWebServerErrorResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = CD3305AD1C737BB000494BB9 /* GCDWebServerErrorResponse.h */; }; CD3305CA1C737BB000494BB9 /* GCDWebServerErrorResponse.m in Sources */ = {isa = PBXBuildFile; fileRef = CD3305AE1C737BB000494BB9 /* GCDWebServerErrorResponse.m */; }; @@ -294,8 +294,8 @@ 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 */, + CD3305C11C737BB000494BB9 /* GCDWebServerFileRequest.h in Headers */, CD3305B71C737BB000494BB9 /* GCDWebServerFunctions.h in Headers */, CD3305B51C737BB000494BB9 /* GCDWebServerConnection.h in Headers */, CD3305BA1C737BB000494BB9 /* GCDWebServerPrivate.h in Headers */, diff --git a/Insider/Insider.h b/Insider/Insider.h index f526886..42188b7 100644 --- a/Insider/Insider.h +++ b/Insider/Insider.h @@ -15,4 +15,5 @@ FOUNDATION_EXPORT double InsiderVersionNumber; FOUNDATION_EXPORT const unsigned char InsiderVersionString[]; #import "GCDWebServer.h" -#import "GCDWebServerURLEncodedFormRequest.h" \ No newline at end of file +#import "GCDWebServerURLEncodedFormRequest.h" +#import "GCDWebServerDataResponse.h" \ No newline at end of file diff --git a/Insider/Insider.swift b/Insider/Insider.swift index 7bc25cf..20f8997 100644 --- a/Insider/Insider.swift +++ b/Insider/Insider.swift @@ -12,6 +12,11 @@ import UIKit @objc final public class Insider: NSObject { + enum StatusCodes: Int { + case Success = 200 + case NotFound = 404 + } + struct Constants { static let defaultPort: UInt = 8080 static let defaultInsiderMethodSelector = Selector("insider:") @@ -19,7 +24,9 @@ final public class Insider: NSObject { public static let sharedInstance = Insider() - public var appDelegateInsiderSelector: Selector? + public lazy var appDelegateInsiderSelector: Selector = { + return Constants.defaultInsiderMethodSelector + }() private let localWebServer = GCDWebServer() @@ -30,33 +37,43 @@ final public class Insider: NSObject { // Add POST handler for x-www-form-urlencoded requests server.addDefaultHandlerForMethod("POST", requestClass: GCDWebServerURLEncodedFormRequest.self, processBlock: { request in + var didProcessParams = false 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) + didProcessParams = self.processRequestParams(json, selector: selector) + } else if let encodedParams = request.arguments { + didProcessParams = self.processRequestParams(encodedParams, selector: selector) } } - return nil + return GCDWebServerDataResponse(statusCode: (didProcessParams) ? StatusCodes.Success.rawValue : StatusCodes.NotFound.rawValue); }) } - private func processRequestParams(params: AnyObject, selector: Selector) { + private func canProcessRequestParams() -> Bool { + return UIApplication.sharedApplication().delegate?.respondsToSelector(appDelegateInsiderSelector) ?? false + } + + private func processRequestParams(params: AnyObject, selector: Selector) -> Bool { + guard canProcessRequestParams() else { + return false + } + dispatch_sync(dispatch_get_main_queue()) { () -> Void in UIApplication.sharedApplication().delegate?.performSelector(selector, withObject: params) } + + return true } + // MARK - Public methods + public func start() { startWithPort(Constants.defaultPort) } public func startWithPort(port: UInt) { - addHandlersForServer(localWebServer, - withAppDelegateInsiderSelector: appDelegateInsiderSelector ?? Constants.defaultInsiderMethodSelector - ) - + addHandlersForServer(localWebServer, withAppDelegateInsiderSelector: appDelegateInsiderSelector) localWebServer.startWithPort(port, bonjourName: nil) }