@@ -69,7 +69,7 @@ public class Manager {
6969
7070 let action = String ( path. characters. dropFirst ( ) ) // remove /
7171
72- let parameters = url. query? . parseURLParams ?? [ : ]
72+ let parameters = url. query? . toQueryDictionary ?? [ : ]
7373 let actionParameters = Manager . actionParameters ( parameters)
7474
7575 // is a reponse?
@@ -93,31 +93,20 @@ public class Manager {
9393 return false
9494 }
9595 else if let actionHandler = actions [ action] { // handle the action
96- let successCallback = { ( returnParams: Parameters ? ) in
97- if let urlString = parameters [ kXCUSuccess] , url = NSURL ( string: urlString) {
98- // returnParams
99- let comp = NSURLComponents ( URL: url, resolvingAgainstBaseURL: false ) !
100- if let query = returnParams? . query {
96+ let successCallback : SuccessCallback = { [ weak self] returnParams in
97+ self ? . openCallback ( parameters, type: . success) { comp in
98+ if let query = returnParams? . queryString {
10199 comp &= query
102100 }
103- if let newURL = comp. URL {
104- Manager . openURL ( newURL)
105- }
106101 }
107102 }
108- let failureCallback = { ( error: FailureCallbackErrorType ) in
109- if let urlString = parameters [ kXCUError] , url = NSURL ( string: urlString) {
110- let comp = NSURLComponents ( URL: url, resolvingAgainstBaseURL: false ) !
103+ let failureCallback : FailureCallback = { [ weak self] error in
104+ self ? . openCallback ( parameters, type: . error) { comp in
111105 comp &= error. XCUErrorQuery
112- if let newURL = comp. URL {
113- Manager . openURL ( newURL)
114- }
115106 }
116107 }
117- let cancelCallback = {
118- if let urlString = parameters [ kXCUCancel] , url = NSURL ( string: urlString) {
119- Manager . openURL ( url)
120- }
108+ let cancelCallback : CancelCallback = { [ weak self] in
109+ self ? . openCallback ( parameters, type: . cancel)
121110 }
122111
123112 actionHandler ( actionParameters, successCallback, failureCallback, cancelCallback)
@@ -138,6 +127,16 @@ public class Manager {
138127 }
139128 return false
140129 }
130+
131+ private func openCallback( parameters: [ String : String] , type: ResponseType, handler: ( ( NSURLComponents) - > Void) ? = nil ) {
132+ if let urlString = parameters [ type. key] , url = NSURL ( string: urlString) ,
133+ comp = NSURLComponents ( URL: url, resolvingAgainstBaseURL: false ) {
134+ handler ? ( comp)
135+ if let newURL = comp. URL {
136+ Manager . openURL ( newURL)
137+ }
138+ }
139+ }
141140
142141 // Handle url with manager shared instance
143142 public static func handleOpenURL( url: NSURL) - > Bool {
@@ -183,6 +182,7 @@ public class Manager {
183182
184183 // MARK: internal
185184
185+
186186 func sendRequest( request: Request) throws {
187187 if !request. client. appInstalled {
188188 throw CallbackURLKitError . AppWithSchemeNotInstalled ( scheme: request. client. URLScheme)
@@ -199,17 +199,12 @@ public class Manager {
199199 xcuComponents. path = " / " + kResponse
200200
201201 let xcuParams : Parameters = [ kRequestID: request. ID]
202-
203- if request. successCallback != nil {
204- xcuComponents. query = ( xcuParams + [ kResponseType: ResponseType . success. rawValue] ) . query
205- query [ kXCUSuccess] = xcuComponents. URL? . absoluteString ?? " "
206-
207- xcuComponents. query = ( xcuParams + [ kResponseType: ResponseType . cancel. rawValue] ) . query
208- query [ kXCUCancel] = xcuComponents. URL? . absoluteString ?? " "
209- }
210- if request. failureCallback != nil {
211- xcuComponents. query = ( xcuParams + [ kResponseType: ResponseType . error. rawValue] ) . query
212- query [ kXCUError] = xcuComponents. URL? . absoluteString ?? " "
202+
203+ for reponseType in request. responseTypes {
204+ xcuComponents. queryDictionary = xcuParams + [ kResponseType: reponseType. rawValue]
205+ if let urlString = xcuComponents. URL? . absoluteString {
206+ query [ reponseType. key] = urlString
207+ }
213208 }
214209
215210 if request. hasCallback {
@@ -248,7 +243,7 @@ public class Manager {
248243 }
249244
250245 static var appName: String {
251- if let appName = NSBundle . mainBundle ( ) . localizedInfoDictionary ? [ " CFBundleDisplayName " ] as? String {
246+ if let appName = NSBundle . mainBundle ( ) . localizedInfoDictionary ? [ " CFBundleDisplayName " ] as? String {
252247 return appName
253248 }
254249 return NSBundle . mainBundle ( ) . infoDictionary ? [ " CFBundleDisplayName " ] as? String ?? " CallbackURLKit "
@@ -261,11 +256,10 @@ public class Manager {
261256extension Manager {
262257
263258 public func registerToURLEvent( ) {
264- NSAppleEventManager . sharedAppleEventManager ( ) . setEventHandler ( self , andSelector: " handleGetURLEvent:withReplyEvent: " , forEventClass: AEEventClass ( kInternetEventClass) , andEventID: AEEventID ( kAEGetURL) )
259+ NSAppleEventManager . sharedAppleEventManager ( ) . setEventHandler ( self , andSelector: #selector ( Manager . handleURLEvent ( _ : withReply : ) ) , forEventClass: AEEventClass ( kInternetEventClass) , andEventID: AEEventID ( kAEGetURL) )
265260 }
266261
267- public func handleGetURLEvent( event: NSAppleEventDescriptor ! , withReplyEvent replyEvent: NSAppleEventDescriptor ! )
268- {
262+ @objc public func handleURLEvent( event: NSAppleEventDescriptor , withReply replyEvent: NSAppleEventDescriptor ) {
269263 if let urlString = event. paramDescriptorForKeyword ( AEKeyword ( keyDirectObject) ) ? . stringValue, url = NSURL ( string: urlString) {
270264 handleOpenURL ( url)
271265 }
0 commit comments