Skip to content

Commit

Permalink
refactor!: Easy deprecation cleanups (#1455)
Browse files Browse the repository at this point in the history
* refactor: Deprecate some API to be removed in next major

* refactor: Remove some dead code for pre-iOS 11 versions

* refactor: Make CDVPlugin viewController a CDVViewController

This has generally always been the case, but it was typed as a
UIViewController which resulted in a bunch of casting when you wanted to
do something useful with it.

* refactor!: Deprecate and remove some unused orientation code

* refactor: Move the CDVWebViewEngineConfigurationDelegate protocol
  • Loading branch information
dpogue authored Aug 15, 2024
1 parent c8e126d commit c6fd16f
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,12 @@ @implementation CDVLaunchScreen

- (void)show:(CDVInvokedUrlCommand*)command
{
if ([self.viewController isKindOfClass:[CDVViewController class]]) {
[(CDVViewController*)self.viewController showLaunchScreen:YES];
}
[self.viewController showLaunchScreen:YES];
}

- (void)hide:(CDVInvokedUrlCommand*)command
{
if ([self.viewController isKindOfClass:[CDVViewController class]]) {
[(CDVViewController*)self.viewController showLaunchScreen:NO];
}
[self.viewController showLaunchScreen:NO];
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ Licensed to the Apache Software Foundation (ASF) under one
#import <objc/message.h>

#define CDV_BRIDGE_NAME @"cordova"
#define CDV_WKWEBVIEW_FILE_URL_LOAD_SELECTOR @"loadFileURL:allowingReadAccessToURL:"

@interface CDVWebViewWeakScriptMessageHandler : NSObject <WKScriptMessageHandler>

Expand Down Expand Up @@ -272,32 +271,6 @@ - (void)pluginInitialize
name:UIApplicationWillEnterForegroundNotification object:nil];

NSLog(@"Using WKWebView");

[self addURLObserver];
}

- (void)onReset {
[self addURLObserver];
}

static void * KVOContext = &KVOContext;

- (void)addURLObserver {
if(!IsAtLeastiOSVersion(@"9.0")){
[self.webView addObserver:self forKeyPath:@"URL" options:0 context:KVOContext];
}
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
if (context == KVOContext) {
if (object == [self webView] && [keyPath isEqualToString: @"URL"] && [object valueForKeyPath:keyPath] == nil){
NSLog(@"URL is nil. Reloading WKWebView");
[(WKWebView*)_engineWebView reload];
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}

- (void) onAppWillEnterForeground:(NSNotification*)notification {
Expand Down Expand Up @@ -381,15 +354,7 @@ - (NSURL*) URL

- (BOOL) canLoadRequest:(NSURLRequest*)request
{
// See: https://issues.apache.org/jira/browse/CB-9636
SEL wk_sel = NSSelectorFromString(CDV_WKWEBVIEW_FILE_URL_LOAD_SELECTOR);

// if it's a file URL, check whether WKWebView has the selector (which is in iOS 9 and up only)
if (request.URL.fileURL) {
return [_engineWebView respondsToSelector:wk_sel];
} else {
return YES;
}
return YES;
}

- (void)updateSettings:(NSDictionary*)settings
Expand Down
12 changes: 10 additions & 2 deletions CordovaLib/Classes/Public/CDVAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ Licensed to the Apache Software Foundation (ASF) under one
*/

#import <Cordova/CDVAppDelegate.h>
#import <Cordova/CDVAvailability.h>
#import <Cordova/CDVPlugin.h>
#import <Cordova/CDVViewController.h>

@implementation CDVAppDelegate

Expand Down Expand Up @@ -68,6 +71,10 @@ - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDiction
return NO;
}

// all plugins will get the notification, and their handlers will be called
[[NSNotificationCenter defaultCenter] postNotificationName:CDVPluginHandleOpenURLNotification object:url userInfo:options];

// TODO: This should be deprecated and removed in Cordova iOS 9, since we're passing this data in the notification userInfo now
NSMutableDictionary * openURLData = [[NSMutableDictionary alloc] init];

[openURLData setValue:url forKey:@"url"];
Expand All @@ -80,9 +87,10 @@ - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDiction
[openURLData setValue:options[UIApplicationOpenURLOptionsAnnotationKey] forKey:@"annotation"];
}

// all plugins will get the notification, and their handlers will be called
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification object:openURLData]];
#pragma clang diagnostic pop

return YES;
}
Expand Down
1 change: 1 addition & 0 deletions CordovaLib/Classes/Public/CDVCommandQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Licensed to the Apache Software Foundation (ASF) under one
#include <objc/message.h>
#import <Cordova/CDVCommandQueue.h>
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVPlugin.h>
#import "CDVCommandDelegateImpl.h"
#import "CDVJSON_private.h"
#import "CDVDebug.h"
Expand Down
6 changes: 5 additions & 1 deletion CordovaLib/Classes/Public/CDVPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ - (instancetype)initWithWebViewEngine:(id <CDVWebViewEngineProtocol>)theWebViewE
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppTerminate) name:UIApplicationWillTerminateNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOpenURL:) name:CDVPluginHandleOpenURLNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOpenURLWithApplicationSourceAndAnnotation:) name:CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReset) name:CDVPluginResetNotification object:theWebViewEngine.engineWebView];

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleOpenURLWithApplicationSourceAndAnnotation:) name:CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification object:nil];
#pragma clang diagnostic pop

self.webViewEngine = theWebViewEngine;
}
return self;
Expand Down
1 change: 1 addition & 0 deletions CordovaLib/Classes/Public/CDVPluginResult.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one
// using CDVCommandStatus as ObjC-style constants rather than as Swift enum
// values.
// These constants alias the enum values back to their previous names.
// TODO: Remove in Cordova iOS 9
#define SWIFT_ENUM_COMPAT_HACK(enumVal) const CDVCommandStatus SWIFT_##enumVal NS_SWIFT_NAME(enumVal) = enumVal
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_NO_RESULT);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_OK);
Expand Down
73 changes: 1 addition & 72 deletions CordovaLib/Classes/Public/CDVViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Licensed to the Apache Software Foundation (ASF) under one
#import <objc/message.h>
#import <Foundation/NSCharacterSet.h>
#import <Cordova/CDV.h>
#import <Cordova/CDVPlugin.h>
#import "CDVPlugin+Private.h"
#import <Cordova/CDVConfigParser.h>
#import <Cordova/NSDictionary+CordovaPreferences.h>
Expand Down Expand Up @@ -55,7 +56,6 @@ @interface CDVViewController () <CDVWebViewEngineConfigurationDelegate> { }

@implementation CDVViewController

@synthesize supportedOrientations;
@synthesize pluginObjects, pluginsMap, startupPluginNames;
@synthesize configParser, settings;
@synthesize wwwFolderName, startPage, initialized, openURL;
Expand Down Expand Up @@ -84,9 +84,6 @@ - (void)__init
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onWebViewPageDidLoad:)
name:CDVPageDidLoadNotification object:nil];

// read from UISupportedInterfaceOrientations (or UISupportedInterfaceOrientations~iPad, if its iPad) from -Info.plist
self.supportedOrientations = [self parseInterfaceOrientations:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"UISupportedInterfaceOrientations"]];

self.initialized = YES;
}
Expand Down Expand Up @@ -351,65 +348,6 @@ -(void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIVie
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVViewWillTransitionToSizeNotification object:[NSValue valueWithCGSize:size]]];
}

- (NSArray*)parseInterfaceOrientations:(NSArray*)orientations
{
NSMutableArray* result = [[NSMutableArray alloc] init];

if (orientations != nil) {
NSEnumerator* enumerator = [orientations objectEnumerator];
NSString* orientationString;

while (orientationString = [enumerator nextObject]) {
if ([orientationString isEqualToString:@"UIInterfaceOrientationPortrait"]) {
[result addObject:[NSNumber numberWithInt:UIInterfaceOrientationPortrait]];
} else if ([orientationString isEqualToString:@"UIInterfaceOrientationPortraitUpsideDown"]) {
[result addObject:[NSNumber numberWithInt:UIInterfaceOrientationPortraitUpsideDown]];
} else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeLeft"]) {
[result addObject:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeLeft]];
} else if ([orientationString isEqualToString:@"UIInterfaceOrientationLandscapeRight"]) {
[result addObject:[NSNumber numberWithInt:UIInterfaceOrientationLandscapeRight]];
}
}
}

// default
if ([result count] == 0) {
[result addObject:[NSNumber numberWithInt:UIInterfaceOrientationPortrait]];
}

return result;
}

- (BOOL)shouldAutorotate
{
return YES;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
NSUInteger ret = 0;

if ([self supportsOrientation:UIInterfaceOrientationPortrait]) {
ret = ret | (1 << UIInterfaceOrientationPortrait);
}
if ([self supportsOrientation:UIInterfaceOrientationPortraitUpsideDown]) {
ret = ret | (1 << UIInterfaceOrientationPortraitUpsideDown);
}
if ([self supportsOrientation:UIInterfaceOrientationLandscapeRight]) {
ret = ret | (1 << UIInterfaceOrientationLandscapeRight);
}
if ([self supportsOrientation:UIInterfaceOrientationLandscapeLeft]) {
ret = ret | (1 << UIInterfaceOrientationLandscapeLeft);
}

return ret;
}

- (BOOL)supportsOrientation:(UIInterfaceOrientation)orientation
{
return [self.supportedOrientations containsObject:@(orientation)];
}

/// Retrieves the view from a newwly initialized webViewEngine
/// @param bounds The bounds with which the webViewEngine will be initialized
- (nonnull UIView*)newCordovaViewWithFrame:(CGRect)bounds
Expand Down Expand Up @@ -697,15 +635,6 @@ - (void)onAppWillEnterForeground:(NSNotification*)notification
[self checkAndReinitViewUrl];
// NSLog(@"%@",@"applicationWillEnterForeground");
[self.commandDelegate evalJs:@"cordova.fireDocumentEvent('resume');"];

if (!IsAtLeastiOSVersion(@"11.0")) {
/** Clipboard fix **/
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
NSString* string = pasteboard.string;
if (string) {
[pasteboard setValue:string forPasteboardType:@"public.text"];
}
}
}

// This method is called to let your application know that it moved from the inactive to active state.
Expand Down
10 changes: 7 additions & 3 deletions CordovaLib/include/Cordova/CDVAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
*/

#import <UIKit/UIKit.h>
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVAvailabilityDeprecated.h>

@class CDVViewController;

@interface CDVAppDelegate : UIResponder <UIApplicationDelegate>

@property (nullable, nonatomic, strong) IBOutlet UIWindow* window;
@property (nullable, nonatomic, strong) IBOutlet CDVViewController* viewController;
@property (nullable, nonatomic, strong) IBOutlet UIWindow* window __deprecated_msg("The window is now managed through the iOS SceneDelegate.");

// TODO: Remove in Cordova iOS 9
@property (nullable, nonatomic, strong) IBOutlet CDVViewController* viewController CDV_DEPRECATED(8, "");

@end
2 changes: 2 additions & 0 deletions CordovaLib/include/Cordova/CDVAvailabilityDeprecated.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#ifdef __clang__
#define CDV_DEPRECATED(version, msg) __attribute__((deprecated("Deprecated in Cordova " #version ". " msg)))
#define CDV_DEPRECATED_WITH_REPLACEMENT(version, msg, repl) __attribute__((deprecated("Deprecated in Cordova " #version ". " msg, repl)))
#else
#define CDV_DEPRECATED(version, msg) __attribute__((deprecated()))
#define CDV_DEPRECATED_WITH_REPLACEMENT(version, msg, repl) __attribute__((deprecated()))
#endif
8 changes: 5 additions & 3 deletions CordovaLib/include/Cordova/CDVPlugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Cordova/CDVAvailabilityDeprecated.h>
#import <Cordova/CDVPluginResult.h>
#import <Cordova/NSMutableArray+QueueAdditions.h>
#import <Cordova/CDVCommandDelegate.h>
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVWebViewEngineProtocol.h>

@interface UIView (org_apache_cordova_UIView_Extension)
Expand All @@ -32,7 +34,7 @@

extern NSString* const CDVPageDidLoadNotification;
extern NSString* const CDVPluginHandleOpenURLNotification;
extern NSString* const CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification;
extern NSString* const CDVPluginHandleOpenURLWithAppSourceAndAnnotationNotification CDV_DEPRECATED(8, "Find sourceApplication and annotations in the userInfo of the CDVPluginHandleOpenURLNotification notification.");
extern NSString* const CDVPluginResetNotification;
extern NSString* const CDVViewWillAppearNotification;
extern NSString* const CDVViewDidAppearNotification;
Expand All @@ -47,15 +49,15 @@ extern NSString* const CDVViewWillTransitionToSizeNotification;
@property (nonatomic, readonly, weak) UIView* webView;
@property (nonatomic, readonly, weak) id <CDVWebViewEngineProtocol> webViewEngine;

@property (nonatomic, weak) UIViewController* viewController;
@property (nonatomic, weak) CDVViewController* viewController;
@property (nonatomic, weak) id <CDVCommandDelegate> commandDelegate;

@property (readonly, assign) BOOL hasPendingOperation;

- (void)pluginInitialize;

- (void)handleOpenURL:(NSNotification*)notification;
- (void)handleOpenURLWithApplicationSourceAndAnnotation:(NSNotification*)notification;
- (void)handleOpenURLWithApplicationSourceAndAnnotation:(NSNotification*)notification CDV_DEPRECATED(8, "Use the handleOpenUrl method and the notification userInfo data.");
- (void)onAppTerminate;
- (void)onMemoryWarning;
- (void)onReset;
Expand Down
25 changes: 14 additions & 11 deletions CordovaLib/include/Cordova/CDVPluginResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,25 @@ typedef NS_ENUM(NSUInteger, CDVCommandStatus) {
CDVCommandStatus_ERROR NS_SWIFT_NAME(error)
};

#ifdef __swift__
// This exists to preserve compatibility with early Swift plugins, who are
// using CDVCommandStatus as ObjC-style constants rather than as Swift enum
// values.
// This declares extern'ed constants (implemented in CDVPluginResult.m)
#define SWIFT_ENUM_COMPAT_HACK(enumVal) extern const CDVCommandStatus SWIFT_##enumVal NS_SWIFT_NAME(enumVal)
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_NO_RESULT);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_OK);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_CLASS_NOT_FOUND_EXCEPTION);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_ILLEGAL_ACCESS_EXCEPTION);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_INSTANTIATION_EXCEPTION);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_MALFORMED_URL_EXCEPTION);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_IO_EXCEPTION);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_INVALID_ACTION);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_JSON_EXCEPTION);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_ERROR);
// TODO: Remove this in Cordova iOS 9
#define SWIFT_ENUM_COMPAT_HACK(enumVal, replacement) extern const CDVCommandStatus SWIFT_##enumVal NS_SWIFT_NAME(enumVal) CDV_DEPRECATED_WITH_REPLACEMENT(8, "Use the CDVCommandStatus." #replacement " enum value instead", "CDVCommandStatus." #replacement)
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_NO_RESULT, noResult);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_OK, ok);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_CLASS_NOT_FOUND_EXCEPTION, classNotFoundException);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_ILLEGAL_ACCESS_EXCEPTION, illegalAccessException);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_INSTANTIATION_EXCEPTION, instantiationException);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_MALFORMED_URL_EXCEPTION, malformedUrlException);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_IO_EXCEPTION, ioException);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_INVALID_ACTION, invalidAction);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_JSON_EXCEPTION, jsonException);
SWIFT_ENUM_COMPAT_HACK(CDVCommandStatus_ERROR, error);
#undef SWIFT_ENUM_COMPAT_HACK
#endif

@interface CDVPluginResult : NSObject {}

Expand Down
2 changes: 2 additions & 0 deletions CordovaLib/include/Cordova/CDVScreenOrientationDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/

#import <Foundation/Foundation.h>
#import <Cordova/CDVAvailabilityDeprecated.h>

CDV_DEPRECATED(8, "Unused")
@protocol CDVScreenOrientationDelegate <NSObject>

- (UIInterfaceOrientationMask)supportedInterfaceOrientations;
Expand Down
1 change: 1 addition & 0 deletions CordovaLib/include/Cordova/CDVURLSchemeHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#import <Foundation/Foundation.h>
#import <WebKit/WebKit.h>
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVPlugin.h>


@interface CDVURLSchemeHandler : NSObject <WKURLSchemeHandler>
Expand Down
Loading

0 comments on commit c6fd16f

Please sign in to comment.