Skip to content

Commit 92df3b7

Browse files
authored
fix(compat): Set the app delegate viewController for plugins (#1493)
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.
1 parent a8f9c57 commit 92df3b7

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CordovaLib/Classes/Public/CDVViewController.m

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Licensed to the Apache Software Foundation (ASF) under one
2222
#import <WebKit/WebKit.h>
2323
#import <objc/message.h>
2424

25+
#import <Cordova/CDVAppDelegate.h>
2526
#import <Cordova/CDVPlugin.h>
2627
#import "CDVPlugin+Private.h"
2728
#import <Cordova/CDVConfigParser.h>
@@ -295,6 +296,17 @@ - (void)viewDidLoad
295296
{
296297
[super viewDidLoad];
297298

299+
// TODO: Remove in Cordova iOS 9
300+
if ([UIApplication.sharedApplication.delegate isKindOfClass:[CDVAppDelegate class]]) {
301+
CDVAppDelegate *appDelegate = (CDVAppDelegate *)UIApplication.sharedApplication.delegate;
302+
#pragma clang diagnostic push
303+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
304+
if (appDelegate.viewController == nil) {
305+
appDelegate.viewController = self;
306+
}
307+
#pragma clang diagnostic pop
308+
}
309+
298310
// Load settings
299311
[self loadSettings];
300312

CordovaLib/include/Cordova/CDVAppDelegate.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ NS_ASSUME_NONNULL_BEGIN
4141
@property (nullable, nonatomic, strong) IBOutlet UIWindow *window API_DEPRECATED_WITH_REPLACEMENT("SceneDelegate:window", ios(2.0, 13.0));
4242

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

4646
@end
4747

0 commit comments

Comments
 (0)