aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/objective-c
diff options
context:
space:
mode:
authorGravatar Jorge Canizales <jcanizales@google.com>2015-05-30 16:49:34 -0700
committerGravatar Jorge Canizales <jcanizales@google.com>2015-05-30 16:49:34 -0700
commitf1c368c0680f22667b25f46b3555ab5ce581a809 (patch)
tree087ad904eb09c595eeb89e9d19c56d9848ead5ae /src/objective-c
parent2c31a56d4a2af7177e5aaa2f3f40f5f6db8f570f (diff)
Fix problem loading certs for tests of library projects
In library projects (cf. app projects) the main bundle is nil. `NSBundle+bundleForClass:` works in both types of projects. Also makes the library load the certificates only once.
Diffstat (limited to 'src/objective-c')
-rw-r--r--src/objective-c/GRPCClient/private/GRPCSecureChannel.m18
1 files changed, 11 insertions, 7 deletions
diff --git a/src/objective-c/GRPCClient/private/GRPCSecureChannel.m b/src/objective-c/GRPCClient/private/GRPCSecureChannel.m
index 47bdfe3f28..2cbc6e0f83 100644
--- a/src/objective-c/GRPCClient/private/GRPCSecureChannel.m
+++ b/src/objective-c/GRPCClient/private/GRPCSecureChannel.m
@@ -38,13 +38,17 @@
@implementation GRPCSecureChannel
- (instancetype)initWithHost:(NSString *)host {
- // TODO(jcanizales): Load certs only once.
- NSURL *certsURL = [[NSBundle mainBundle] URLForResource:@"gRPC.bundle/roots" withExtension:@"pem"];
- NSData *certsData = [NSData dataWithContentsOfURL:certsURL];
- NSString *certsString = [[NSString alloc] initWithData:certsData encoding:NSUTF8StringEncoding];
-
- grpc_credentials *credentials = grpc_ssl_credentials_create(certsString.UTF8String, NULL);
- return (self = [super initWithChannel:grpc_secure_channel_create(credentials,
+ static const grpc_credentials *kCredentials;
+ static dispatch_once_t loading;
+ dispatch_once(&loading, ^{
+ // Do not use NSBundle.mainBundle, as it's nil for tests of library projects.
+ NSBundle *bundle = [NSBundle bundleForClass:self.class];
+ NSString *certsPath = [bundle pathForResource:@"gRPC.bundle/roots" ofType:@"pem"];
+ NSData *certsData = [NSData dataWithContentsOfFile:certsPath];
+ NSString *certsString = [[NSString alloc] initWithData:certsData encoding:NSUTF8StringEncoding];
+ kCredentials = grpc_ssl_credentials_create(certsString.UTF8String, NULL);
+ });
+ return (self = [super initWithChannel:grpc_secure_channel_create(kCredentials,
host.UTF8String,
NULL)]);
}