[ios][alohalytics] Use browser User-Agent for statistics.

This commit is contained in:
Alex Zolotarev 2015-05-26 14:41:03 +03:00
parent 9d19ff9158
commit 962140040c
2 changed files with 17 additions and 1 deletions

View file

@ -41,7 +41,10 @@ SOFTWARE.
#import <UIKit/UIDevice.h>
#import <UIKit/UIScreen.h>
#import <UIKit/UIApplication.h>
#import <UiKit/UIWebView.h>
#import <AdSupport/ASIdentifierManager.h>
// Export user agent for HTTP module.
NSString * gBrowserUserAgent = nil;
#endif // TARGET_OS_IPHONE
using namespace alohalytics;
@ -287,8 +290,11 @@ static UIBackgroundTaskIdentifier sBackgroundTaskId = UIBackgroundTaskInvalid;
}
+ (void)setup:(NSString *)serverUrl andFirstLaunch:(BOOL)isFirstLaunch withLaunchOptions:(NSDictionary *)options {
#if (TARGET_OS_IPHONE > 0)
// Initialize User Agent later, as it takes significant time at startup.
dispatch_async(dispatch_get_main_queue(), ^(void) {
gBrowserUserAgent = [[[UIWebView alloc] initWithFrame:CGRectZero] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
});
// Subscribe to basic app lifecycle events.
sBackgroundThreadQueue = ::dispatch_queue_create([serverUrl UTF8String], DISPATCH_QUEUE_SERIAL);
NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];

View file

@ -36,6 +36,11 @@ SOFTWARE.
#import <Foundation/NSError.h>
#import <Foundation/NSFileManager.h>
#include <TargetConditionals.h> // TARGET_OS_IPHONE
#if (TARGET_OS_IPHONE > 0) // Works for all iOS devices, including iPad.
extern NSString * gBrowserUserAgent;
#endif
#include "../http_client.h"
#include "../logger.h"
@ -60,6 +65,11 @@ bool HTTPClientPlatformWrapper::RunHTTPRequest() {
if (!user_agent_.empty()) {
[request setValue:[NSString stringWithUTF8String:user_agent_.c_str()] forHTTPHeaderField:@"User-Agent"];
}
#if (TARGET_OS_IPHONE > 0)
else if (gBrowserUserAgent) {
[request setValue:gBrowserUserAgent forHTTPHeaderField:@"User-Agent"];
}
#endif // TARGET_OS_IPHONE
if (!basic_auth_user_.empty()) {
NSData * loginAndPassword = [[NSString stringWithUTF8String:(basic_auth_user_ + ":" + basic_auth_password_).c_str()] dataUsingEncoding:NSUTF8StringEncoding];
[request setValue:[NSString stringWithFormat:@"Basic %@", [loginAndPassword base64Encoding]] forHTTPHeaderField:@"Authorization"];