Skip to content

Commit

Permalink
Add possibility to call a method with given params on AppDelegate ins…
Browse files Browse the repository at this point in the history
…tance.
  • Loading branch information
alexmx committed Feb 16, 2016
1 parent 6c8284a commit cdf2926
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 10 deletions.
10 changes: 10 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
disabled_rules:
- trailing_whitespace
- trailing_newline
- line_length
- type_name

excluded:
- Carthage
- Pods
- Libs
10 changes: 6 additions & 4 deletions Insider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -684,6 +684,7 @@
CD3305F21C738D6B00494BB9 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
CD3305F71C738E6800494BB9 /* Build configuration list for PBXAggregateTarget "SwiftLint" */ = {
isa = XCConfigurationList;
Expand All @@ -692,6 +693,7 @@
CD3305F91C738E6800494BB9 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
3 changes: 2 additions & 1 deletion Insider/Insider.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ FOUNDATION_EXPORT double InsiderVersionNumber;
//! Project version string for Insider.
FOUNDATION_EXPORT const unsigned char InsiderVersionString[];

#import "GCDWebServer.h"
#import "GCDWebServer.h"
#import "GCDWebServerURLEncodedFormRequest.h"
38 changes: 33 additions & 5 deletions Insider/Insider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
5 changes: 5 additions & 0 deletions InsiderDemo/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit cdf2926

Please sign in to comment.