diff options
Diffstat (limited to 'src/objective-c/examples/SwiftSample')
-rw-r--r-- | src/objective-c/examples/SwiftSample/Bridging-Header.h | 1 | ||||
-rw-r--r-- | src/objective-c/examples/SwiftSample/Podfile | 1 | ||||
-rw-r--r-- | src/objective-c/examples/SwiftSample/ViewController.swift | 42 |
3 files changed, 34 insertions, 10 deletions
diff --git a/src/objective-c/examples/SwiftSample/Bridging-Header.h b/src/objective-c/examples/SwiftSample/Bridging-Header.h index 33db2dd1cb..65f768a760 100644 --- a/src/objective-c/examples/SwiftSample/Bridging-Header.h +++ b/src/objective-c/examples/SwiftSample/Bridging-Header.h @@ -39,6 +39,7 @@ #import <RxLibrary/GRXWriter+Immediate.h> #import <GRPCClient/GRPCCall.h> #import <ProtoRPC/ProtoMethod.h> +#import <ProtoRPC/ProtoRPC.h> #import <RemoteTest/Test.pbrpc.h> #endif diff --git a/src/objective-c/examples/SwiftSample/Podfile b/src/objective-c/examples/SwiftSample/Podfile index 7b5941eef7..3611b00863 100644 --- a/src/objective-c/examples/SwiftSample/Podfile +++ b/src/objective-c/examples/SwiftSample/Podfile @@ -1,6 +1,7 @@ source 'https://github.com/CocoaPods/Specs.git' platform :ios, '8.0' +pod 'Protobuf', :path => "../../../../third_party/protobuf" pod 'gRPC', :path => "../../../.." pod 'RemoteTest', :path => "../RemoteTestClient" diff --git a/src/objective-c/examples/SwiftSample/ViewController.swift b/src/objective-c/examples/SwiftSample/ViewController.swift index e4e7aeae49..76dad9e132 100644 --- a/src/objective-c/examples/SwiftSample/ViewController.swift +++ b/src/objective-c/examples/SwiftSample/ViewController.swift @@ -45,17 +45,37 @@ class ViewController: UIViewController { request.fillUsername = true request.fillOauthScope = true + // Example gRPC call using a generated proto client library: let service = RMTTestService(host: RemoteHost) - service.unaryCallWithRequest(request) { (response: RMTSimpleResponse?, error: NSError?) in + service.unaryCallWithRequest(request) { response, error in + if let response = response { + NSLog("1. Finished successfully with response:\n\(response)") + } else { + NSLog("1. Finished with error: \(error!)") + } + } + + + // Same but manipulating headers: + + var RPC : ProtoRPC! // Needed to convince Swift to capture by reference (__block) + RPC = service.RPCToUnaryCallWithRequest(request) { response, error in if let response = response { - NSLog("Finished successfully with response:\n\(response)") + NSLog("2. Finished successfully with response:\n\(response)") } else { - NSLog("Finished with error: \(error!)") + NSLog("2. Finished with error: \(error!)") } + NSLog("2. Response headers: \(RPC.responseHeaders)") + NSLog("2. Response trailers: \(RPC.responseTrailers)") } + RPC.requestHeaders["My-Header"] = "My value" + + RPC.start() + + // Same example call using the generic gRPC client library: let method = ProtoMethod(package: "grpc.testing", service: "TestService", method: "UnaryCall") @@ -64,14 +84,16 @@ class ViewController: UIViewController { let call = GRPCCall(host: RemoteHost, path: method.HTTPPath, requestsWriter: requestsWriter) - let responsesWriteable = GRXWriteable { (value: AnyObject?, error: NSError?) in - if let value = value as? NSData { - NSLog("Received response:\n\(RMTSimpleResponse(data: value, error: nil))") + call.requestHeaders["My-Header"] = "My value" + + call.startWithWriteable(GRXWriteable { response, error in + if let response = response as? NSData { + NSLog("3. Received response:\n\(RMTSimpleResponse(data: response, error: nil))") } else { - NSLog("Finished with error: \(error!)") + NSLog("3. Finished with error: \(error!)") } - } - - call.startWithWriteable(responsesWriteable) + NSLog("3. Response headers: \(call.responseHeaders)") + NSLog("3. Response trailers: \(call.responseTrailers)") + }) } } |