Skip to content

Commit

Permalink
refactor(template)!: Update app template to Swift & storyboards (#1457)
Browse files Browse the repository at this point in the history
* refactor!: Make CDVViewController more API friendly

* refactor: Remove outdated app init code from CDVAppDelegate

The template's storyboard should handle the initialization of the view
controller, rather than constructing it manually here.

* refactor!: Convert template project to Swift

Bump min iOS version for template app to 13.0 (due to UISceneDelegate)

* refactor: Add stub headers for bad plugin extensions

* fix: Turn off user script sandboxing for the app project
  • Loading branch information
dpogue authored Aug 20, 2024
1 parent 495ab61 commit 68aa80e
Show file tree
Hide file tree
Showing 20 changed files with 487 additions and 502 deletions.
23 changes: 0 additions & 23 deletions CordovaLib/Classes/Public/CDVAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,8 @@ - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:
return YES;
}

/**
* This is main kick off after the app inits, the views and Settings are setup here. (preferred - iOS4 and up)
*/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary<UIApplicationLaunchOptionsKey, id> *)launchOptions
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];

self.window = [[UIWindow alloc] initWithFrame:screenBounds];
self.window.autoresizesSubviews = YES;

// only set if not already set in subclass
if (self.viewController == nil) {
self.viewController = [[CDVViewController alloc] init];
}

// Set your app's start page by setting the <content src='foo.html' /> tag in config.xml.
// If necessary, uncomment the line below to override it.
// self.viewController.startPage = @"index.html";

// NOTE: To customize the view's frame size (which defaults to full screen), override
// [self.viewController viewWillAppear:] in your view controller.

self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];

return YES;
}

Expand Down
40 changes: 31 additions & 9 deletions CordovaLib/Classes/Public/CDVViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ Licensed to the Apache Software Foundation (ASF) under one
#import <Cordova/NSDictionary+CordovaPreferences.h>
#import "CDVCommandDelegateImpl.h"

UIColor* defaultBackgroundColor(void) {
static UIColor* defaultBackgroundColor(void) {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
if (@available(iOS 13.0, *)) {
return UIColor.systemBackgroundColor;
} else {
return UIColor.whiteColor;
}
#endif

return UIColor.whiteColor;
}

@interface CDVViewController () <CDVWebViewEngineConfigurationDelegate> { }
@interface CDVViewController () <CDVWebViewEngineConfigurationDelegate> {
id <CDVWebViewEngineProtocol> _webViewEngine;
id <CDVCommandDelegate> _commandDelegate;
CDVCommandQueue* _commandQueue;
UIColor* _backgroundColor;
UIColor* _splashBackgroundColor;
}

@property (nonatomic, readwrite, strong) NSXMLParser* configParser;
@property (nonatomic, readwrite, strong) NSMutableDictionary* settings;
Expand All @@ -62,6 +70,8 @@ @implementation CDVViewController
@synthesize commandDelegate = _commandDelegate;
@synthesize commandQueue = _commandQueue;
@synthesize webViewEngine = _webViewEngine;
@synthesize backgroundColor = _backgroundColor;
@synthesize splashBackgroundColor = _splashBackgroundColor;
@dynamic webView;

- (void)__init
Expand All @@ -85,6 +95,8 @@ - (void)__init
name:CDVPageDidLoadNotification object:nil];


self.showInitialSplashScreen = YES;

self.initialized = YES;
}
}
Expand All @@ -110,6 +122,16 @@ - (id)init
return self;
}

- (void)setBackgroundColor:(UIColor *)color
{
_backgroundColor = color ?: defaultBackgroundColor();
}

- (void)setSplashBackgroundColor:(UIColor *)color
{
_splashBackgroundColor = color ?: self.backgroundColor;
}

-(NSString*)configFilePath{
NSString* path = self.configFile ?: @"config.xml";

Expand Down Expand Up @@ -298,12 +320,12 @@ - (void)viewDidLoad
}
// /////////////////

UIColor* bgDefault = defaultBackgroundColor();
UIColor* bgColor = [UIColor colorNamed:@"BackgroundColor"] ?: bgDefault;
UIColor* bgSplash = [UIColor colorNamed:@"SplashScreenBackgroundColor"] ?: bgColor;
[self.webView setBackgroundColor:self.backgroundColor];
[self.launchView setBackgroundColor:self.splashBackgroundColor];

[self.webView setBackgroundColor:bgColor];
[self.launchView setBackgroundColor:bgSplash];
if (self.showInitialSplashScreen) {
[self.launchView setAlpha:1];
}
}

-(void)viewWillAppear:(BOOL)animated
Expand Down
51 changes: 39 additions & 12 deletions CordovaLib/include/Cordova/CDVViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,11 @@

@class CDVPlugin;

/*!
@abstract The main view controller for Cordova web content.
*/
@interface CDVViewController : UIViewController {
@protected
id <CDVWebViewEngineProtocol> _webViewEngine;
@protected
id <CDVCommandDelegate> _commandDelegate;
@protected
CDVCommandQueue* _commandQueue;
}
@interface CDVViewController : UIViewController

NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, readonly, weak) IBOutlet UIView* webView;
@property (nonatomic, readonly, strong) UIView* launchView;

@property (nullable, nonatomic, readonly, strong) NSMutableDictionary* pluginObjects;
@property (nonatomic, readonly, strong) NSDictionary* pluginsMap;
Expand All @@ -60,6 +49,38 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic, readonly, strong) id <CDVCommandDelegate> commandDelegate;


/**
A boolean value indicating whether to show the splash screen while the webview
is initially loading.
The default value is `YES`.
This can be set in the storyboard file as a view controller attribute.
*/
@property (nonatomic) IBInspectable BOOL showInitialSplashScreen;

/**
The color drawn behind the web content.
This is used as the background color for the web view behind any HTML content
and during loading before web content has been rendered. The default value is
the system background color.
This can be set in the storyboard file as a view controller attribute.
*/
@property (nonatomic, null_resettable, copy) IBInspectable UIColor *backgroundColor;

/**
The color drawn behind the splash screen content.
This is used as the background color for the splash screen while the web
content is loading. If a page background color has been specified, that will
be used as the default value, otherwise the system background color is used.
This can be set in the storyboard file as a view controller attribute.
*/
@property (nonatomic, null_resettable, copy) IBInspectable UIColor *splashBackgroundColor;

- (UIView*)newCordovaViewWithFrame:(CGRect)bounds;

- (nullable NSString*)appURLScheme;
Expand All @@ -71,6 +92,12 @@ NS_ASSUME_NONNULL_BEGIN

- (void)parseSettingsWithParser:(NSObject <NSXMLParserDelegate>*)delegate;

/**
Toggles the display of the splash screen overtop of the web view.
- Parameters:
- visible: Whether to make the splash screen visible or not.
*/
- (void)showLaunchScreen:(BOOL)visible;

NS_ASSUME_NONNULL_END
Expand Down
10 changes: 4 additions & 6 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,11 @@ class ProjectCreator {
['__PROJECT_NAME__.xcworkspace', 'xcshareddata', 'xcschemes', '__PROJECT_NAME__.xcscheme'],
['__PROJECT_NAME__.xcodeproj', 'project.pbxproj'],
['__PROJECT_NAME__', 'AppDelegate.h'],
['__PROJECT_NAME__', 'AppDelegate.m'],
['__PROJECT_NAME__', 'MainViewController.h'],
['__PROJECT_NAME__', 'MainViewController.m'],
['__PROJECT_NAME__', 'main.m'],
['__PROJECT_NAME__', '__PROJECT_NAME__-Info.plist'],
['__PROJECT_NAME__', '__PROJECT_NAME__-Prefix.pch']
['__PROJECT_NAME__', 'AppDelegate.swift'],
['__PROJECT_NAME__', 'SceneDelegate.swift'],
['__PROJECT_NAME__', 'ViewController.swift'],
['__PROJECT_NAME__', '__PROJECT_NAME__-Info.plist']
]) {
this.expandProjectNameInFileContents(this.projectPath(...p));
}
Expand All @@ -192,7 +191,6 @@ class ProjectCreator {
['__PROJECT_NAME__.xcworkspace'],
['__PROJECT_NAME__.xcodeproj'],
['__PROJECT_NAME__', '__PROJECT_NAME__-Info.plist'],
['__PROJECT_NAME__', '__PROJECT_NAME__-Prefix.pch'],
['__PROJECT_NAME__']
]) {
this.expandProjectNameInBaseName(this.projectPath(...p));
Expand Down
3 changes: 0 additions & 3 deletions templates/cordova/build-debug.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
//

#include "build.xcconfig"

GCC_PREPROCESSOR_DEFINITIONS = $(inherited) DEBUG=1

#include "build-extras.xcconfig"

// (CB-11792)
Expand Down
4 changes: 0 additions & 4 deletions templates/cordova/build-release.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
//

#include "build.xcconfig"

CODE_SIGN_IDENTITY = iPhone Distribution
CODE_SIGN_IDENTITY[sdk=iphoneos*] = iPhone Distribution

#include "build-extras.xcconfig"

// (CB-11792)
Expand Down
19 changes: 0 additions & 19 deletions templates/cordova/build.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,3 @@
// XCode build settings shared by all Build Configurations.
// Settings are overridden by configuration-level .xcconfig file (build-release/build-debug).
//

HEADER_SEARCH_PATHS = "$(TARGET_BUILD_DIR)/usr/local/lib/include" "$(OBJROOT)/UninstalledProducts/include" "$(OBJROOT)/UninstalledProducts/$(PLATFORM_NAME)/include" "$(BUILT_PRODUCTS_DIR)"
OTHER_LDFLAGS = -ObjC

// Type of signing identity used for codesigning, resolves to first match of given type.
// "iPhone Developer": Development builds (default, local only; iOS Development certificate) or "iPhone Distribution": Distribution builds (Adhoc/In-House/AppStore; iOS Distribution certificate)
CODE_SIGN_IDENTITY = iPhone Developer
CODE_SIGN_IDENTITY[sdk=iphoneos*] = iPhone Developer

// (CB-9721) Set ENABLE_BITCODE to NO in build.xcconfig
ENABLE_BITCODE = NO

// (CB-9719) Set CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES to YES in build.xcconfig
CLANG_ALLOW_NON_MODULAR_INCLUDES_IN_FRAMEWORK_MODULES = YES

// (CB-10072)
SWIFT_OBJC_BRIDGING_HEADER = $(PROJECT_DIR)/$(PROJECT_NAME)/Bridging-Header.h

GCC_PREPROCESSOR_DEFINITIONS = $(inherited) WK_WEB_VIEW_ONLY=$(WK_WEB_VIEW_ONLY)
Loading

0 comments on commit 68aa80e

Please sign in to comment.