For the transfer of information, I used the App Group. It remained to verify the two directions of data transfer:
Transfer data from the main part of the project to the MessageFilterExtension.
Transfer data from MessageFilterExtension to the main part of the project.
The first way worked without problems. But the second did not work out in any way. Here is the part of the code where I write information from the MessageFilterExtension.
extension UserDefaults {
var sender: String? {
get { return self.string(forKey: #function) }
set { self.set(newValue, forKey: #function) }
}
var messageBody: String? {
get { return string(forKey: #function) }
set { set(newValue, forKey: #function) }
}
var blockedNumber: String? {
get { return string(forKey: #function) }
set { set(newValue, forKey: #function) }
}
}
extension MessageFilterExtension: ILMessageFilterQueryHandling {
// ...
private func offlineAction(for queryRequest: ILMessageFilterQueryRequest)
-> ILMessageFilterAction {
if let userDefaults = UserDefaults(suiteName: "group.GUI.TestExtSms2") {
userDefaults.sender = queryRequest.sender ?? "No sender"
userDefaults.messageBody = queryRequest.messageBody ?? "No body"
if let sender = queryRequest.sender,
let blockedNumber = userDefaults.blockedNumber {
return (sender == blockedNumber) ? .filter : .none
}
}
return .none
}
When I entered the desired phone number (blockedNumber), the lock worked. But I could not get data on SMS (sender and messageBody) in the main part of the project.
You just can't.
https://developer.apple.com/documentation/sms_and_call_reporting/sms_and_mms_message_filtering
For privacy reasons, the system handles all communication with your associated server; your Message Filter app extension can't access the network directly.
Also for privacy reasons, your app extension can't write data to containers shared with the containing app.