Skip to content

Commit 1df8447

Browse files
authored
[webview_flutter] Convert iOS impl example app to Swift (#13538)
Converts the remaining Obj-C code in `webview_flutter_ios/example/ios` to Swift: - Converts the AppDelegate to Swift, using the repo's standard boilerplate file - Removes `main.m` in favor of the annotation on the app delegate - Adds a bridging header to access the generated registrant - Converts the XCUITest to Swift The XCUITest remains disabled for flakiness, but I tested locally that I could see it pass locally with the skip removed. Missed piece of flutter/flutter#119110 ## Pre-Review Checklist [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 51c71be commit 1df8447

7 files changed

Lines changed: 126 additions & 155 deletions

File tree

‎packages/webview_flutter/webview_flutter_wkwebview/example/ios/Runner.xcodeproj/project.pbxproj‎

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
archiveVersion = 1;
44
classes = {
55
};
6-
objectVersion = 54;
6+
objectVersion = 60;
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
00F6627730996A6B00A7E716 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 00F6627630996A6400A7E716 /* AppDelegate.swift */; };
1011
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
1112
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
1213
78A318202AECB46A00862997 /* FlutterGeneratedPluginSwiftPackage in Frameworks */ = {isa = PBXBuildFile; productRef = 78A3181F2AECB46A00862997 /* FlutterGeneratedPluginSwiftPackage */; };
@@ -48,12 +49,10 @@
4849
8FEC64862DA2C6DC00C48569 /* WebpagePreferencesProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FEC64842DA2C6DC00C48569 /* WebpagePreferencesProxyAPITests.swift */; };
4950
8FEC64872DA2C6DC00C48569 /* SecCertificateProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FEC64822DA2C6DC00C48569 /* SecCertificateProxyAPITests.swift */; };
5051
8FEC64882DA2C6DC00C48569 /* SecTrustProxyAPITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8FEC64832DA2C6DC00C48569 /* SecTrustProxyAPITests.swift */; };
51-
978B8F6F1D3862AE00F588F7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */; };
52-
97C146F31CF9000F007C117D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 97C146F21CF9000F007C117D /* main.m */; };
5352
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
5453
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
5554
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
56-
F7151F77266057800028CB91 /* FLTWebViewUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = F7151F76266057800028CB91 /* FLTWebViewUITests.m */; };
55+
F7151F77266057800028CB91 /* WebViewUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7151F76266057800028CB91 /* WebViewUITests.swift */; };
5756
/* End PBXBuildFile section */
5857

5958
/* Begin PBXContainerItemProxy section */
@@ -87,6 +86,8 @@
8786
/* End PBXCopyFilesBuildPhase section */
8887

8988
/* Begin PBXFileReference section */
89+
00F6627630996A6400A7E716 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
90+
00F6627830996ACF00A7E716 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
9091
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
9192
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
9293
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
@@ -96,8 +97,6 @@
9697
78DABEA22ED26510000E7860 /* webview_flutter_wkwebview */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = webview_flutter_wkwebview; path = ../../darwin/webview_flutter_wkwebview; sourceTree = "<group>"; };
9798
78E0A7A72DC9AD7400C4905E /* FlutterGeneratedPluginSwiftPackage */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = FlutterGeneratedPluginSwiftPackage; path = Flutter/ephemeral/Packages/FlutterGeneratedPluginSwiftPackage; sourceTree = "<group>"; };
9899
7AFA3C8E1D35360C0083082E /* Release.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Release.xcconfig; path = Flutter/Release.xcconfig; sourceTree = "<group>"; };
99-
7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
100-
7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = "<group>"; };
101100
8F0E23512EEB5D6B002AB342 /* ColorProxyAPITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ColorProxyAPITests.swift; path = ../../darwin/Tests/ColorProxyAPITests.swift; sourceTree = SOURCE_ROOT; };
102101
8F0EDFD22E1F4967001938E6 /* ProxyAPIRegistrarTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ProxyAPIRegistrarTests.swift; path = ../../darwin/Tests/ProxyAPIRegistrarTests.swift; sourceTree = SOURCE_ROOT; };
103102
8F1488C52D2DE27000191744 /* ErrorProxyAPITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ErrorProxyAPITests.swift; path = ../../darwin/Tests/ErrorProxyAPITests.swift; sourceTree = SOURCE_ROOT; };
@@ -139,13 +138,12 @@
139138
9740EEB21CF90195004384FC /* Debug.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Debug.xcconfig; path = Flutter/Debug.xcconfig; sourceTree = "<group>"; };
140139
9740EEB31CF90195004384FC /* Generated.xcconfig */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xcconfig; name = Generated.xcconfig; path = Flutter/Generated.xcconfig; sourceTree = "<group>"; };
141140
97C146EE1CF9000F007C117D /* Runner.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Runner.app; sourceTree = BUILT_PRODUCTS_DIR; };
142-
97C146F21CF9000F007C117D /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
143141
97C146FB1CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
144142
97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
145143
97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
146144
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
147145
F7151F74266057800028CB91 /* RunnerUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
148-
F7151F76266057800028CB91 /* FLTWebViewUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = FLTWebViewUITests.m; sourceTree = "<group>"; };
146+
F7151F76266057800028CB91 /* WebViewUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WebViewUITests.swift; sourceTree = "<group>"; };
149147
F7151F78266057800028CB91 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
150148
/* End PBXFileReference section */
151149

@@ -260,27 +258,18 @@
260258
97C146F01CF9000F007C117D /* Runner */ = {
261259
isa = PBXGroup;
262260
children = (
263-
7AFFD8ED1D35381100E5BB4D /* AppDelegate.h */,
264-
7AFFD8EE1D35381100E5BB4D /* AppDelegate.m */,
261+
00F6627630996A6400A7E716 /* AppDelegate.swift */,
262+
00F6627830996ACF00A7E716 /* Runner-Bridging-Header.h */,
265263
97C146FA1CF9000F007C117D /* Main.storyboard */,
266264
97C146FD1CF9000F007C117D /* Assets.xcassets */,
267265
97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */,
268266
97C147021CF9000F007C117D /* Info.plist */,
269-
97C146F11CF9000F007C117D /* Supporting Files */,
270267
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */,
271268
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */,
272269
);
273270
path = Runner;
274271
sourceTree = "<group>";
275272
};
276-
97C146F11CF9000F007C117D /* Supporting Files */ = {
277-
isa = PBXGroup;
278-
children = (
279-
97C146F21CF9000F007C117D /* main.m */,
280-
);
281-
name = "Supporting Files";
282-
sourceTree = "<group>";
283-
};
284273
B8AEEA11D6ECBD09750349AE /* Pods */ = {
285274
isa = PBXGroup;
286275
children = (
@@ -291,7 +280,7 @@
291280
F7151F75266057800028CB91 /* RunnerUITests */ = {
292281
isa = PBXGroup;
293282
children = (
294-
F7151F76266057800028CB91 /* FLTWebViewUITests.m */,
283+
F7151F76266057800028CB91 /* WebViewUITests.swift */,
295284
F7151F78266057800028CB91 /* Info.plist */,
296285
);
297286
path = RunnerUITests;
@@ -521,8 +510,7 @@
521510
isa = PBXSourcesBuildPhase;
522511
buildActionMask = 2147483647;
523512
files = (
524-
978B8F6F1D3862AE00F588F7 /* AppDelegate.m in Sources */,
525-
97C146F31CF9000F007C117D /* main.m in Sources */,
513+
00F6627730996A6B00A7E716 /* AppDelegate.swift in Sources */,
526514
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */,
527515
);
528516
runOnlyForDeploymentPostprocessing = 0;
@@ -531,7 +519,7 @@
531519
isa = PBXSourcesBuildPhase;
532520
buildActionMask = 2147483647;
533521
files = (
534-
F7151F77266057800028CB91 /* FLTWebViewUITests.m in Sources */,
522+
F7151F77266057800028CB91 /* WebViewUITests.swift in Sources */,
535523
);
536524
runOnlyForDeploymentPostprocessing = 0;
537525
};
@@ -746,9 +734,9 @@
746734
);
747735
PRODUCT_BUNDLE_IDENTIFIER = dev.flutter.plugins.webviewFlutterExample;
748736
PRODUCT_NAME = "$(TARGET_NAME)";
749-
SWIFT_OBJC_BRIDGING_HEADER = "RunnerTests/Runner-Bridging-Header.h";
737+
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
750738
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
751-
SWIFT_VERSION = 6.0;
739+
SWIFT_VERSION = 5.0;
752740
VERSIONING_SYSTEM = "apple-generic";
753741
};
754742
name = Debug;
@@ -777,8 +765,8 @@
777765
);
778766
PRODUCT_BUNDLE_IDENTIFIER = dev.flutter.plugins.webviewFlutterExample;
779767
PRODUCT_NAME = "$(TARGET_NAME)";
780-
SWIFT_OBJC_BRIDGING_HEADER = "RunnerTests/Runner-Bridging-Header.h";
781-
SWIFT_VERSION = 6.0;
768+
SWIFT_OBJC_BRIDGING_HEADER = "Runner/Runner-Bridging-Header.h";
769+
SWIFT_VERSION = 5.0;
782770
VERSIONING_SYSTEM = "apple-generic";
783771
};
784772
name = Release;

‎packages/webview_flutter/webview_flutter_wkwebview/example/ios/Runner/AppDelegate.m‎

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import Flutter
6+
import UIKit
7+
8+
@main
9+
@objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate {
10+
override func application(
11+
_ application: UIApplication,
12+
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
13+
) -> Bool {
14+
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
15+
}
16+
17+
func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) {
18+
GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry)
19+
}
20+
}

packages/webview_flutter/webview_flutter_wkwebview/example/ios/Runner/AppDelegate.h renamed to packages/webview_flutter/webview_flutter_wkwebview/example/ios/Runner/Runner-Bridging-Header.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,4 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#import <Flutter/Flutter.h>
6-
#import <UIKit/UIKit.h>
7-
8-
@interface AppDelegate : FlutterAppDelegate <FlutterImplicitEngineDelegate>
9-
10-
@end
5+
#import "GeneratedPluginRegistrant.h"

‎packages/webview_flutter/webview_flutter_wkwebview/example/ios/Runner/main.m‎

Lines changed: 0 additions & 13 deletions
This file was deleted.

‎packages/webview_flutter/webview_flutter_wkwebview/example/ios/RunnerUITests/FLTWebViewUITests.m‎

Lines changed: 0 additions & 89 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// Copyright 2013 The Flutter Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import XCTest
6+
7+
private func getPixelColorInImage(image: CGImage, x: Int, y: Int) -> UIColor? {
8+
guard let provider = image.dataProvider,
9+
let pixelData = provider.data,
10+
let data = CFDataGetBytePtr(pixelData)
11+
else {
12+
return nil
13+
}
14+
15+
// `data` is an unsafe pointer, so ensure its target stays valid.
16+
return withExtendedLifetime(pixelData) {
17+
let pixelInfo = (image.bytesPerRow * y) + (x * 4) // 4 bytes per pixel
18+
guard pixelInfo + 3 < CFDataGetLength(pixelData) else {
19+
return nil
20+
}
21+
let red = data[pixelInfo + 0]
22+
let green = data[pixelInfo + 1]
23+
let blue = data[pixelInfo + 2]
24+
let alpha = data[pixelInfo + 3]
25+
26+
return UIColor(
27+
red: CGFloat(red) / 255.0,
28+
green: CGFloat(green) / 255.0,
29+
blue: CGFloat(blue) / 255.0,
30+
alpha: CGFloat(alpha) / 255.0)
31+
}
32+
}
33+
34+
class WebViewUITests: XCTestCase {
35+
private var app: XCUIApplication!
36+
37+
override func setUp() {
38+
self.continueAfterFailure = false
39+
40+
app = XCUIApplication()
41+
app.launch()
42+
}
43+
44+
func testTransparentBackground() throws {
45+
try XCTSkipIf(true, "Test is flaky. See https://github.com/flutter/flutter/issues/124156")
46+
47+
let menu = app.buttons["Show menu"]
48+
if !menu.waitForExistence(timeout: 30.0) {
49+
XCTFail("Unable to find menu: \(app.debugDescription)")
50+
}
51+
menu.tap()
52+
53+
let transparentBackground = app.buttons["Transparent background example"]
54+
if !transparentBackground.waitForExistence(timeout: 30.0) {
55+
XCTFail("Unable to find Transparent background example: \(app.debugDescription)")
56+
}
57+
transparentBackground.tap()
58+
59+
let transparentBackgroundLoaded = app.webViews.staticTexts["Transparent background test"]
60+
if !transparentBackgroundLoaded.waitForExistence(timeout: 30.0) {
61+
XCTFail("Unable to find Transparent background test: \(app.debugDescription)")
62+
}
63+
64+
let screenshot = XCUIScreen.main.screenshot()
65+
let screenshotCGImage = try XCTUnwrap(screenshot.image.cgImage)
66+
let centerLeftColor = try XCTUnwrap(
67+
getPixelColorInImage(image: screenshotCGImage, x: 0, y: screenshotCGImage.height / 2))
68+
let centerColor = try XCTUnwrap(
69+
getPixelColorInImage(
70+
image: screenshotCGImage, x: screenshotCGImage.width / 2, y: screenshotCGImage.height / 2))
71+
72+
let centerLeftColorSpace = try XCTUnwrap(centerLeftColor.cgColor.colorSpace)
73+
// Flutter Colors.green color : 0xFF4CAF50 -> rgba(76, 175, 80, 1)
74+
// https://github.com/flutter/flutter/blob/f4abaa0735eba4dfd8f33f73363911d63931fe03/packages/flutter/lib/src/material/colors.dart#L1208
75+
// The background color of the webview is : rgba(0, 0, 0, 0.5)
76+
// The expected color is : rgba(38, 87, 40, 1)
77+
let flutterGreenComponents: [CGFloat] = [38.0 / 255.0, 87.0 / 255.0, 40.0 / 255.0, 1.0]
78+
let flutterGreenColor = flutterGreenComponents.withUnsafeBufferPointer { buffer in
79+
CGColor(colorSpace: centerLeftColorSpace, components: buffer.baseAddress!)
80+
}
81+
82+
let redColorComponents: [CGFloat] = [1.0, 0.0, 0.0, 1.0]
83+
let redColor = redColorComponents.withUnsafeBufferPointer { buffer in
84+
CGColor(colorSpace: centerLeftColorSpace, components: buffer.baseAddress!)
85+
}
86+
87+
XCTAssertEqual(flutterGreenColor, centerLeftColor.cgColor)
88+
XCTAssertEqual(redColor, centerColor.cgColor)
89+
}
90+
}

0 commit comments

Comments
 (0)