Building an iOS SMS Spam Filter App with CoreML
This tutorial walks through creating a custom iOS SMS spam filter app, covering extraction of personal SMS data from an iPhone backup, training a CoreML text‑classification model with CreateML, implementing a Message Filter Extension in Xcode, and exploring advanced update strategies.
This article provides a comprehensive tutorial on building a custom iOS SMS spam filter app. The author documents the complete development process from start to finish.
Part 1: Obtaining SMS Training Data
The first challenge was obtaining spam SMS samples for training. Since no public dataset was available, the author used personal SMS messages from their iPhone. The article explains how to export SMS from iPhone backup: create an unencrypted backup in Finder, locate the backup folder, find the database file named 3d0d7e5fb2ce2888e13d06e4d46c395e047a3d28 , copy it to a separate location, and open it with SQLite database software like SQLPro for SQLite. The author provides the SQL query to extract messages:
SELECT datetime(message.date, 'unixepoch', '+31 years', '-6 hours') as Timestamp, handle.id, message.text, case when message.is_from_me then 'From me' else 'To me' end as Sender FROM message, handle WHERE message.handle_id = handle.ROWID AND message.text NOT NULL;
Part 2: Training CoreML Text Classification Model
The article details how to use Apple's CreateML tool to train a text classification model. The supported data formats are JSON and CSV, with each entry containing "text" and "label" fields. The author explains the filter categories supported by iOS Message Filtering: none (insufficient information), allow (normal messages), junk (spam), promotion (promotional messages), and transaction (transactional messages). Each category can have sub-categories. For training, the author used four labels: allow, junk, promotion, and transaction.
Part 3: Message Filter App Extension Development
The article explains how to create a Message Filter Extension target in Xcode and implement the filtering logic. Key implementation details include:
1. Adding the Message Filter Extension target to the project
2. Importing the trained CoreML model into both the main app and extension targets
3. Implementing the filter logic using the model:
import Foundation import IdentityLookup import CoreML enum SMSFilterActionType: String { case transation case promotion case allow case junk func formatFilterAction() -> ILMessageFilterAction { switch self { case .transation: return ILMessageFilterAction.transaction case .promotion: return ILMessageFilterAction.promotion case .allow: return ILMessageFilterAction.allow case .junk: return ILMessageFilterAction.junk } } } struct SMSFilterUtil { static func filter(with messageBody: String) -> ILMessageFilterAction { var filterAction: ILMessageFilterAction = .none let configuration = MLModelConfiguration() do { let model = try SmsClassifier(configuration: configuration) let resultLabel = try model.prediction(text: messageBody).label if let resultFilterAction = SMSFilterActionType(rawValue: resultLabel)?.formatFilterAction() { filterAction = resultFilterAction } } catch { print(error) } return filterAction } }
4. Calling the filter in the extension's offlineAction method:
@available(iOSApplicationExtension 16.0, *) private func offlineAction(for queryRequest: ILMessageFilterQueryRequest) -> (ILMessageFilterAction, ILMessageFilterSubAction) { guard let messageBody = queryRequest.messageBody else { return (.none, .none) } let action = MWSMSFilterUtil.filter(with: messageBody) return (action, .none) }
Part 4: Advanced Considerations and Future Improvements
The article discusses three potential improvement approaches: (1) Training and updating the model within the app using custom rules, (2) A hybrid approach combining local rules, local models, and server-side models, and (3) Dynamic model updates by storing the model in a shared App Group directory and downloading updates.
Sohu Tech Products
A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.