Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ios): Remove deprecation warnings and old code #1031

Merged
merged 2 commits into from
Nov 4, 2023
Merged
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
149 changes: 32 additions & 117 deletions src/ios/CDVWKInAppBrowser.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@ Licensed to the Apache Software Foundation (ASF) under one

#import "CDVWKInAppBrowser.h"
#import <Cordova/NSDictionary+CordovaPreferences.h>

#if __has_include(<Cordova/CDVWebViewProcessPoolFactory.h>) // Cordova-iOS >=6
#import <Cordova/CDVWebViewProcessPoolFactory.h>
#elif __has_include("CDVWKProcessPoolFactory.h") // Cordova-iOS <6 with WKWebView plugin
#import "CDVWKProcessPoolFactory.h"
#endif

#import <Cordova/CDVWebViewProcessPoolFactory.h>
#import <Cordova/CDVPluginResult.h>

#define kInAppBrowserTargetSelf @"_self"
Expand All @@ -43,11 +37,6 @@ Licensed to the Apache Software Foundation (ASF) under one

#pragma mark CDVWKInAppBrowser

@interface CDVWKInAppBrowser () {
NSInteger _previousStatusBarStyle;
}
@end

@implementation CDVWKInAppBrowser

static CDVWKInAppBrowser* instance = nil;
Expand All @@ -59,7 +48,6 @@ + (id) getInstance{
- (void)pluginInitialize
{
instance = self;
_previousStatusBarStyle = -1;
_callbackIdPattern = nil;
_beforeload = @"";
_waitForBeforeload = NO;
Expand Down Expand Up @@ -140,64 +128,27 @@ - (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
}

if (browserOptions.clearcache) {
bool isAtLeastiOS11 = false;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
if (@available(iOS 11.0, *)) {
isAtLeastiOS11 = true;
}
#endif

if(isAtLeastiOS11){
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
// Deletes all cookies
WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray* cookies) {
NSHTTPCookie* cookie;
for(cookie in cookies){
[cookieStore deleteCookie:cookie completionHandler:nil];
}
}];
#endif
}else{
// https://stackoverflow.com/a/31803708/777265
// Only deletes domain cookies (not session cookies)
[dataStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
completionHandler:^(NSArray<WKWebsiteDataRecord *> * __nonnull records) {
for (WKWebsiteDataRecord *record in records){
NSSet<NSString*>* dataTypes = record.dataTypes;
if([dataTypes containsObject:WKWebsiteDataTypeCookies]){
[[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
forDataRecords:@[record]
completionHandler:^{}];
}
}
}];
}
// Deletes all cookies
WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray* cookies) {
NSHTTPCookie* cookie;
for(cookie in cookies){
[cookieStore deleteCookie:cookie completionHandler:nil];
}
}];
}

if (browserOptions.clearsessioncache) {
bool isAtLeastiOS11 = false;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
if (@available(iOS 11.0, *)) {
isAtLeastiOS11 = true;
}
#endif
if (isAtLeastiOS11) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
// Deletes session cookies
WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray* cookies) {
NSHTTPCookie* cookie;
for(cookie in cookies){
if(cookie.sessionOnly){
[cookieStore deleteCookie:cookie completionHandler:nil];
}
// Deletes session cookies
WKHTTPCookieStore* cookieStore = dataStore.httpCookieStore;
[cookieStore getAllCookies:^(NSArray* cookies) {
NSHTTPCookie* cookie;
for(cookie in cookies){
if(cookie.sessionOnly){
[cookieStore deleteCookie:cookie completionHandler:nil];
}
}];
#endif
}else{
NSLog(@"clearsessioncache not available below iOS 11.0");
}
}
}];
}

if (self.inAppBrowserViewController == nil) {
Expand Down Expand Up @@ -279,14 +230,6 @@ - (void)show:(CDVInvokedUrlCommand*)command withNoAnimate:(BOOL)noAnimate
NSLog(@"Tried to show IAB after it was closed.");
return;
}
if (_previousStatusBarStyle != -1) {
NSLog(@"Tried to show IAB while already shown");
return;
}

if(!initHidden){
_previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
}

__block CDVInAppBrowserNavigationController* nav = [[CDVInAppBrowserNavigationController alloc]
initWithRootViewController:self.inAppBrowserViewController];
Expand Down Expand Up @@ -334,17 +277,10 @@ - (void)hide:(CDVInvokedUrlCommand*)command


}
if (_previousStatusBarStyle == -1) {
NSLog(@"Tried to hide IAB while already hidden");
return;
}

_previousStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;

// Run later to avoid the "took a long time" log message.
dispatch_async(dispatch_get_main_queue(), ^{
if (self.inAppBrowserViewController != nil) {
_previousStatusBarStyle = -1;
[self.inAppBrowserViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
}
});
Expand All @@ -354,16 +290,17 @@ - (void)openInCordovaWebView:(NSURL*)url withOptions:(NSString*)options
{
NSURLRequest* request = [NSURLRequest requestWithURL:url];
// the webview engine itself will filter for this according to <allow-navigation> policy
// in config.xml for cordova-ios-4.0
// in config.xml
[self.webViewEngine loadRequest:request];
}

- (void)openInSystem:(NSURL*)url
{
if ([[UIApplication sharedApplication] openURL:url] == NO) {
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
[[UIApplication sharedApplication] openURL:url];
}
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
if (!success) {
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
}
}];
}

- (void)loadAfterBeforeload:(CDVInvokedUrlCommand*)command
Expand Down Expand Up @@ -682,15 +619,6 @@ - (void)browserExit
// Based on https://stackoverflow.com/questions/4544489/how-to-remove-a-uiwindow
self->tmpWindow.hidden = YES;
self->tmpWindow = nil;

if (IsAtLeastiOSVersion(@"7.0")) {
if (_previousStatusBarStyle != -1) {
[[UIApplication sharedApplication] setStatusBarStyle:_previousStatusBarStyle];

}
}

_previousStatusBarStyle = -1; // this value was reset before reapplying it. caused statusbar to stay black on ios7
}

@end //CDVWKInAppBrowser
Expand Down Expand Up @@ -752,15 +680,11 @@ - (void)createViews

//WKWebView options
configuration.allowsInlineMediaPlayback = _browserOptions.allowinlinemediaplayback;
if (IsAtLeastiOSVersion(@"10.0")) {
configuration.ignoresViewportScaleLimits = _browserOptions.enableviewportscale;
if(_browserOptions.mediaplaybackrequiresuseraction == YES){
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;
}else{
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
}
}else{ // iOS 9
configuration.mediaPlaybackRequiresUserAction = _browserOptions.mediaplaybackrequiresuseraction;
configuration.ignoresViewportScaleLimits = _browserOptions.enableviewportscale;
if(_browserOptions.mediaplaybackrequiresuseraction == YES){
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeAll;
}else{
configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
}

if (@available(iOS 13.0, *)) {
Expand Down Expand Up @@ -813,11 +737,7 @@ - (void)createViews
self.webView.allowsLinkPreview = NO;
self.webView.allowsBackForwardNavigationGestures = NO;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
if (@available(iOS 11.0, *)) {
[self.webView.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
#endif
[self.webView.scrollView setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];

self.spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
self.spinner.alpha = 1.000;
Expand Down Expand Up @@ -947,7 +867,7 @@ - (void)setCloseButtonTitle:(NSString*)title : (NSString*) colorString : (int) b
// but, if you want to set this yourself, knock yourself out (we can't set the title for a system Done button, so we have to create a new one)
self.closeButton = nil;
// Initialize with title if title is set, otherwise the title will be 'Done' localized
self.closeButton = title != nil ? [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleBordered target:self action:@selector(close)] : [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
self.closeButton = title != nil ? [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStylePlain target:self action:@selector(close)] : [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(close)];
self.closeButton.enabled = YES;
// If color on closebutton is requested then initialize with that that color, otherwise use initialize with default
self.closeButton.tintColor = colorString != nil ? [self colorFromHexString:colorString] : [UIColor colorWithRed:60.0 / 255.0 green:136.0 / 255.0 blue:230.0 / 255.0 alpha:1];
Expand Down Expand Up @@ -1148,13 +1068,8 @@ - (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
}

//
// On iOS 7 the status bar is part of the view's dimensions, therefore it's height has to be taken into account.
// The height of it could be hardcoded as 20 pixels, but that would assume that the upcoming releases of iOS won't
// change that value.
//
- (float) getStatusBarOffset {
return (float) IsAtLeastiOSVersion(@"7.0") ? [[UIApplication sharedApplication] statusBarFrame].size.height : 0.0;
return (float) [[UIApplication sharedApplication] statusBarFrame].size.height;
}

- (void) rePositionViews {
Expand Down
Loading