demande un « numéro de série » (saisi manuellement, car une app App Store ne peut pas lire le vrai numéro de série),
simule l’envoi d’un ordre de blocage au niveau de l’app (pas du device),
et ne déverrouille l’app que si on saisit le code 653521686.
Remarque importante: sur iOS, une app standard ne peut pas verrouiller l’appareil. Ce code crée un « blocage au niveau de l’app » (plein écran) nécessitant le code pour sortir.
Fichier: ContentView.swift
swift
import SwiftUI
final class AppLockManager: ObservableObject {
@Published var isLocked: Bool = false
@Published var serialNumber: String = "" // Saisi par l'utilisateur
@Published var lastSentSerial: String? = nil
@Published var lastSentAt: Date? = nil
@Published var failedAttempts: Int = 0
private let unlockCode = "653521686"
func lockApp() {
// Simulation d'envoi d'un ordre de blocage (ex: POST vers un serveur)
lastSentSerial = serialNumber.isEmpty ? "N/A" : serialNumber
lastSentAt = Date()
isLocked = true
}
func attemptUnlock(code: String) {
if code == unlockCode {
isLocked = false
failedAttempts = 0
} else {
failedAttempts += 1
}
}
func reset() {
isLocked = false
failedAttempts = 0
}
}
struct ContentView: View {
@StateObject private var lockManager = AppLockManager()
var body: some View {
ZStack {
if lockManager.isLocked {
LockScreenView(lockManager: lockManager)
} else {
VStack(spacing: 24) {
Text("Blocage d’appareil (niveau app)")
.font(.title2).bold()
VStack(alignment: .leading, spacing: 8) {
Text("Numéro de série")
.font(.headline)
TextField("Ex: C02XXXXXXXXXX", text: $lockManager.serialNumber)
.textFieldStyle(.roundedBorder)
.autocapitalization(.allCharacters)
.disableAutocorrection(true)
}
Button {
lockManager.lockApp()
} label: {
Text("Envoyer l’ordre de blocage")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
.disabled(lockManager.serialNumber.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
if let serial = lockManager.lastSentSerial, let date = lockManager.lastSentAt {
VStack(alignment: .leading, spacing: 4) {
Text("Dernière commande envoyée:")
Text("SN: \(serial)")
.font(.system(.body, design: .monospaced))
Text("À: \(DateFormatter.localizedString(from: date, dateStyle: .medium, timeStyle: .medium))")
.foregroundStyle(.secondary)
}
.padding()
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 12))
}
Spacer()
}
.padding()
}
}
.animation(.easeInOut, value: lockManager.isLocked)
}
}
struct LockScreenView: View {
@ObservedObject var lockManager: AppLockManager
@State private var inputCode: String = ""
@State private var shake: Bool = false
@FocusState private var codeFieldFocused: Bool
var body: some View {
ZStack {
Color.red.ignoresSafeArea()
VStack(spacing: 24) {
Image(systemName: "lock.fill")
.font(.system(size: 64))
.foregroundStyle(.white)
Text("App verrouillée")
.font(.largeTitle).bold()
.foregroundStyle(.white)
Text("Pour débloquer, saisissez le code.")
.foregroundStyle(.white.opacity(0.9))
VStack(spacing: 12) {
SecureField("Code de déverrouillage", text: $inputCode)
.textContentType(.password)
.keyboardType(.numberPad)
.padding()
.background(.white.opacity(0.15), in: RoundedRectangle(cornerRadius: 12))
.foregroundStyle(.white)
.focused($codeFieldFocused)
.shake($shake)
Button {
codeFieldFocused = false
lockManager.attemptUnlock(code: inputCode)
if lockManager.failedAttempts == 0 {
inputCode = ""
} else {
withAnimation(.default) { shake.toggle() }
DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) { shake = false }
inputCode = ""
}
} label: {
Text("Déverrouiller")
.frame(maxWidth: .infinity)
}
.buttonStyle(.borderedProminent)
if lockManager.failedAttempts > 0 {
Text("Code incorrect. Tentatives: \(lockManager.failedAttempts)")
.foregroundStyle(.white.opacity(0.9))
}
}
.padding(.horizontal)
Spacer()
HStack {
if let serial = lockManager.lastSentSerial, let date = lockManager.lastSentAt {
Text("SN: \(serial)")
.font(.system(.caption, design: .monospaced))
.foregroundStyle(.white.opacity(0.8))
Spacer()
Text(DateFormatter.localizedString(from: date, dateStyle: .none, timeStyle: .short))
.foregroundStyle(.white.opacity(0.8))
}
}
.padding(.horizontal)
.padding(.bottom, 8)
}
.padding()
}
.onAppear {
codeFieldFocused = true
}
}
}
// MARK: - Shake modifier
struct Shake: AnimatableModifier {
var amount: CGFloat = 0
var animatableData: CGFloat {
get { amount }
set { amount = newValue }
}
func body(content: Content) -> some View {
content
.offset(x: amount)
}
}
extension View {
func shake(_ shake: Binding<Bool>) -> some View {
modifier(Shake(amount: shake.wrappedValue ? 10 : 0))
}
}
@main
struct AppLockDemoApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Notes:
Ce code bloque l’app (plein écran) et non l’appareil. Il n’existe pas d’API publique pour verrouiller l’appareil depuis une app standard. Seuls les MDM/Profils supervisés peuvent, côté gestion, envoyer un « Device Lock » (et encore, sous conditions).
Le « numéro de série » est saisi manuellement, car une app App Store ne peut pas lire le vrai serial de l’appareil.