Skip to content

Commit

Permalink
feature: async user agent usinc WKWebView
Browse files Browse the repository at this point in the history
  • Loading branch information
radko93 authored and mikehardy committed Aug 29, 2019
1 parent 8e4fe78 commit 07bf0aa
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion ios/RNDeviceInfo/RNDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#import <React/RCTUtils.h>
#import "RNDeviceInfo.h"
#import "DeviceUID.h"
#import <WebKit/WebKit.h>

#if !(TARGET_OS_TV)
#import <LocalAuthentication/LocalAuthentication.h>
Expand All @@ -34,6 +35,7 @@ typedef NS_ENUM(NSInteger, DeviceType) {

@implementation RNDeviceInfo
{
WKWebView *webView;
bool hasListeners;
}

Expand Down Expand Up @@ -131,7 +133,7 @@ - (NSString*) deviceLocale
return [NSLocale preferredLanguages];
}

- (NSString*) deviceCountry
- (NSString*) deviceCountry
{
NSString *country = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
return country;
Expand Down Expand Up @@ -398,6 +400,32 @@ - (NSDictionary *)powerState
resolve(@(locationServicesEnabled));
}

RCT_EXPORT_METHOD(getUserAgent:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
#if TARGET_OS_TV
reject(@"not available");
#else
__weak RNDeviceInfo *weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong RNDeviceInfo *strongSelf = weakSelf;
if (strongSelf) {
// Save WKWebView (it might deallocate before we ask for user Agent)
strongSelf->webView = [[WKWebView alloc] init];

[strongSelf->webView evaluateJavaScript:@"window.navigator.userAgent;" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
if (error) {
reject(@"getUserAgentError", error.localizedDescription, error);
return;
}
resolve([NSString stringWithFormat:@"%@", result]);
// Destroy the WKWebView after task is complete
strongSelf->webView = nil;
}];
}
});
#endif
}

RCT_EXPORT_METHOD(getAvailableLocationProviders:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject)
{
#if !TARGET_OS_TV
Expand Down

0 comments on commit 07bf0aa

Please sign in to comment.