@capacitor/ios 4.0.0-beta.1 → 4.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,24 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ # [4.0.0-beta.2](https://github.com/ionic-team/capacitor/compare/4.0.0-beta.1...4.0.0-beta.2) (2022-07-08)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **ios:** Add check for both serverURL and localURL in navigation ([#5736](https://github.com/ionic-team/capacitor/issues/5736)) ([8e824f3](https://github.com/ionic-team/capacitor/commit/8e824f33ad4df898fb8c0936a8f5e9041832a5c5))
12
+ * **ios:** properly deliver retained events after listener re-add [#5732](https://github.com/ionic-team/capacitor/issues/5732) ([c5d6328](https://github.com/ionic-team/capacitor/commit/c5d632831924a1bcc868bc46b42f7ff619408752))
13
+
14
+
15
+ ### Features
16
+
17
+ * **ios:** Add `setServerBasePath(path:)` to CAPWebView ([#5742](https://github.com/ionic-team/capacitor/issues/5742)) ([1afbf8a](https://github.com/ionic-team/capacitor/commit/1afbf8a9dd0b8f7b1ac439d24e5d8ba26f786318))
18
+ * Add CapWebView ([#5715](https://github.com/ionic-team/capacitor/issues/5715)) ([143d266](https://github.com/ionic-team/capacitor/commit/143d266ef0a818bac59dbbdaeda3b5c382ebfa1d))
19
+
20
+
21
+
22
+
23
+
6
24
  # [4.0.0-beta.1](https://github.com/ionic-team/capacitor/compare/4.0.0-beta.0...4.0.0-beta.1) (2022-06-27)
7
25
 
8
26
 
@@ -175,11 +175,7 @@ import Cordova
175
175
  }
176
176
  if let statusBarStyle = plist["UIStatusBarStyle"] as? String {
177
177
  if statusBarStyle == "UIStatusBarStyleDarkContent" {
178
- if #available(iOS 13.0, *) {
179
- self.statusBarStyle = .darkContent
180
- } else {
181
- self.statusBarStyle = .default
182
- }
178
+ self.statusBarStyle = .darkContent
183
179
  } else if statusBarStyle != "UIStatusBarStyleDefault" {
184
180
  self.statusBarStyle = .lightContent
185
181
  }
@@ -304,7 +300,7 @@ extension CAPBridgeViewController {
304
300
  if let backgroundColor = configuration.backgroundColor {
305
301
  aWebView.backgroundColor = backgroundColor
306
302
  aWebView.scrollView.backgroundColor = backgroundColor
307
- } else if #available(iOS 13, *) {
303
+ } else {
308
304
  // Use the system background colors if background is not set by user
309
305
  aWebView.backgroundColor = UIColor.systemBackground
310
306
  aWebView.scrollView.backgroundColor = UIColor.systemBackground
@@ -41,7 +41,7 @@
41
41
 
42
42
  - (void)addEventListener:(NSString *)eventName listener:(CAPPluginCall *)listener {
43
43
  NSMutableArray *listenersForEvent = [self.eventListeners objectForKey:eventName];
44
- if(!listenersForEvent) {
44
+ if(listenersForEvent == nil || [listenersForEvent count] == 0) {
45
45
  listenersForEvent = [[NSMutableArray alloc] initWithObjects:listener, nil];
46
46
  [self.eventListeners setValue:listenersForEvent forKey:eventName];
47
47
 
@@ -0,0 +1,240 @@
1
+ import Foundation
2
+ import WebKit
3
+ import UIKit
4
+
5
+ open class CAPWebView: UIView {
6
+ lazy var webView: WKWebView = createWebView(
7
+ with: configuration,
8
+ assetHandler: assetHandler,
9
+ delegationHandler: delegationHandler
10
+ )
11
+
12
+ private lazy var capacitorBridge = CapacitorBridge(
13
+ with: configuration,
14
+ delegate: self,
15
+ cordovaConfiguration: configDescriptor.cordovaConfiguration,
16
+ assetHandler: assetHandler,
17
+ delegationHandler: delegationHandler
18
+ )
19
+
20
+ public final var bridge: CAPBridgeProtocol {
21
+ return capacitorBridge
22
+ }
23
+
24
+ private lazy var configDescriptor = instanceDescriptor()
25
+ private lazy var configuration = InstanceConfiguration(with: configDescriptor, isDebug: CapacitorBridge.isDevEnvironment)
26
+
27
+ private lazy var assetHandler: WebViewAssetHandler = {
28
+ let handler = WebViewAssetHandler(router: _Router())
29
+ handler.setAssetPath(configuration.appLocation.path)
30
+ return handler
31
+ }()
32
+
33
+ private lazy var delegationHandler = WebViewDelegationHandler()
34
+
35
+ public required init?(coder: NSCoder) {
36
+ super.init(coder: coder)
37
+ setup()
38
+ }
39
+
40
+ public init() {
41
+ super.init(frame: .zero)
42
+ setup()
43
+ }
44
+
45
+ private func setup() {
46
+ CAPLog.enableLogging = configuration.loggingEnabled
47
+ logWarnings(for: configDescriptor)
48
+
49
+ if configDescriptor.instanceType == .fixed { updateBinaryVersion() }
50
+
51
+ addSubview(webView)
52
+ webView.translatesAutoresizingMaskIntoConstraints = false
53
+ NSLayoutConstraint.activate([
54
+ webView.topAnchor.constraint(equalTo: topAnchor),
55
+ webView.bottomAnchor.constraint(equalTo: bottomAnchor),
56
+ webView.leadingAnchor.constraint(equalTo: leadingAnchor),
57
+ webView.trailingAnchor.constraint(equalTo: trailingAnchor)
58
+ ])
59
+
60
+ guard FileManager.default.fileExists(atPath: bridge.config.appStartFileURL.path) else { fatalLoadError() }
61
+ capacitorDidLoad()
62
+
63
+ let url = bridge.config.appStartServerURL
64
+ CAPLog.print("⚡️ Loading app at \(url.absoluteString)")
65
+ capacitorBridge.webViewDelegationHandler.willLoadWebview(webView)
66
+ _ = webView.load(URLRequest(url: url))
67
+ }
68
+
69
+ public lazy final var isNewBinary: Bool = {
70
+ if let curVersionCode = Bundle.main.infoDictionary?["CFBundleVersion"] as? String,
71
+ let curVersionName = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
72
+ if let lastVersionCode = UserDefaults.standard.string(forKey: "lastBinaryVersionCode"),
73
+ let lastVersionName = UserDefaults.standard.string(forKey: "lastBinaryVersionName") {
74
+ return (curVersionCode.isEqual(lastVersionCode) == false || curVersionName.isEqual(lastVersionName) == false)
75
+ }
76
+ }
77
+ return false
78
+ }()
79
+
80
+ open func instanceDescriptor() -> InstanceDescriptor {
81
+ let descriptor = InstanceDescriptor.init()
82
+ if !isNewBinary && !descriptor.cordovaDeployDisabled {
83
+ if let persistedPath = UserDefaults.standard.string(forKey: "serverBasePath"), !persistedPath.isEmpty {
84
+ if let libPath = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true).first {
85
+ descriptor.appLocation = URL(fileURLWithPath: libPath, isDirectory: true)
86
+ .appendingPathComponent("NoCloud")
87
+ .appendingPathComponent("ionic_built_snapshots")
88
+ .appendingPathComponent(URL(fileURLWithPath: persistedPath, isDirectory: true).lastPathComponent)
89
+ }
90
+ }
91
+ }
92
+ return descriptor
93
+ }
94
+
95
+ /**
96
+ Allows any additional configuration to be performed. The `webView` and `bridge` properties will be set by this point.
97
+
98
+ - Note: This is called before the webview has been added to the view hierarchy. Not all operations may be possible at
99
+ this time.
100
+ */
101
+ open func capacitorDidLoad() {
102
+ }
103
+
104
+ open func loadInitialContext(_ userContentController: WKUserContentController) {
105
+ CAPLog.print("in loadInitialContext base")
106
+ }
107
+
108
+ public func setServerBasePath(path: String) {
109
+ let url = URL(fileURLWithPath: path, isDirectory: true)
110
+ guard FileManager.default.fileExists(atPath: url.path) else { return }
111
+
112
+ capacitorBridge.config = capacitorBridge.config.updatingAppLocation(url)
113
+ capacitorBridge.webViewAssetHandler.setAssetPath(url.path)
114
+
115
+ DispatchQueue.main.async { [weak self] in
116
+ guard let self = self else { return }
117
+ _ = self.webView.load(URLRequest(url: self.capacitorBridge.config.serverURL))
118
+ }
119
+ }
120
+ }
121
+
122
+ extension CAPWebView {
123
+
124
+ open func webViewConfiguration(for instanceConfiguration: InstanceConfiguration) -> WKWebViewConfiguration {
125
+ let webViewConfiguration = WKWebViewConfiguration()
126
+ webViewConfiguration.allowsInlineMediaPlayback = true
127
+ webViewConfiguration.suppressesIncrementalRendering = false
128
+ webViewConfiguration.allowsAirPlayForMediaPlayback = true
129
+ webViewConfiguration.mediaTypesRequiringUserActionForPlayback = []
130
+ if let appendUserAgent = instanceConfiguration.appendedUserAgentString {
131
+ if let appName = webViewConfiguration.applicationNameForUserAgent {
132
+ webViewConfiguration.applicationNameForUserAgent = "\(appName) \(appendUserAgent)"
133
+ } else {
134
+ webViewConfiguration.applicationNameForUserAgent = appendUserAgent
135
+ }
136
+ }
137
+ return webViewConfiguration
138
+ }
139
+
140
+ private func createWebView(with configuration: InstanceConfiguration, assetHandler: WebViewAssetHandler, delegationHandler: WebViewDelegationHandler) -> WKWebView {
141
+ // set the cookie policy
142
+ HTTPCookieStorage.shared.cookieAcceptPolicy = HTTPCookie.AcceptPolicy.always
143
+ // setup the web view configuration
144
+ let webViewConfig = webViewConfiguration(for: configuration)
145
+ webViewConfig.setURLSchemeHandler(assetHandler, forURLScheme: configuration.localURL.scheme ?? InstanceDescriptorDefaults.scheme)
146
+ webViewConfig.userContentController = delegationHandler.contentController
147
+ // create the web view and set its properties
148
+ loadInitialContext(webViewConfig.userContentController)
149
+ let webView = WKWebView(frame: .zero, configuration: webViewConfig)
150
+ webView.scrollView.bounces = false
151
+ webView.scrollView.contentInsetAdjustmentBehavior = configuration.contentInsetAdjustmentBehavior
152
+ webView.allowsLinkPreview = configuration.allowLinkPreviews
153
+ webView.configuration.preferences.setValue(true, forKey: "allowFileAccessFromFileURLs")
154
+ webView.scrollView.isScrollEnabled = configuration.scrollingEnabled
155
+
156
+ if let overrideUserAgent = configuration.overridenUserAgentString {
157
+ webView.customUserAgent = overrideUserAgent
158
+ }
159
+
160
+ if let backgroundColor = configuration.backgroundColor {
161
+ self.backgroundColor = backgroundColor
162
+ webView.backgroundColor = backgroundColor
163
+ webView.scrollView.backgroundColor = backgroundColor
164
+ } else if #available(iOS 13, *) {
165
+ // Use the system background colors if background is not set by user
166
+ self.backgroundColor = UIColor.systemBackground
167
+ webView.backgroundColor = UIColor.systemBackground
168
+ webView.scrollView.backgroundColor = UIColor.systemBackground
169
+ }
170
+
171
+ // set our delegates
172
+ webView.uiDelegate = delegationHandler
173
+ webView.navigationDelegate = delegationHandler
174
+ return webView
175
+ }
176
+
177
+ private func logWarnings(for descriptor: InstanceDescriptor) {
178
+ if descriptor.warnings.contains(.missingAppDir) {
179
+ CAPLog.print("⚡️ ERROR: Unable to find application directory at: \"\(descriptor.appLocation.absoluteString)\"!")
180
+ }
181
+ if descriptor.instanceType == .fixed {
182
+ if descriptor.warnings.contains(.missingFile) {
183
+ CAPLog.print("Unable to find capacitor.config.json, make sure it exists and run npx cap copy.")
184
+ }
185
+ if descriptor.warnings.contains(.invalidFile) {
186
+ CAPLog.print("Unable to parse capacitor.config.json. Make sure it's valid JSON.")
187
+ }
188
+ if descriptor.warnings.contains(.missingCordovaFile) {
189
+ CAPLog.print("Unable to find config.xml, make sure it exists and run npx cap copy.")
190
+ }
191
+ if descriptor.warnings.contains(.invalidCordovaFile) {
192
+ CAPLog.print("Unable to parse config.xml. Make sure it's valid XML.")
193
+ }
194
+ }
195
+ }
196
+
197
+ private func updateBinaryVersion() {
198
+ guard isNewBinary else {
199
+ return
200
+ }
201
+ guard let versionCode = Bundle.main.infoDictionary?["CFBundleVersion"] as? String,
202
+ let versionName = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String else {
203
+ return
204
+ }
205
+ let prefs = UserDefaults.standard
206
+ prefs.set(versionCode, forKey: "lastBinaryVersionCode")
207
+ prefs.set(versionName, forKey: "lastBinaryVersionName")
208
+ prefs.set("", forKey: "serverBasePath")
209
+ prefs.synchronize()
210
+ }
211
+
212
+ private func fatalLoadError() -> Never {
213
+ printLoadError()
214
+ exit(1)
215
+ }
216
+
217
+ private func printLoadError() {
218
+ let fullStartPath = capacitorBridge.config.appStartFileURL.path
219
+
220
+ CAPLog.print("⚡️ ERROR: Unable to load \(fullStartPath)")
221
+ CAPLog.print("⚡️ This file is the root of your web app and must exist before")
222
+ CAPLog.print("⚡️ Capacitor can run. Ensure you've run capacitor copy at least")
223
+ CAPLog.print("⚡️ or, if embedding, that this directory exists as a resource directory.")
224
+ }
225
+ }
226
+
227
+ extension CAPWebView: CAPBridgeDelegate {
228
+ internal var bridgedWebView: WKWebView? {
229
+ return webView
230
+ }
231
+
232
+ internal var bridgedViewController: UIViewController? {
233
+ // search for the parent view controller
234
+ var object = self.next
235
+ while !(object is UIViewController) && object != nil {
236
+ object = object?.next
237
+ }
238
+ return object as? UIViewController
239
+ }
240
+ }
@@ -125,6 +125,15 @@ public protocol JSValueContainer: JSStringContainer, JSBoolContainer, JSIntConta
125
125
  }
126
126
 
127
127
  extension JSValueContainer {
128
+ public func getValue(_ key: String) -> JSValue? {
129
+ return jsObjectRepresentation[key]
130
+ }
131
+
132
+ @available(*, message: "All values returned conform to JSValue, use getValue(_:) instead.", renamed: "getValue(_:)")
133
+ public func getAny(_ key: String) -> Any? {
134
+ return getValue(key)
135
+ }
136
+
128
137
  public func getString(_ key: String) -> String? {
129
138
  return jsObjectRepresentation[key] as? String
130
139
  }
@@ -1,20 +1,8 @@
1
1
  import UIKit
2
2
 
3
3
  internal class TmpViewController: UIViewController {
4
- var count = 0
5
4
  override func viewDidAppear(_ animated: Bool) {
6
5
  super.viewDidAppear(animated)
7
- // On iOS 12 viewDidAppear is called when TmpViewController is created and
8
- // when the presented VC gets dismissed.
9
- // On iOS 13 only when the presented VC gets dismissed.
10
- // We only want to send the notification when the presented VC gets dismissed.
11
- if #available(iOS 13, *) {
12
- count += 2
13
- } else {
14
- count += 1
15
- }
16
- if count > 1 {
17
- NotificationCenter.default.post(CapacitorBridge.tmpVCAppeared)
18
- }
6
+ NotificationCenter.default.post(CapacitorBridge.tmpVCAppeared)
19
7
  }
20
8
  }
@@ -102,7 +102,12 @@ internal class WebViewDelegationHandler: NSObject, WKNavigationDelegate, WKUIDel
102
102
 
103
103
  // otherwise, is this a new window or a main frame navigation but to an outside source
104
104
  let toplevelNavigation = (navigationAction.targetFrame == nil || navigationAction.targetFrame?.isMainFrame == true)
105
- if navURL.absoluteString.contains(bridge.config.serverURL.absoluteString) == false, toplevelNavigation {
105
+
106
+ // Check if the url being navigated to is configured as an application url (whether local or remote)
107
+ let isApplicationNavigation = navURL.absoluteString.starts(with: bridge.config.serverURL.absoluteString) ||
108
+ navURL.absoluteString.starts(with: bridge.config.localURL.absoluteString)
109
+
110
+ if !isApplicationNavigation, toplevelNavigation {
106
111
  // disallow and let the system handle it
107
112
  if UIApplication.shared.applicationState == .active {
108
113
  UIApplication.shared.open(navURL, options: [:], completionHandler: nil)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capacitor/ios",
3
- "version": "4.0.0-beta.1",
3
+ "version": "4.0.0-beta.2",
4
4
  "description": "Capacitor: Cross-platform apps with JavaScript and the web",
5
5
  "homepage": "https://capacitorjs.com",
6
6
  "author": "Ionic Team <hi@ionic.io> (https://ionic.io)",
@@ -29,5 +29,5 @@
29
29
  "publishConfig": {
30
30
  "access": "public"
31
31
  },
32
- "gitHead": "8520516c0883cb11df3b0097ac793290235c27de"
32
+ "gitHead": "902e1f56af32143b817cf03233264203bcd5adab"
33
33
  }