Skip to content

Commit

Permalink
fix(ios): Make Bridge webView first responder (#7753)
Browse files Browse the repository at this point in the history
  • Loading branch information
theproducer authored Nov 21, 2024
1 parent 0a0dd1a commit 77e4668
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?: {
Expand Down
9 changes: 8 additions & 1 deletion ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions ios/Capacitor/Capacitor/CAPInstanceConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions ios/Capacitor/Capacitor/CAPInstanceConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -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]];
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions ios/Capacitor/Capacitor/CAPInstanceDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions ios/Capacitor/Capacitor/CAPInstanceDescriptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ - (void)_setDefaultsWithAppLocation:(NSURL*)location {
_allowLinkPreviews = YES;
_handleApplicationNotifications = YES;
_isWebDebuggable = NO;
_hasInitialFocus = YES;
_contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
_appLocation = location;
_limitsNavigationsToAppBoundDomains = FALSE;
Expand Down
3 changes: 3 additions & 0 deletions ios/Capacitor/Capacitor/CAPInstanceDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 77e4668

Please sign in to comment.