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

Commit

Permalink
Change UIWebView (deprecated since iOS 12.0) to WKWebView (#2)
Browse files Browse the repository at this point in the history
* replaced deprecated UIWebView with WKWebKit

* fixed instance retain issues

* added API_AVAILABLE keyword to property and check if iOS 8.0 is available in method call -> WKWebView is only available since iOS 8
  • Loading branch information
jstuth authored and j0j00 committed Oct 1, 2019
1 parent cdf7a9d commit dc4e121
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
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

0 comments on commit dc4e121

Please sign in to comment.