Skip to content

Commit

Permalink
feat(ios): Add limitsNavigationsToAppBoundDomains configuration option (
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile authored Jul 6, 2021
1 parent 6791348 commit 2b7016f
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,21 @@ export interface CapacitorConfig {
* @since 3.0.0
*/
includePlugins?: string[];

/**
* Sets WKWebView configuration for limitsNavigationsToAppBoundDomains.
*
* If the Info.plist file includes `WKAppBoundDomains` key, it's recommended to
* set this option to true, otherwise some features won't work.
* But as side effect, it blocks navigation outside the domains in the
* `WKAppBoundDomains` list.
* `localhost` (or the value configured as `server.hostname`) also needs to be
* added to the `WKAppBoundDomains` list.
*
* @since 3.1.0
* @default false
*/
limitsNavigationsToAppBoundDomains?: boolean;
};

server?: {
Expand Down
3 changes: 3 additions & 0 deletions ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ import Cordova
webViewConfiguration.suppressesIncrementalRendering = false
webViewConfiguration.allowsAirPlayForMediaPlayback = true
webViewConfiguration.mediaTypesRequiringUserActionForPlayback = []
if #available(iOS 14.0, *) {
webViewConfiguration.limitsNavigationsToAppBoundDomains = instanceConfiguration.limitsNavigationsToAppBoundDomains
}
if let appendUserAgent = instanceConfiguration.appendedUserAgentString {
if let appName = webViewConfiguration.applicationNameForUserAgent {
webViewConfiguration.applicationNameForUserAgent = "\(appName) \(appendUserAgent)"
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 @@ -22,6 +22,7 @@ NS_SWIFT_NAME(InstanceConfiguration)
@property (nonatomic, readonly) UIScrollViewContentInsetAdjustmentBehavior contentInsetAdjustmentBehavior;
@property (nonatomic, readonly, nonnull) NSURL *appLocation;
@property (nonatomic, readonly, nullable) NSString *appStartPath;
@property (nonatomic, readonly) BOOL limitsNavigationsToAppBoundDomains;

@property (nonatomic, readonly, nonnull) NSDictionary *legacyConfig DEPRECATED_MSG_ATTRIBUTE("Use direct properties instead");

Expand Down
1 change: 1 addition & 0 deletions ios/Capacitor/Capacitor/CAPInstanceConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ - (instancetype)initWithDescriptor:(CAPInstanceDescriptor *)descriptor isDebug:(
_contentInsetAdjustmentBehavior = descriptor.contentInsetAdjustmentBehavior;
_appLocation = descriptor.appLocation;
_appStartPath = descriptor.appStartPath;
_limitsNavigationsToAppBoundDomains = descriptor.limitsNavigationsToAppBoundDomains;
_pluginConfigurations = descriptor.pluginConfigurations;
_legacyConfig = descriptor.legacyConfig;
// construct the necessary URLs
Expand Down
5 changes: 5 additions & 0 deletions ios/Capacitor/Capacitor/CAPInstanceDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ NS_SWIFT_NAME(InstanceDescriptor)
@discussion Defaults to nil, in which case Capacitor will attempt to load @c index.html.
*/
@property (nonatomic, copy, nullable) NSString *appStartPath;
/**
@brief Whether or not the Capacitor WebView will limit the navigation to @c WKAppBoundDomains listed in the Info.plist.
@discussion Defaults to @c false. Set by @c ios.limitsNavigationsToAppBoundDomains in the configuration file. Required to be @c true for plugins to work if the app includes @c WKAppBoundDomains in the Info.plist.
*/
@property (nonatomic, assign) BOOL limitsNavigationsToAppBoundDomains;
/**
@brief The parser used to load the cofiguration for Cordova plugins.
*/
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 {
_handleApplicationNotifications = YES;
_contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
_appLocation = location;
_limitsNavigationsToAppBoundDomains = FALSE;
_cordovaConfiguration = [[CDVConfigParser alloc] init];
_warnings = 0;
if (location == nil) {
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 @@ -122,6 +122,9 @@ internal extension InstanceDescriptor {
} else if let hideLogs = (config[keyPath: "ios.hideLogs"] as? Bool) ?? (config[keyPath: "hideLogs"] as? Bool), hideLogs {
loggingBehavior = .none
}
if let limitsNavigations = config[keyPath: "ios.limitsNavigationsToAppBoundDomains"] as? Bool {
limitsNavigationsToAppBoundDomains = limitsNavigations
}
}
}
// swiftlint:enable cyclomatic_complexity
Expand Down

0 comments on commit 2b7016f

Please sign in to comment.