Skip to content

Commit 372ab99

Browse files
camfrandsenFMorschel
authored andcommitted
[webview_flutter_wkwebview] Expose the allowsLinkPreview property in WKWebView for iOS (flutter#5029)
Based off of flutter/plugins#5110 So most of the credit goes to @b099l3 Also exposed it through the webview_controller This PR fixes this issue: - flutter/flutter#69748 - (duplicate) flutter/flutter#100783
1 parent 77ff740 commit 372ab99

12 files changed

Lines changed: 369 additions & 151 deletions

File tree

packages/webview_flutter/webview_flutter_wkwebview/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 3.20.0
2+
3+
* Adds support to disable a preview of the destination for a link. See
4+
`WebKitWebViewController.setAllowsLinkPreview`.
5+
16
## 3.19.0
27

38
* Adds support to set the over-scroll mode for the WebView. See `WebKitWebViewController.setOverScrollMode`.

packages/webview_flutter/webview_flutter_wkwebview/darwin/Tests/WebViewProxyAPITests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,18 @@ class WebViewProxyAPITests: XCTestCase {
278278
XCTAssertFalse(instance.isInspectable)
279279
}
280280

281+
@MainActor func testSetAllowsLinkPreview() {
282+
let registrar = TestProxyApiRegistrar()
283+
let api = webViewProxyAPI(forRegistrar: registrar)
284+
285+
let instance = TestViewWKWebView()
286+
let allow: Bool = true
287+
try? api.pigeonDelegate.setAllowsLinkPreview(
288+
pigeonApi: api, pigeonInstance: instance, allow: allow)
289+
290+
XCTAssertEqual(instance.allowsLinkPreview, allow)
291+
}
292+
281293
@MainActor func testGetCustomUserAgent() {
282294
let registrar = TestProxyApiRegistrar()
283295
let api = webViewProxyAPI(forRegistrar: registrar)

packages/webview_flutter/webview_flutter_wkwebview/darwin/webview_flutter_wkwebview/Sources/webview_flutter_wkwebview/WebKitLibrary.g.swift

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v25.2.0), do not edit directly.
4+
// Autogenerated from Pigeon (v25.3.0), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66

77
import Foundation
@@ -77,6 +77,7 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
7777
if value is NSNull { return nil }
7878
return value as! T?
7979
}
80+
8081
/// Handles the callback when an object is deallocated.
8182
protocol WebKitLibraryPigeonInternalFinalizerDelegate: AnyObject {
8283
/// Invoked when the strong reference of an object is deallocated in an `InstanceManager`.
@@ -4774,6 +4775,14 @@ protocol PigeonApiDelegateUIViewWKWebView {
47744775
func getCustomUserAgent(pigeonApi: PigeonApiUIViewWKWebView, pigeonInstance: WKWebView) throws
47754776
-> String?
47764777
#endif
4778+
#if !os(macOS)
4779+
/// Whether to allow previews for link destinations and detected data such as
4780+
/// addresses and phone numbers.
4781+
///
4782+
/// Defaults to true.
4783+
func setAllowsLinkPreview(
4784+
pigeonApi: PigeonApiUIViewWKWebView, pigeonInstance: WKWebView, allow: Bool) throws
4785+
#endif
47774786
}
47784787

47794788
protocol PigeonApiProtocolUIViewWKWebView {
@@ -5272,6 +5281,27 @@ final class PigeonApiUIViewWKWebView: PigeonApiProtocolUIViewWKWebView {
52725281
getCustomUserAgentChannel.setMessageHandler(nil)
52735282
}
52745283
#endif
5284+
#if !os(macOS)
5285+
let setAllowsLinkPreviewChannel = FlutterBasicMessageChannel(
5286+
name: "dev.flutter.pigeon.webview_flutter_wkwebview.UIViewWKWebView.setAllowsLinkPreview",
5287+
binaryMessenger: binaryMessenger, codec: codec)
5288+
if let api = api {
5289+
setAllowsLinkPreviewChannel.setMessageHandler { message, reply in
5290+
let args = message as! [Any?]
5291+
let pigeonInstanceArg = args[0] as! WKWebView
5292+
let allowArg = args[1] as! Bool
5293+
do {
5294+
try api.pigeonDelegate.setAllowsLinkPreview(
5295+
pigeonApi: api, pigeonInstance: pigeonInstanceArg, allow: allowArg)
5296+
reply(wrapResult(nil))
5297+
} catch {
5298+
reply(wrapError(error))
5299+
}
5300+
}
5301+
} else {
5302+
setAllowsLinkPreviewChannel.setMessageHandler(nil)
5303+
}
5304+
#endif
52755305
}
52765306

52775307
#if !os(macOS)
@@ -5424,6 +5454,14 @@ protocol PigeonApiDelegateNSViewWKWebView {
54245454
func getCustomUserAgent(pigeonApi: PigeonApiNSViewWKWebView, pigeonInstance: WKWebView) throws
54255455
-> String?
54265456
#endif
5457+
#if !os(iOS)
5458+
/// Whether to allow previews for link destinations and detected data such as
5459+
/// addresses and phone numbers.
5460+
///
5461+
/// Defaults to true.
5462+
func setAllowsLinkPreview(
5463+
pigeonApi: PigeonApiNSViewWKWebView, pigeonInstance: WKWebView, allow: Bool) throws
5464+
#endif
54275465
}
54285466

54295467
protocol PigeonApiProtocolNSViewWKWebView {
@@ -5900,6 +5938,27 @@ final class PigeonApiNSViewWKWebView: PigeonApiProtocolNSViewWKWebView {
59005938
getCustomUserAgentChannel.setMessageHandler(nil)
59015939
}
59025940
#endif
5941+
#if !os(iOS)
5942+
let setAllowsLinkPreviewChannel = FlutterBasicMessageChannel(
5943+
name: "dev.flutter.pigeon.webview_flutter_wkwebview.NSViewWKWebView.setAllowsLinkPreview",
5944+
binaryMessenger: binaryMessenger, codec: codec)
5945+
if let api = api {
5946+
setAllowsLinkPreviewChannel.setMessageHandler { message, reply in
5947+
let args = message as! [Any?]
5948+
let pigeonInstanceArg = args[0] as! WKWebView
5949+
let allowArg = args[1] as! Bool
5950+
do {
5951+
try api.pigeonDelegate.setAllowsLinkPreview(
5952+
pigeonApi: api, pigeonInstance: pigeonInstanceArg, allow: allowArg)
5953+
reply(wrapResult(nil))
5954+
} catch {
5955+
reply(wrapError(error))
5956+
}
5957+
}
5958+
} else {
5959+
setAllowsLinkPreviewChannel.setMessageHandler(nil)
5960+
}
5961+
#endif
59035962
}
59045963

59055964
#if !os(iOS)

packages/webview_flutter/webview_flutter_wkwebview/darwin/webview_flutter_wkwebview/Sources/webview_flutter_wkwebview/WebViewProxyAPIDelegate.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,17 @@ class WebViewProxyAPIDelegate: PigeonApiDelegateWKWebView, PigeonApiDelegateUIVi
380380
return try getCustomUserAgent(
381381
pigeonApi: getUIViewWKWebViewAPI(pigeonApi), pigeonInstance: pigeonInstance)
382382
}
383+
384+
func setAllowsLinkPreview(
385+
pigeonApi: PigeonApiUIViewWKWebView, pigeonInstance: WKWebView, allow: Bool
386+
) throws {
387+
pigeonInstance.allowsLinkPreview = allow
388+
}
389+
390+
func setAllowsLinkPreview(
391+
pigeonApi: PigeonApiNSViewWKWebView, pigeonInstance: WKWebView, allow: Bool
392+
) throws {
393+
try setAllowsLinkPreview(
394+
pigeonApi: getUIViewWKWebViewAPI(pigeonApi), pigeonInstance: pigeonInstance, allow: allow)
395+
}
383396
}

packages/webview_flutter/webview_flutter_wkwebview/lib/src/common/platform_webview.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,20 @@ class PlatformWebView {
397397

398398
throw UnimplementedError('${webView.runtimeType} is not supported.');
399399
}
400+
401+
/// Whether to allow previews for link destinations and detected data such as
402+
/// addresses and phone numbers.
403+
///
404+
/// Defaults to true.
405+
Future<void> setAllowsLinkPreview(bool allow) {
406+
final WKWebView webView = nativeWebView;
407+
switch (webView) {
408+
case UIViewWKWebView():
409+
return webView.setAllowsLinkPreview(allow);
410+
case NSViewWKWebView():
411+
return webView.setAllowsLinkPreview(allow);
412+
}
413+
414+
throw UnimplementedError('${webView.runtimeType} is not supported.');
415+
}
400416
}

packages/webview_flutter/webview_flutter_wkwebview/lib/src/common/web_kit.g.dart

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2013 The Flutter Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
4-
// Autogenerated from Pigeon (v25.2.0), do not edit directly.
4+
// Autogenerated from Pigeon (v25.3.0), do not edit directly.
55
// See also: https://pub.dev/packages/pigeon
66
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import, no_leading_underscores_for_local_identifiers
77

@@ -5822,6 +5822,39 @@ class UIViewWKWebView extends UIView implements WKWebView {
58225822
}
58235823
}
58245824

5825+
/// Whether to allow previews for link destinations and detected data such as
5826+
/// addresses and phone numbers.
5827+
///
5828+
/// Defaults to true.
5829+
Future<void> setAllowsLinkPreview(bool allow) async {
5830+
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
5831+
_pigeonVar_codecUIViewWKWebView;
5832+
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
5833+
const String pigeonVar_channelName =
5834+
'dev.flutter.pigeon.webview_flutter_wkwebview.UIViewWKWebView.setAllowsLinkPreview';
5835+
final BasicMessageChannel<Object?> pigeonVar_channel =
5836+
BasicMessageChannel<Object?>(
5837+
pigeonVar_channelName,
5838+
pigeonChannelCodec,
5839+
binaryMessenger: pigeonVar_binaryMessenger,
5840+
);
5841+
final Future<Object?> pigeonVar_sendFuture =
5842+
pigeonVar_channel.send(<Object?>[this, allow]);
5843+
final List<Object?>? pigeonVar_replyList =
5844+
await pigeonVar_sendFuture as List<Object?>?;
5845+
if (pigeonVar_replyList == null) {
5846+
throw _createConnectionError(pigeonVar_channelName);
5847+
} else if (pigeonVar_replyList.length > 1) {
5848+
throw PlatformException(
5849+
code: pigeonVar_replyList[0]! as String,
5850+
message: pigeonVar_replyList[1] as String?,
5851+
details: pigeonVar_replyList[2],
5852+
);
5853+
} else {
5854+
return;
5855+
}
5856+
}
5857+
58255858
@override
58265859
UIViewWKWebView pigeon_copy() {
58275860
return UIViewWKWebView.pigeon_detached(
@@ -6577,6 +6610,39 @@ class NSViewWKWebView extends NSObject implements WKWebView {
65776610
}
65786611
}
65796612

6613+
/// Whether to allow previews for link destinations and detected data such as
6614+
/// addresses and phone numbers.
6615+
///
6616+
/// Defaults to true.
6617+
Future<void> setAllowsLinkPreview(bool allow) async {
6618+
final _PigeonInternalProxyApiBaseCodec pigeonChannelCodec =
6619+
_pigeonVar_codecNSViewWKWebView;
6620+
final BinaryMessenger? pigeonVar_binaryMessenger = pigeon_binaryMessenger;
6621+
const String pigeonVar_channelName =
6622+
'dev.flutter.pigeon.webview_flutter_wkwebview.NSViewWKWebView.setAllowsLinkPreview';
6623+
final BasicMessageChannel<Object?> pigeonVar_channel =
6624+
BasicMessageChannel<Object?>(
6625+
pigeonVar_channelName,
6626+
pigeonChannelCodec,
6627+
binaryMessenger: pigeonVar_binaryMessenger,
6628+
);
6629+
final Future<Object?> pigeonVar_sendFuture =
6630+
pigeonVar_channel.send(<Object?>[this, allow]);
6631+
final List<Object?>? pigeonVar_replyList =
6632+
await pigeonVar_sendFuture as List<Object?>?;
6633+
if (pigeonVar_replyList == null) {
6634+
throw _createConnectionError(pigeonVar_channelName);
6635+
} else if (pigeonVar_replyList.length > 1) {
6636+
throw PlatformException(
6637+
code: pigeonVar_replyList[0]! as String,
6638+
message: pigeonVar_replyList[1] as String?,
6639+
details: pigeonVar_replyList[2],
6640+
);
6641+
} else {
6642+
return;
6643+
}
6644+
}
6645+
65806646
@override
65816647
NSViewWKWebView pigeon_copy() {
65826648
return NSViewWKWebView.pigeon_detached(

0 commit comments

Comments
 (0)