MS Bookings 不再在 WKWebView 内工作。

huangapple go评论143阅读模式
英文:

MS bookings not working anymore inside WKWebView

问题

WVNavigationDelegate中,我还有一个注入到WebView的JavaScript脚本,用于拦截需要检测预订是否成功的特定调用,但出于简化的目的,我将其省略了,因为问题在没有这段代码的情况下仍然发生。

在Android WebView和其他浏览器中正常工作的情况下,是否还有其他功能(例如cookies)需要配置,以便这些页面正常工作?

请注意,以下只是提供给您的部分翻译,代码部分保持原样:

  1. `WVNavigationDelegate`中,我还有一个注入到WebViewJavaScript脚本,用于拦截一个特定调用,以检测预订是否成功。但是,为了简化问题,我在这里省略了它,因为即使没有这段代码,问题仍然会出现。
  2. Android WebView和其他浏览器中正常工作的情况下,是否还有其他功能(例如cookies)需要配置,以便这些页面正常工作?

希望这有助于您的问题!

英文:

I have a WKWebView in an iOS app using SwiftUI which loads a Microsft bookings web page (I have tested this with multiple booking pages and it happens with any public booking page). This was working until now, MS bookings now displays an error page with a generic error "Something went wrong" and a button to refresh the page (when clicking on this button a sigin page appears). In a Webview on Android or in a 'regular' browser everything works fine. The code looks like this:

  1. import SwiftUI
  2. import WebKit
  3. struct WebView: UIViewRepresentable {
  4. let request: URLRequest
  5. let options: Options
  6. private var navDelegate: WKNavigationDelegate
  7. private var scrollDelegate: UIScrollViewDelegate
  8. init(
  9. request: URLRequest,
  10. options: Options = Options()
  11. ) {
  12. self.request = request
  13. self.content = nil
  14. self.options = options
  15. self.navDelegate = WVNavigationDelegate()
  16. self.scrollDelegate = WVUIScrollViewDelegate(scrollEnabled: options.scrollEnabled)
  17. }
  18. func makeUIView(context: Context) -> WKWebView {
  19. let wv = WKWebView()
  20. wv.navigationDelegate = navDelegate
  21. wv.scrollView.delegate = scrollDelegate
  22. // Seems to make no difference
  23. if #available(iOS 15.4, *) {
  24. wv.configuration.preferences.isSiteSpecificQuirksModeEnabled = true
  25. }
  26. return wv
  27. }
  28. func updateUIView(_ uiView: WKWebView, context: Context) {
  29. uiView.load(req)
  30. uiView.scrollView.isScrollEnabled = options.scrollEnabled
  31. }
  32. struct Options {
  33. var scrollEnabled: Bool = true
  34. var zoomEnabled: Bool = true
  35. }
  36. }
  37. class WVUIScrollViewDelegate: NSObject, UIScrollViewDelegate {
  38. let scrollEnabled: Bool
  39. init(scrollEnabled: Bool) {
  40. self.scrollEnabled = scrollEnabled
  41. }
  42. func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?) {
  43. scrollView.pinchGestureRecognizer?.isEnabled = scrollEnabled
  44. }
  45. }
  46. class WVNavigationDelegate: NSObject, WKNavigationDelegate {
  47. // Test to check if cookies were the issue but didn't make any difference
  48. func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
  49. guard let response = navigationResponse.response as? HTTPURLResponse, let url = navigationResponse.response.url else {
  50. decisionHandler(.cancel)
  51. return
  52. }
  53. if let headerFields = response.allHeaderFields as? [String: String] {
  54. let cookies = HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: url)
  55. cookies.forEach { cookie in
  56. webView.configuration.websiteDataStore.httpCookieStore.setCookie(cookie)
  57. HTTPCookieStorage.shared.setCookie(cookie)
  58. }
  59. }
  60. decisionHandler(.allow)
  61. }
  62. }

In the WVNavigationDelegate I also have a js script injected in the webview to intercept a specific call needed to detect when the booking is made but I left i out for simplicity since the problem also occurs without this code.

Are there other functionalities like the cookies I have to configure since those pages are working in Android WebViews and other browsers?

答案1

得分: 1

我遇到了相同的问题。在大致相同的时间发生。

其他人也遇到了类似的问题:https://learn.microsoft.com/en-us/answers/questions/1195010/ios-webkit-wkwebview-is-not-supported-to-access-th

在找到解决方案之前,我的临时解决方法是使用安装的默认浏览器在应用程序之外打开特定的URL并关闭WebView视图。

解决方法的代码如下:

  1. if let bookingURL = navigationAction.request.url,
  2. bookingURL.host == "outlook.office365.com" {
  3. closeFunction?() // 关闭WebView视图
  4. UIApplication.shared.open(bookingURL)
  5. }

如果我有更好或真正的解决方案,我将在这里发布。

英文:

I'm having the same issue. Occurred around the same time.

Other people having similar issues: https://learn.microsoft.com/en-us/answers/questions/1195010/ios-webkit-wkwebview-is-not-supported-to-access-th

My workaround, until there is a solution, is to open this specific URL with the installed default browser outside the app and dismiss the WebView sheet therefore.

Workaround Code looks like this:

  1. if let bookingURL = navigationAction.request.url,
  2. bookingURL.host == "outlook.office365.com" {
  3. closeFunction?() // dismiss WebView-Sheet
  4. UIApplication.shared.open(bookingURL)
  5. }

If I have a better or real solution, I will post it here.

huangapple
  • 本文由 发表于 2023年8月9日 17:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866199-2.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定