Skip to content

Instantly share code, notes, and snippets.

@1998code
Created July 5, 2024 12:16
Show Gist options
  • Save 1998code/0327f7a6cdbc3373689669f630623a33 to your computer and use it in GitHub Desktop.
Save 1998code/0327f7a6cdbc3373689669f630623a33 to your computer and use it in GitHub Desktop.
Email Block List (SwiftUI)
//
// ContentView.swift
// Email Block List
//
// Created by Ming on 5/7/2024.
//
import SwiftUI
struct ContentView: View {
@State private var email = ""
@State private var status = ""
var body: some View {
NavigationStack {
VStack(alignment: .leading) {
TextField("Email", text: $email)
.textFieldStyle(RoundedBorderTextFieldStyle())
.onChange(of: email) { newValue, _ in
checkBlockList(email: newValue) { isBlocked in
if isBlocked {
status = "Blocked: \(email.split(separator: "@")[1])"
} else {
status = "Safe to continue"
}
}
}
if status != "" {
Label(status, systemImage: status == "Blocked" ? "xmark.circle.fill" : "checkmark.circle.fill")
.foregroundColor(status.contains("Blocked") ? .red : .green)
}
Spacer()
}.padding(.horizontal)
.navigationTitle("Register")
.toolbar {
ToolbarItemGroup(placement: .navigationBarLeading) {
Label("Input your email", systemImage: "envelope")
.labelStyle(.titleAndIcon)
}
}
}
}
func checkBlockList(email: String, completion: @escaping (Bool) -> Void) {
let url = URL(string: "https://raw.githubusercontent.com/disposable-email-domains/disposable-email-domains/master/disposable_email_blocklist.conf")!
let task = URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data {
if let string = String(data: data, encoding: .utf8) {
let lines = string.components(separatedBy: "\n")
for line in lines {
if email.contains(line) {
completion(true)
return
}
}
}
}
completion(false)
}
task.resume()
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment