Skip to content

Commit

Permalink
feat(ios): add webContentsDebuggingEnabled configuration (#6495)
Browse files Browse the repository at this point in the history
Co-authored-by: jcesarmobile <[email protected]>
  • Loading branch information
markemer and jcesarmobile authored Apr 13, 2023
1 parent 0a40477 commit c691e4a
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cli/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,16 @@ export interface CapacitorConfig {
* @default true
*/
handleApplicationNotifications?: boolean;

/**
* Using Xcode 14.3, on iOS 16.4 and greater, enable debuggable web content for release builds.
*
* If not set, it's `true` for development builds.
*
* @since 4.8.0
* @default false
*/
webContentsDebuggingEnabled?: boolean;
};

server?: {
Expand Down
4 changes: 4 additions & 0 deletions ios/Capacitor/Capacitor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
2F81F5CA26FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 2F81F5C826FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.m */; };
373A69C1255C9360000A6F44 /* NotificationHandlerProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373A69C0255C9360000A6F44 /* NotificationHandlerProtocol.swift */; };
373A69F2255C95D0000A6F44 /* NotificationRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 373A69F1255C95D0000A6F44 /* NotificationRouter.swift */; };
4D0D590F29E86FAB008A6833 /* WKWebView+Capacitor.h in Headers */ = {isa = PBXBuildFile; fileRef = 4D0D590D29E86FAB008A6833 /* WKWebView+Capacitor.h */; settings = {ATTRIBUTES = (Public, ); }; };
501CBAA71FC0A723009B0D4D /* WebKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 501CBAA61FC0A723009B0D4D /* WebKit.framework */; };
50503EE91FC08595003606DC /* Capacitor.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 50503EDF1FC08594003606DC /* Capacitor.framework */; };
50503EEE1FC08595003606DC /* CapacitorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 50503EED1FC08595003606DC /* CapacitorTests.swift */; };
Expand Down Expand Up @@ -143,6 +144,7 @@
2F81F5C826FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "CAPBridgeViewController+CDVScreenOrientationDelegate.m"; sourceTree = "<group>"; };
373A69C0255C9360000A6F44 /* NotificationHandlerProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationHandlerProtocol.swift; sourceTree = "<group>"; };
373A69F1255C95D0000A6F44 /* NotificationRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationRouter.swift; sourceTree = "<group>"; };
4D0D590D29E86FAB008A6833 /* WKWebView+Capacitor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "WKWebView+Capacitor.h"; sourceTree = "<group>"; };
501CBAA61FC0A723009B0D4D /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
50503EDF1FC08594003606DC /* Capacitor.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Capacitor.framework; sourceTree = BUILT_PRODUCTS_DIR; };
50503EE81FC08595003606DC /* CapacitorTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = CapacitorTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -359,6 +361,7 @@
62959B112524DA7700A3D7F1 /* Data+Capacitor.swift */,
62FABD1925AE5C01007B3814 /* Array+Capacitor.swift */,
62D43AEF2581817500673C24 /* WKWebView+Capacitor.swift */,
4D0D590D29E86FAB008A6833 /* WKWebView+Capacitor.h */,
62D43B642582A13D00673C24 /* WKWebView+Capacitor.m */,
2F81F5C726FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.h */,
2F81F5C826FB7CB400DD35BE /* CAPBridgeViewController+CDVScreenOrientationDelegate.m */,
Expand Down Expand Up @@ -441,6 +444,7 @@
buildActionMask = 2147483647;
files = (
62959B412524DA7800A3D7F1 /* Capacitor.h in Headers */,
4D0D590F29E86FAB008A6833 /* WKWebView+Capacitor.h in Headers */,
62959B452524DA7800A3D7F1 /* CAPPlugin.h in Headers */,
62959B162524DA7800A3D7F1 /* CAPPluginCall.h in Headers */,
62959B3B2524DA7800A3D7F1 /* CAPPluginMethod.h in Headers */,
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 @@ -19,6 +19,7 @@ NS_SWIFT_NAME(InstanceConfiguration)
@property (nonatomic, readonly) BOOL scrollingEnabled;
@property (nonatomic, readonly) BOOL allowLinkPreviews;
@property (nonatomic, readonly) BOOL handleApplicationNotifications;
@property (nonatomic, readonly) BOOL isWebDebuggable;
@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 @@ -37,6 +37,7 @@ - (instancetype)initWithDescriptor:(CAPInstanceDescriptor *)descriptor isDebug:(
_limitsNavigationsToAppBoundDomains = descriptor.limitsNavigationsToAppBoundDomains;
_preferredContentMode = descriptor.preferredContentMode;
_pluginConfigurations = descriptor.pluginConfigurations;
_isWebDebuggable = descriptor.isWebDebuggable;
_legacyConfig = descriptor.legacyConfig;
// construct the necessary URLs
_localURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@://%@", descriptor.urlScheme, descriptor.urlHostname]];
Expand Down Expand Up @@ -67,6 +68,7 @@ - (instancetype)initWithConfiguration:(CAPInstanceConfiguration*)configuration a
_scrollingEnabled = configuration.scrollingEnabled;
_allowLinkPreviews = configuration.allowLinkPreviews;
_handleApplicationNotifications = configuration.handleApplicationNotifications;
_isWebDebuggable = configuration.isWebDebuggable;
_cordovaDeployDisabled = configuration.cordovaDeployDisabled;
_contentInsetAdjustmentBehavior = configuration.contentInsetAdjustmentBehavior;
// we don't care about internal usage of deprecated APIs and the framework should build cleanly
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 @@ -93,6 +93,11 @@ NS_SWIFT_NAME(InstanceDescriptor)
@discussion Defaults to @c true. Required to be @c true for notification plugins to work correctly. Set to @c false if your application will handle notifications independently.
*/
@property (nonatomic, assign) BOOL handleApplicationNotifications;
/**
@brief Enables web debugging by setting isInspectable of @c WKWebView to @c true on iOS 16.4 and greater
@discussion Defaults to true in debug mode and false in production
*/
@property (nonatomic, assign) BOOL isWebDebuggable;
/**
@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 @@ -39,6 +39,7 @@ - (void)_setDefaultsWithAppLocation:(NSURL*)location {
_scrollingEnabled = YES;
_allowLinkPreviews = YES;
_handleApplicationNotifications = YES;
_isWebDebuggable = NO;
_contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
_appLocation = location;
_limitsNavigationsToAppBoundDomains = FALSE;
Expand Down
7 changes: 7 additions & 0 deletions ios/Capacitor/Capacitor/CAPInstanceDescriptor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ internal extension InstanceDescriptor {
if let handleNotifications = config[keyPath: "ios.handleApplicationNotifications"] as? Bool {
handleApplicationNotifications = handleNotifications
}
if let webContentsDebuggingEnabled = config[keyPath: "ios.webContentsDebuggingEnabled"] as? Bool {
isWebDebuggable = webContentsDebuggingEnabled
} else {
#if DEBUG
isWebDebuggable = true
#endif
}
}
}
// swiftlint:enable cyclomatic_complexity
Expand Down
2 changes: 2 additions & 0 deletions ios/Capacitor/Capacitor/Capacitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ FOUNDATION_EXPORT const unsigned char CapacitorVersionString[];
#import <Capacitor/CAPPluginMethod.h>
#import <Capacitor/CAPInstanceDescriptor.h>
#import <Capacitor/CAPInstanceConfiguration.h>
#import <Capacitor/WKWebView+Capacitor.h>

11 changes: 11 additions & 0 deletions ios/Capacitor/Capacitor/CapacitorBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,8 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol {
observers.append(NotificationCenter.default.addObserver(forName: type(of: self).tmpVCAppeared.name, object: .none, queue: .none) { [weak self] _ in
self?.tmpWindow = nil
})

self.setupWebDebugging(configuration: configuration)
}

deinit {
Expand Down Expand Up @@ -418,6 +420,15 @@ internal class CapacitorBridge: NSObject, CAPBridgeProtocol {
return "\(type(of: self).capacitorSite)docs/\(url)"
}

private func setupWebDebugging(configuration: InstanceConfiguration) {
let isWebDebuggable = configuration.isWebDebuggable
if isWebDebuggable, #unavailable(iOS 16.4) {
CAPLog.print("⚡️ Warning: isWebDebuggable only functions as intended on iOS 16.4 and above.")
}

self.webView?.setInspectableIfRequired(isWebDebuggable)
}

/**
Handle a call from JavaScript. First, find the corresponding plugin, construct a selector,
and perform that selector on the plugin instance.
Expand Down
11 changes: 11 additions & 0 deletions ios/Capacitor/Capacitor/WKWebView+Capacitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@import UIKit;
@import WebKit;

#ifndef WKWebView_Capacitor_h
#define WKWebView_Capacitor_h

@interface WKWebView (CapacitorInspectablity)
- (void)setInspectableIfRequired: (BOOL)shouldInspect;
@end

#endif /* WKWebView_Capacitor_h */
13 changes: 13 additions & 0 deletions ios/Capacitor/Capacitor/WKWebView+Capacitor.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,16 @@ + (void)load {
[self _swizzleKeyboardMethods];
}
@end

// TODO: Remove this after Xcode 14.3 is required
@implementation WKWebView (CapacitorInspectablity)

- (void)setInspectableIfRequired: (BOOL)shouldInspect {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 160400
if (@available(iOS 16.4, *)) {
self.inspectable = shouldInspect;
}
#endif
}

@end

0 comments on commit c691e4a

Please sign in to comment.