diff --git a/cli/src/declarations.ts b/cli/src/declarations.ts index 4bd9b54cd..a0975a6be 100644 --- a/cli/src/declarations.ts +++ b/cli/src/declarations.ts @@ -449,6 +449,14 @@ export interface CapacitorConfig { * @default false */ webContentsDebuggingEnabled?: boolean; + + /** + * Whether to give the webview initial focus. + * + * @since 7.0.0 + * @default true + */ + initialFocus?: boolean; }; server?: { diff --git a/ios/Capacitor/Capacitor/CAPBridgeViewController.swift b/ios/Capacitor/Capacitor/CAPBridgeViewController.swift index 6b50e85c1..93ab96c39 100644 --- a/ios/Capacitor/Capacitor/CAPBridgeViewController.swift +++ b/ios/Capacitor/Capacitor/CAPBridgeViewController.swift @@ -59,10 +59,17 @@ import Cordova override open func viewDidLoad() { super.viewDidLoad() - self.becomeFirstResponder() loadWebView() } + override open func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + + if bridge?.config.hasInitialFocus ?? true { + self.webView?.becomeFirstResponder() + } + } + override open func canPerformUnwindSegueAction(_ action: Selector, from fromViewController: UIViewController, withSender sender: Any) -> Bool { return false } diff --git a/ios/Capacitor/Capacitor/CAPInstanceConfiguration.h b/ios/Capacitor/Capacitor/CAPInstanceConfiguration.h index 81ebe3d1b..171dbb463 100644 --- a/ios/Capacitor/Capacitor/CAPInstanceConfiguration.h +++ b/ios/Capacitor/Capacitor/CAPInstanceConfiguration.h @@ -21,6 +21,7 @@ NS_SWIFT_NAME(InstanceConfiguration) @property (nonatomic, readonly) BOOL allowLinkPreviews; @property (nonatomic, readonly) BOOL handleApplicationNotifications; @property (nonatomic, readonly) BOOL isWebDebuggable; +@property (nonatomic, readonly) BOOL hasInitialFocus; @property (nonatomic, readonly) BOOL cordovaDeployDisabled; @property (nonatomic, readonly) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior; @property (nonatomic, readonly, nonnull) NSURL *appLocation; diff --git a/ios/Capacitor/Capacitor/CAPInstanceConfiguration.m b/ios/Capacitor/Capacitor/CAPInstanceConfiguration.m index 0bb8ecd8f..d6b7785e3 100644 --- a/ios/Capacitor/Capacitor/CAPInstanceConfiguration.m +++ b/ios/Capacitor/Capacitor/CAPInstanceConfiguration.m @@ -39,6 +39,7 @@ - (instancetype)initWithDescriptor:(CAPInstanceDescriptor *)descriptor isDebug:( _preferredContentMode = descriptor.preferredContentMode; _pluginConfigurations = descriptor.pluginConfigurations; _isWebDebuggable = descriptor.isWebDebuggable; + _hasInitialFocus = descriptor.hasInitialFocus; _legacyConfig = descriptor.legacyConfig; // construct the necessary URLs _localURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@://%@", descriptor.urlScheme, descriptor.urlHostname]]; @@ -71,6 +72,7 @@ - (instancetype)initWithConfiguration:(CAPInstanceConfiguration*)configuration a _allowLinkPreviews = configuration.allowLinkPreviews; _handleApplicationNotifications = configuration.handleApplicationNotifications; _isWebDebuggable = configuration.isWebDebuggable; + _hasInitialFocus = configuration.hasInitialFocus; _cordovaDeployDisabled = configuration.cordovaDeployDisabled; _contentInsetAdjustmentBehavior = configuration.contentInsetAdjustmentBehavior; // we don't care about internal usage of deprecated APIs and the framework should build cleanly diff --git a/ios/Capacitor/Capacitor/CAPInstanceDescriptor.h b/ios/Capacitor/Capacitor/CAPInstanceDescriptor.h index 228b3a41d..f477ef55b 100644 --- a/ios/Capacitor/Capacitor/CAPInstanceDescriptor.h +++ b/ios/Capacitor/Capacitor/CAPInstanceDescriptor.h @@ -103,6 +103,12 @@ NS_SWIFT_NAME(InstanceDescriptor) @discussion Defaults to true in debug mode and false in production */ @property (nonatomic, assign) BOOL isWebDebuggable; +/** + @brief Whether or not the webview will have focus. + @discussion Defaults to @c true. Set by @c ios.initialFocus in the configuration file. + */ +@property (nonatomic, assign) BOOL hasInitialFocus; + /** @brief How the web view will inset its content @discussion Set by @c ios.contentInset in the configuration file. Corresponds to @c contentInsetAdjustmentBehavior on WKWebView. diff --git a/ios/Capacitor/Capacitor/CAPInstanceDescriptor.m b/ios/Capacitor/Capacitor/CAPInstanceDescriptor.m index 6d3566776..2d7a4fba9 100644 --- a/ios/Capacitor/Capacitor/CAPInstanceDescriptor.m +++ b/ios/Capacitor/Capacitor/CAPInstanceDescriptor.m @@ -41,6 +41,7 @@ - (void)_setDefaultsWithAppLocation:(NSURL*)location { _allowLinkPreviews = YES; _handleApplicationNotifications = YES; _isWebDebuggable = NO; + _hasInitialFocus = YES; _contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; _appLocation = location; _limitsNavigationsToAppBoundDomains = FALSE; diff --git a/ios/Capacitor/Capacitor/CAPInstanceDescriptor.swift b/ios/Capacitor/Capacitor/CAPInstanceDescriptor.swift index 9645d005e..75b990b72 100644 --- a/ios/Capacitor/Capacitor/CAPInstanceDescriptor.swift +++ b/ios/Capacitor/Capacitor/CAPInstanceDescriptor.swift @@ -141,6 +141,9 @@ internal extension InstanceDescriptor { isWebDebuggable = true #endif } + if let initialFocus = config[keyPath: "ios.initialFocus"] as? Bool { + hasInitialFocus = initialFocus + } } } // swiftlint:enable cyclomatic_complexity