This example demonstrates how to integrate the SpeedOf.Me speed test API into an iOS app using WKWebView.
The SpeedOf.Me API is JavaScript-based, so iOS apps use WKWebView to:
Copy speedtest.html and SpeedTestView.swift to your Xcode project.
In speedtest.html, replace the placeholder values:
SomApi.account = "YOUR_API_KEY";
SomApi.domainName = "your-domain.com";
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
SpeedTestView()
}
}
}
iOS App
└── SpeedTestView (SwiftUI)
└── SpeedTestWebView (UIViewRepresentable)
└── WKWebView
└── speedtest.html
└── SomApi.js
▼
window.webkit.messageHandlers.speedTest.postMessage()
▼
WKScriptMessageHandler.didReceive(message:)
The HTML page sends messages to Swift via:
window.webkit.messageHandlers.speedTest.postMessage({
type: 'completed',
data: result
});
if let url = Bundle.main.url(forResource: "speedtest", withExtension: "html") {
webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
}
if let url = URL(string: "https://your-domain.com/speedtest.html") {
webView.load(URLRequest(url: url))
}
SomApi.config.sustainTime = 4; // 1-8 seconds
SomApi.config.testServerEnabled = true; // Show test server
SomApi.config.userInfoEnabled = true; // Get IP/hostname
SomApi.config.latencyTestEnabled = true; // Include latency test
SomApi.config.uploadTestEnabled = true; // Include upload test