Skip to content

Commit

Permalink
Use WKWebView instead of UIWebView to get userAgent
Browse files Browse the repository at this point in the history
  • Loading branch information
thviQit committed Mar 13, 2020
1 parent a4cbf6f commit 2584bcb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion QueueITLib/IOSUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@interface IOSUtils : NSObject
+(NSString*)getUserId;
+(NSString*)getUserAgent;
+(void)getUserAgent:(void (^)(NSString*))completionHandler;
+(NSString*)getLibraryVersion;
+(NSString*)getSdkVersion;
@end
21 changes: 16 additions & 5 deletions QueueITLib/IOSUtils.m
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#import <UIKit/UIKit.h>
#import <WebKit/WebKit.h>
#import "IOSUtils.h"

@implementation IOSUtils
WKWebView* webView;

+(NSString*)getUserId{
UIDevice* device = [[UIDevice alloc]init];
Expand All @@ -10,10 +11,20 @@ +(NSString*)getUserId{
return uuid;
}

+(NSString*)getUserAgent{
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString* secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
return secretAgent;
+(void)getUserAgent:(void (^)(NSString*))completionHandler{
WKWebView* view = [[WKWebView alloc] initWithFrame:CGRectZero];
[view evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable userAgent, NSError * _Nullable error) {
if (error == nil) {
completionHandler(userAgent);
}
else {
NSLog(@"Error getting userAgent");
NSLog(@"%@", [error localizedDescription]);
completionHandler(@"");
}
}];

webView = view;
}

+(NSString*)getLibraryVersion{
Expand Down
10 changes: 8 additions & 2 deletions QueueITLib/QueueITEngine.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#import <UIKit/UIKit.h>
#import "QueueITEngine.h"
#import "QueueITWKViewController.h"
#import "QueueService.h"
Expand Down Expand Up @@ -143,9 +142,16 @@ -(void)showQueue:(NSString*)queueUrl targetUrl:(NSString*)targetUrl
}

-(void)tryEnqueue
{
[IOSUtils getUserAgent:^(NSString * userAgent) {
[self tryEnqueueWithUserAgent:userAgent];
}];
}

-(void)tryEnqueueWithUserAgent:(NSString*)secretAgent
{
NSString* userId = [IOSUtils getUserId];
NSString* userAgent = [NSString stringWithFormat:@"%@;%@", [IOSUtils getUserAgent], [IOSUtils getLibraryVersion]];
NSString* userAgent = [NSString stringWithFormat:@"%@;%@", secretAgent, [IOSUtils getLibraryVersion]];
NSString* sdkVersion = [IOSUtils getSdkVersion];

QueueService* qs = [QueueService sharedInstance];
Expand Down

0 comments on commit 2584bcb

Please sign in to comment.