Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting User-Agent in AFNetworking

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

It all worked pretty well testing the app on the simulator as well as on a real device. The user-agent was set correctly and the api calls worked.

As Apple tested the app, they rejected it because the app was unable to connect to the api. As we checked the server logfiles, we noticed, that every request sent out by the Apple testers had a completely different user-agent, than the one we set in the code.

The (correct) user-agent, that was set as we tested the app:

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

The (incorrect) user-agent, as it appeared in our logs:

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

The app uses AFNetworking and sets its user-agent as follows:

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

Do you have any idea why this is not working as Apple tests the app, while its completely okay when we do?

Best regards

like image 249
Mbeezy Avatar asked Apr 06 '15 18:04

Mbeezy


3 Answers

I tried to change my user agent with code below

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

but has no effect until rename @"user-agent" to @"User-Agent"

like image 118
nail Avatar answered Oct 22 '22 06:10

nail


Try to do next:

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

this will save generated byAFNetworking data and aslo append it by your part

like image 37
hbk Avatar answered Oct 22 '22 04:10

hbk


The user agent that appears in your logs is AFNetworking's default user-agent. This means your code that sets the header wasn't called.

Perhaps your code gets compiled out in a release build? This could happen if you have something like #ifdef DEBUG or NSAssert() in a related logic path.

If this isn't enough information, please post additional code.

like image 37
Aaron Brager Avatar answered Oct 22 '22 06:10

Aaron Brager