Swift World Time Integration
For iOS apps, relying on the user's device time can lead to errors in time-sensitive features (like auctions or countdowns). Use `URLSession` to get the server time.
SWIFT
import Foundation
struct TimeResponse: Codable {
let datetime: String
let timezone: String
let unixtime: Int
}
func fetchTime() {
let url = URL(string: "https://time.now/developer/api/timezone/Europe/Paris")!
URLSession.shared.dataTask(with: url) { data, _, _ in
if let data = data {
if let response = try? JSONDecoder().decode(TimeResponse.self, from: data) {
print("Paris Time: \(response.datetime)")
}
}
}.resume()
}
Why use Time.Now for Mobile?
- Type-safe with Codable
- Prevents device-time cheating
- Works on iOS/macOS/watchOS