Skip to content
This repository has been archived by the owner on Jun 5, 2021. It is now read-only.

Change UIWebView (deprecated since iOS 12.0) to WKWebView #2

Merged
merged 3 commits into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ios/Classes/FlutterUserAgentPlugin.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#import <sys/utsname.h>
#import <UIKit/UIKit.h>
#import <Flutter/Flutter.h>
#import <WebKit/WebKit.h>

@interface FlutterUserAgentPlugin : NSObject<FlutterPlugin>
@property (nonatomic) bool isEmulator;
@property (nonatomic) WKWebView* webView API_AVAILABLE(ios(8.0));
@end
58 changes: 37 additions & 21 deletions ios/Classes/FlutterUserAgentPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([@"getProperties" isEqualToString:call.method]) {
result([self constantsToExport]);
[self constantsToExport:^(NSDictionary * _Nonnull constants) {
result(constants);
}];
} else {
result(FlutterMethodNotImplemented);
}
}

@synthesize isEmulator;
@synthesize webView;

//eg. Darwin/16.3.0
- (NSString *)darwinVersion
Expand Down Expand Up @@ -146,15 +149,26 @@ - (NSString *)deviceName

}

- (NSString *)getWebViewUserAgent
- (void)getWebViewUserAgent:(void (^ _Nullable)(NSString * _Nullable webViewUserAgent, NSError * _Nullable error))completionHandler
{
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString * webViewUserAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
// Do we need to free up the webView?
return webViewUserAgent;
if (@available(ios 8.0, *)) {
if (self.webView == nil) {
// retain because `evaluateJavaScript:` is asynchronous
self.webView = [[WKWebView alloc] init];
}
// Not sure if this is really neccesary
[self.webView loadHTMLString:@"<html></html>" baseURL:nil];

[self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:completionHandler];
} else {
UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectZero];
NSString * webViewUserAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];
// Do we need to free up the webView?
completionHandler(webViewUserAgent, nil);
}
}

- (NSDictionary *)constantsToExport
- (void)constantsToExport:(void (^ _Nullable)(NSDictionary * _Nonnull constants))completionHandler
{
UIDevice *currentDevice = [UIDevice currentDevice];

Expand All @@ -167,20 +181,22 @@ - (NSDictionary *)constantsToExport

NSString *userAgent = [NSString stringWithFormat:@"CFNetwork/%@ Darwin/%@ (%@ %@/%@)", cfnVersion, darwinVersion, deviceName, currentDevice.systemName, currentDevice.systemVersion];

return @{
@"isEmulator": @(self.isEmulator),
@"systemName": currentDevice.systemName,
@"systemVersion": currentDevice.systemVersion,
@"applicationName": appName,
@"applicationVersion": appVersion,
@"buildNumber": buildNumber,
@"darwinVersion": darwinVersion,
@"cfnetworkVersion": cfnVersion,
@"deviceName": deviceName,
@"packageUserAgent": [NSString stringWithFormat:@"%@/%@.%@ %@)", appName, appVersion, buildNumber, userAgent],
@"userAgent": userAgent,
@"webViewUserAgent": self.getWebViewUserAgent ?: [NSNull null]
};
[self getWebViewUserAgent:^(NSString * _Nullable webViewUserAgent, NSError * _Nullable error) {
completionHandler(@{
@"isEmulator": @(self.isEmulator),
@"systemName": currentDevice.systemName,
@"systemVersion": currentDevice.systemVersion,
@"applicationName": appName,
@"applicationVersion": appVersion,
@"buildNumber": buildNumber,
@"darwinVersion": darwinVersion,
@"cfnetworkVersion": cfnVersion,
@"deviceName": deviceName,
@"packageUserAgent": [NSString stringWithFormat:@"%@/%@.%@ %@)", appName, appVersion, buildNumber, userAgent],
@"userAgent": userAgent,
@"webViewUserAgent": webViewUserAgent ?: [NSNull null]
});
}];
}

@end