Skip to content
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
32 changes: 30 additions & 2 deletions lib/src/browser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,22 @@ class Browser {
_chrome,
_firefox,
_safari,
_internetExplorer
_internetExplorer,
_wkWebView
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@clairesarsam-wf @robbecker-wf what do you guys think about detecting these "sub" type browsers like WkWebView is for the most part the same as Safari, similar to Rob's question about separating the detection of IE vs Edge from earlier. When WKWebView is detected, should isSafari return true as well as isWKWebView?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@travissanderson-wf We could do that but the issue is if we return true for isSafari in the WKWebView the there would not be a version because the Safari and the WKWebView have different version strings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it could still have a version, currently it would be different depending on if it was parsed out by _Safari or _WKWebView, I just mean we could change the getter for isSafari = this == _safari || this == _wkWebView

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I could see it being useful to know if you're running in a webview.

];

bool get isChrome => this == _chrome;
bool get isFirefox => this == _firefox;
bool get isSafari => this == _safari;
bool get isInternetExplorer => this == _internetExplorer;
bool get isWKWebView => this == _wkWebView;
}

Browser _chrome = new _Chrome();
Browser _firefox = new _Firefox();
Browser _safari = new _Safari();
Browser _internetExplorer = new _InternetExplorer();
Browser _wkWebView = new _WKWebView();

class _Chrome extends Browser {
_Chrome() : super('Chrome', _isChrome, _getVersion);
Expand Down Expand Up @@ -95,7 +98,11 @@ class _Safari extends Browser {
_Safari() : super('Safari', _isSafari, _getVersion);

static bool _isSafari(NavigatorProvider navigator) {
return navigator.vendor.contains('Apple');
// An web view running in an iOS app does not have a 'Version/X.X.X' string in the appVersion
var vendor = navigator.vendor;
return vendor != null &&
vendor.contains('Apple') &&
navigator.appVersion.contains('Version');
}

static Version _getVersion(NavigatorProvider navigator) {
Expand All @@ -108,6 +115,27 @@ class _Safari extends Browser {
}
}

class _WKWebView extends Browser {
_WKWebView() : super('WKWebView', _isWKWebView, _getVersion);

static bool _isWKWebView(NavigatorProvider navigator) {
// An web view running in an iOS app does not have a 'Version/X.X.X' string in the appVersion
var vendor = navigator.vendor;
return vendor != null &&
vendor.contains('Apple') &&
!navigator.appVersion.contains('Version');
}

static Version _getVersion(NavigatorProvider navigator) {
Match match = new RegExp(r'AppleWebKit/(\d+)\.(\d+)\.(\d+)')
.firstMatch(navigator.appVersion);
var major = int.parse(match.group(1));
var minor = int.parse(match.group(2));
var patch = int.parse(match.group(3));
return new Version(major, minor, patch);
}
}

class _InternetExplorer extends Browser {
_InternetExplorer()
: super('Internet Explorer', _isInternetExplorer, _getVersion);
Expand Down
2 changes: 1 addition & 1 deletion smithy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ artifacts:
documentation:
- ./doc/api/api.tar.gz
pub:
- ./platform_detect.pub.tgz
- ./platform_detect.pub.tgz
19 changes: 19 additions & 0 deletions test/browser_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,24 @@ void main() {
expect(browser.isInternetExplorer, false);
expect(browser.version, new Version(9, 1, 3));
});

test('WKWebView', () {
var navigator = new TestNavigator()
..vendor = 'Apple Computer, Inc.'
..userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.8 (KHTML, like Gecko) Version/9.1.3 Safari/601.7.8'
..appVersion = '5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.8 (KHTML, like Gecko) Safari/601.7.8'
..appName = 'Netscape';

Browser.navigator = navigator;
Browser browser = Browser.getCurrentBrowser();

expect(browser.name, 'WKWebView');
expect(browser.isChrome, false);
expect(browser.isFirefox, false);
expect(browser.isSafari, false);
expect(browser.isWKWebView, true);
expect(browser.isInternetExplorer, false);
expect(browser.version, new Version(601, 7, 8));
});
});
}