Swift - How to get the Safari app's user agent string or build version programmatically?

92 views Asked by At

Our app is a shopping app that contains an in-app custom browser using WKWebView.

We occasionally have issues with some merchants for things like social auth (Google/Facebook sign in), PayPal checkout, etc.

We have been able to fix this by setting a custom user agent override using the user agent string we get from Safari app (by manually using safari app to go to a website that detects and displays the user agent, then copying that and setting it in our app as the user agent override for the WKWebView).

The only difference between the iOS WKWebView user agent and Safari app one is the end of the string: Safari/604.1.

Example User Agent Strings from same device:

iOS WKWebView User Agent Mozilla/5.0 (iPhone; CPU iPhone OS 17_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3 Mobile/15E148

Safari App User Agent Mozilla/5.0 (iPhone; CPU iPhone OS 17_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3 Mobile/15E148 Safari/604.1

Since the Safari "build version" (currently 604.1) may change for each iOS version, we need a way to dynamically get the correct one for any provided iOS version.

I have been unable to find any Apple API's that can provide this "Build Version". The closest thing I have found so far is parsing the Info.plist for the SafariServices framework, but the build/version numbers it provides don't match what is displayed in the user agent string.

Code

let bundle = Bundle(for: SFSafariViewController.self)
let bundleId = bundle.bundleIdentifier ?? ""
print("Bundle Identifier: \(String(describing: bundleId))")
print("Bundle Info: \(String(describing: bundle.infoDictionary))")

Resulting Info.plist

"DTCompiler": com.apple.compilers.llvm.clang.1_0, 
"CFBundleSupportedPlatforms": <__NSArrayM 0x600000ce2f40>(iPhoneSimulator), 
"DTXcode": 1500, 
"CFBundleExecutable": SafariServices, 
"BuildMachineOSBuild": 22A380017, 
"CFBundleVersion": 8616.1.27.10.13, 
"UIDeviceFamily": <__NSArrayM 0x600000ce2f70>(1,2), 
"CFBundleNumericVersion": 0, 
"CFBundleIdentifier": com.apple.SafariServices, 
"DTPlatformBuild": 21A323, 
"DTXcodeBuild": 15A6160m, 
"MinimumOSVersion": 17.0, 
"DTPlatformVersion": 17.0, 
"CFBundleInfoDictionaryVersion": 6.0, 
"CFBundleName": SafariServices, 
"DTPlatformName": iphonesimulator, 
"DTSDKName": iphonesimulator17.0.internal, 
"CFBundleDevelopmentRegion": en, 
"DTSDKBuild": 21A323, 
"CFBundleSignature": ????, 
"CFBundlePackageType": FMWK, 
"CFBundleShortVersionString": 17.0

So my questions are:

  1. Is there a way to somehow fetch the Safari App's user agent OR build version programmatically in our app?
  2. Or is there another API or tool we can use to provide the device's iOS version and get a response containing the correct Safari app "Build Version"?
0

There are 0 answers