Skip to content

Commit

Permalink
feat(ios): Add preferredContentMode configuration option (#5583)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed May 3, 2022
1 parent ae04514 commit 5b6dfa3
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,18 @@ export interface CapacitorConfig {
* @default false
*/
limitsNavigationsToAppBoundDomains?: boolean;

/**
* The content mode for the web view to use when it loads and renders web content.
*
* - 'recommended': The content mode that is appropriate for the current device.
* - 'desktop': The content mode that represents a desktop experience.
* - 'mobile': The content mode that represents a mobile experience.
*
* @since 4.0.0
* @default recommended
*/
preferredContentMode?: 'recommended' | 'desktop' | 'mobile';
};

server?: {
Expand Down
9 changes: 9 additions & 0 deletions ios/Capacitor/Capacitor/CAPBridgeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ import Cordova
webViewConfiguration.applicationNameForUserAgent = appendUserAgent
}
}
if let preferredContentMode = instanceConfiguration.preferredContentMode {
var mode = WKWebpagePreferences.ContentMode.recommended
if preferredContentMode == "mobile" {
mode = WKWebpagePreferences.ContentMode.mobile
} else if preferredContentMode == "desktop" {
mode = WKWebpagePreferences.ContentMode.desktop
}
webViewConfiguration.defaultWebpagePreferences.preferredContentMode = mode
}
return webViewConfiguration
}

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 @@ -23,6 +23,7 @@ NS_SWIFT_NAME(InstanceConfiguration)
@property (nonatomic, readonly, nonnull) NSURL *appLocation;
@property (nonatomic, readonly, nullable) NSString *appStartPath;
@property (nonatomic, readonly) BOOL limitsNavigationsToAppBoundDomains;
@property (nonatomic, readonly, nullable) NSString *preferredContentMode;

@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 @@ -35,6 +35,7 @@ - (instancetype)initWithDescriptor:(CAPInstanceDescriptor *)descriptor isDebug:(
_appLocation = descriptor.appLocation;
_appStartPath = descriptor.appStartPath;
_limitsNavigationsToAppBoundDomains = descriptor.limitsNavigationsToAppBoundDomains;
_preferredContentMode = descriptor.preferredContentMode;
_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 @@ -108,6 +108,11 @@ NS_SWIFT_NAME(InstanceDescriptor)
@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 content mode for the web view to use when it loads and renders web content.
@discussion Defaults to @c recommended. Set by @c ios.preferredContentMode in the configuration file.
*/
@property (nonatomic, copy, nullable) NSString *preferredContentMode;
/**
@brief The parser used to load the cofiguration for Cordova plugins.
*/
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 @@ -125,6 +125,9 @@ internal extension InstanceDescriptor {
if let limitsNavigations = config[keyPath: "ios.limitsNavigationsToAppBoundDomains"] as? Bool {
limitsNavigationsToAppBoundDomains = limitsNavigations
}
if let preferredMode = (config[keyPath: "ios.preferredContentMode"] as? String) {
preferredContentMode = preferredMode
}
}
}
// swiftlint:enable cyclomatic_complexity
Expand Down

0 comments on commit 5b6dfa3

Please sign in to comment.