SpeedOf.Me API - iOS Integration

This example demonstrates how to integrate the SpeedOf.Me speed test API into an iOS app using WKWebView.

How It Works

The SpeedOf.Me API is JavaScript-based, so iOS apps use WKWebView to:

  1. Load an HTML page containing the speed test
  2. Receive results via WKScriptMessageHandler

Files

Prerequisites

Quick Start

1. Add Files to Your Project

Copy speedtest.html and SpeedTestView.swift to your Xcode project.

2. Configure API Credentials

In speedtest.html, replace the placeholder values:

SomApi.account = "YOUR_API_KEY";
SomApi.domainName = "your-domain.com";

3. Use the View

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            SpeedTestView()
        }
    }
}

Architecture

iOS App
   └── SpeedTestView (SwiftUI)
           └── SpeedTestWebView (UIViewRepresentable)
                   └── WKWebView
                           └── speedtest.html
                                   └── SomApi.js
                                           ▼
                           window.webkit.messageHandlers.speedTest.postMessage()
                                           ▼
                           WKScriptMessageHandler.didReceive(message:)

JavaScript-to-Swift Communication

The HTML page sends messages to Swift via:

window.webkit.messageHandlers.speedTest.postMessage({
    type: 'completed',
    data: result
});

Loading Options

Option 1: Bundle (Offline)

if let url = Bundle.main.url(forResource: "speedtest", withExtension: "html") {
    webView.loadFileURL(url, allowingReadAccessTo: url.deletingLastPathComponent())
}

Option 2: Remote Server

if let url = URL(string: "https://your-domain.com/speedtest.html") {
    webView.load(URLRequest(url: url))
}

Configuration Options

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

Production Considerations

  1. API Key Security: Store API keys in Keychain or fetch from server
  2. Network Permissions: Add usage description if required
  3. Background Handling: Speed tests should run in foreground only
  4. Memory: WKWebView manages memory well, but monitor for leaks

Troubleshooting

Links