diff --git a/examples/objective-c/helloworld/main.m b/examples/objective-c/helloworld/main.m index b67380812b4..755dce33df9 100644 --- a/examples/objective-c/helloworld/main.m +++ b/examples/objective-c/helloworld/main.m @@ -43,7 +43,7 @@ static NSString * const kHostAddress = @"localhost:50051"; int main(int argc, char * argv[]) { @autoreleasepool { [GRPCCall useInsecureConnectionsForHost:kHostAddress]; - [GRPCCall setUserAgentPrefix:@"HelloWorld/1.0"]; + [GRPCCall setUserAgentPrefix:@"HelloWorld/1.0" forHost:kHostAddress]; HLWGreeter *client = [[HLWGreeter alloc] initWithHost:kHostAddress]; diff --git a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h index ec1557df7fa..bd6b064f166 100644 --- a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h +++ b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.h @@ -38,10 +38,9 @@ @interface GRPCCall (ChannelArg) /** - * Use the provided @c userAgentPrefix at the beginning of the HTTP User Agent string for all calls. + * Use the provided @c userAgentPrefix at the beginning of the HTTP User Agent string for all calls + * to the specified @c host. */ -+ (void)setUserAgentPrefix:(NSString *)userAgentPrefix; - -+ (NSString *)userAgentPrefix; ++ (void)setUserAgentPrefix:(NSString *)userAgentPrefix forHost:(NSString *)host; @end diff --git a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m index 32244c3d313..f2821f6abc0 100644 --- a/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m +++ b/src/objective-c/GRPCClient/GRPCCall+ChannelArg.m @@ -39,16 +39,13 @@ static NSString *_userAgentPrefix; -+ (void)setUserAgentPrefix:(NSString *)userAgentPrefix { - @synchronized(self) { - _userAgentPrefix = userAgentPrefix; - } -} - -+ (NSString *)userAgentPrefix { - @synchronized(self) { - return _userAgentPrefix; ++ (void)setUserAgentPrefix:(NSString *)userAgentPrefix forHost:(NSString *)host { + if (!host) { + [NSException raise:NSInvalidArgumentException + format:@"host and userAgentPrefix must be provided."]; } + GRPCHost *hostConfig = [GRPCHost hostWithAddress:host]; + hostConfig.userAgentPrefix = userAgentPrefix; } @end diff --git a/src/objective-c/GRPCClient/private/GRPCHost.h b/src/objective-c/GRPCClient/private/GRPCHost.h index 6b4f98746d0..69a115e88cf 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.h +++ b/src/objective-c/GRPCClient/private/GRPCHost.h @@ -39,6 +39,7 @@ struct grpc_call; @interface GRPCHost : NSObject @property(nonatomic, readonly) NSString *address; +@property(nonatomic, copy) NSString *userAgentPrefix; /** The following properties should only be modified for testing: */ diff --git a/src/objective-c/GRPCClient/private/GRPCHost.m b/src/objective-c/GRPCClient/private/GRPCHost.m index 8fb9d917227..5b1ca16e120 100644 --- a/src/objective-c/GRPCClient/private/GRPCHost.m +++ b/src/objective-c/GRPCClient/private/GRPCHost.m @@ -109,9 +109,8 @@ if (!_channel) { NSMutableDictionary *args = [NSMutableDictionary dictionary]; - NSString *userAgentPrefix = [[GRPCCall userAgentPrefix] copy]; - if (userAgentPrefix) { - args[@GRPC_ARG_PRIMARY_USER_AGENT_STRING] = userAgentPrefix; + if (_userAgentPrefix) { + args[@GRPC_ARG_PRIMARY_USER_AGENT_STRING] = _userAgentPrefix; } if (_secure) {