Skip to content

Commit

Permalink
[iOS] Use custom method to retrieve web view user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Oct 9, 2024
1 parent b972b0e commit 0342b94
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import UIKit
import Flutter
import WebKit

@main
@objc class AppDelegate: FlutterAppDelegate {

var webView: WKWebView?

override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
Expand All @@ -26,10 +30,21 @@ import Flutter
public func methodChannelHandler(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
debugPrint(call.method)
switch call.method {
case "getWebViewUserAgent":
let userAgent = getWebViewUserAgent()
result(userAgent)
default:
result(FlutterMethodNotImplemented);
return
}
result(nil);
}

private func getWebViewUserAgent() -> String? {
let webConfiguration = WKWebViewConfiguration()
if (webView == nil) {
webView = WKWebView(frame: .zero, configuration: webConfiguration)
}
return webView!.value(forKey: "userAgent") as? String ?? ""
}
}

0 comments on commit 0342b94

Please sign in to comment.