Installing User-Agent in AFNetworking

our iOS application was recently rejected by Apple because it could not establish a valid connection to our api server. We use a specially formatted user agent to register a device token and so on. If the user agent does not fit into our sheme, api blocks the request.

All this very well tested the application on a simulator, as well as on a real device. The user agent was configured correctly and worked with the api.

As Apple tested the application, they rejected it because the application could not connect to the api. When we checked the server log files, we noticed that each request sent by Apple testers had a completely different user agent than the one we installed in the code.

(correct) user agent that was installed when testing the application:

AppName-App/010002 iOS/8.1.2 on Apple iPhone/DeviceToken

(invalid) user agent as it appeared in our logs:

AppName/1.0.0.2 (iPad; iOS 8.1.3; Scale/2.00)

The application uses AFNetworking and installs its user agent as follows:

ConnectionManager.requestSerializer.setValue(IOSREQUESTHEADER, forHTTPHeaderField: "user-agent")

Do you have an idea why this does not work when Apple tests the application while it is completely fine when we do?

Best wishes

+7
ios swift afnetworking afnetworking-2
source share
4 answers

I tried changing my user agent with code below

[self.requestSerializer setValue:@"VAL" forHTTPHeaderField:@"user-agent"];

but has no effect so far rename @"user-agent" to @"user-agent"

+6
source share

Try the following:

 NSString *userAgent = [self.manager.requestSerializer valueForHTTPHeaderField:@"User-Agent"]; userAgent = [userAgent stringByAppendingPathComponent:@"CUSTOM_PART"]; [self.manager.requestSerializer setValue:userAgent forHTTPHeaderField:@"User-Agent"]; 

it will save the generated AFNetworking data and add it to your part

+5
source share

The user agent that appears in your logs is the default user agent AFNetworking. This means that your code that sets the header has not been called.

Perhaps your code compiles in the release build? This can happen if you have something like #ifdef DEBUG or NSAssert() in the associated logical path.

If this is not enough, send an additional code.

+2
source share

gbk solution in Swift 4+ syntax

 if var userAgent = self.manager.requestSerializer.value(forHTTPHeaderField: "User-Agent") { userAgent = userAgent.stringByAppendingPathComponent(path: "CUSTOM_PART") self.manager.requestSerializer.setValue(userAgent, forHTTPHeaderField: "User-Agent") } 
0
source share

All Articles