Skip to content

Commit

Permalink
fix(compat): Set the app delegate viewController for plugins
Browse files Browse the repository at this point in the history
Some plugins expect to be able to access the applications's
CDVViewController via the AppDelegate. While previously we guaranteed
that the `viewController` property was set to a non-nil
CDVViewController, there was never any guarantee that it was set to the
CDVViewController that is actually displaying the app (particularly in
cases such as apps using CordovaLib only for a few pages as part of a
larger app).

We've attempted to deprecate the property here, but some plugins still
rely on the assumption that it will be non-nil, so we are trying to
maintain compatibility in the common case here by assigning to it when a
CDVViewController is loaded. This means there are still times in the app
lifecycle where the `viewController` property is nil, but the
deprecation warning will hopefully spur plugin developers to move away
from that pattern.

Note: CDVPlugin always has a non-nil viewController property of its own,
which is set to the CDVViewController instance the plugin was loaded
into. This is almost always the better choice for plugins to use.
  • Loading branch information
dpogue committed Oct 4, 2024
1 parent 98a3bcd commit 3e7df48
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
11 changes: 11 additions & 0 deletions CordovaLib/Classes/Public/CDVViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,17 @@ - (void)viewDidLoad
{
[super viewDidLoad];

// TODO: Remove in Cordova iOS 9
if ([UIApplication.sharedApplication.delegate isKindOfClass:[CDVAppDelegate class]]) {
CDVAppDelegate *appDelegate = (CDVAppDelegate *)UIApplication.sharedApplication.delegate;
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if (appDelegate.viewController == nil) {
appDelegate.viewController = self;
}
#pragma clang diagnostic pop
}

// Load settings
[self loadSettings];

Expand Down
2 changes: 1 addition & 1 deletion CordovaLib/include/Cordova/CDVAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nullable, nonatomic, strong) IBOutlet UIWindow *window API_DEPRECATED_WITH_REPLACEMENT("SceneDelegate:window", ios(2.0, 13.0));

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

@end

Expand Down

0 comments on commit 3e7df48

Please sign in to comment.