Setting User-Agent in AFNetworking
Asked Answered
C

4

9

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

Corenecoreopsis answered 6/4, 2015 at 18:29 Comment(0)
P
6

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"

Peshawar answered 10/6, 2015 at 10:30 Comment(0)
N
6

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

Naivete answered 22/7, 2015 at 12:37 Comment(0)
I
3

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.

Imf answered 6/4, 2015 at 18:33 Comment(6)
The setValue function is called directly after initialising the Framework. This has to be in the release build because otherwise there wouldn't be any request at all. So there's no possibility of the setValue function not being compiled i thinkCorenecoreopsis
Hard to say, then, without more context.Tidewater
Which parts of the code would help to further investigate the problem?Corenecoreopsis
@MarcoButz All of the code from app launch until that line gets called (of course obfuscating anything like access tokens)Imf
Another possibility is that you have multiple request serializers and you set the user-agent on the wrong one.Imf
i had exactly that problemMishear
C
0

gbk's 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")
    }
Cackle answered 19/3, 2018 at 7:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.