Tuesday, November 29, 2022
HomeiOS Developmentswift - Easy methods to make a message filter with subAction for...

swift – Easy methods to make a message filter with subAction for iOS


I’m making a sms message filter for iOS, I need to classify some message with subAction
I returned subAction, However there isn’t any classification in my iPhone(iOS 16), and my app will classify them to promotion or transaction
that is my code:

import IdentityLookup
ultimate class MessageFilterExtension: ILMessageFilterExtension {}

extension MessageFilterExtension: ILMessageFilterQueryHandling, ILMessageFilterCapabilitiesQueryHandling {
    func deal with(_ capabilitiesQueryRequest: ILMessageFilterCapabilitiesQueryRequest, context: ILMessageFilterExtensionContext, completion: @escaping (ILMessageFilterCapabilitiesQueryResponse) -> Void) {
        let response = ILMessageFilterCapabilitiesQueryResponse()

        // TODO: Replace subActions from ILMessageFilterSubAction enum
        completion(response)
    }

    func deal with(_ queryRequest: ILMessageFilterQueryRequest, context: ILMessageFilterExtensionContext, completion: @escaping (ILMessageFilterQueryResponse) -> Void) {
        let (offlineAction, offlineSubAction) = self.offlineAction(for: queryRequest)
        print("====== STARTED ======")
        change offlineAction {
        case .permit, .junk, .promotion, .transaction:
            // Primarily based on offline knowledge, we all know this message ought to both be Allowed, Filtered as Junk, Promotional or Transactional. Ship response instantly.
            let response = ILMessageFilterQueryResponse()
            response.motion = offlineAction
            response.subAction = offlineSubAction

            completion(response)

        case .none:
            // Primarily based on offline knowledge, we have no idea whether or not this message must be Allowed or Filtered. Defer to community.
            // Notice: Deferring requests to community requires the extension goal's Information.plist to comprise a key with a URL to make use of. See documentation for particulars.
            context.deferQueryRequestToNetwork() { (networkResponse, error) in
                let response = ILMessageFilterQueryResponse()
                response.motion = .none
                response.subAction = .none

                if let networkResponse = networkResponse {
                    // If we acquired a community response, parse it to find out an motion to return in our response.
                    (response.motion, response.subAction) = self.networkAction(for: networkResponse)
                } else {
                    NSLog("Error deferring question request to community: (String(describing: error))")
                }

                completion(response)
            }

        @unknown default:
            break
        }
    }

    personal func offlineAction(for queryRequest: ILMessageFilterQueryRequest) -> (ILMessageFilterAction, ILMessageFilterSubAction) {
        // TODO: Change with logic to carry out offline verify whether or not to filter first (if doable).
        NSLog("recived msg: " + queryRequest.messageBody! + " sender: " + queryRequest.sender!)
        
        // reload setting
        SettingManager.getInstance().load()
        
        let whitelistResult = whitelistHandler(sender: queryRequest.sender!)
        if whitelistResult == .permit {
            return (.permit, .none)
        }
        let callingCodeResult = callingCodeHandler(sender: queryRequest.sender!)
        if  callingCodeResult != .none {
            return (callingCodeResult, .none)
        }
        if bombingHandler(msg: queryRequest.messageBody!) {
            return (.junk, .none)
        }
        let keywordResult = keywordHandler(msg: queryRequest.messageBody!)
        if  keywordResult != .none {
            return (keywordResult, .none)
        }
        
        let classificationResult = classifyHandler(msg: queryRequest.messageBody!)
        if classificationResult.0 != .none {
            return (classificationResult.0, classificationResult.1)
        }
        return (.none, .none)

    }


    func classifyHandler(msg: String) -> (ILMessageFilterAction, ILMessageFilterSubAction) {
        NSLog("checking msg's classification")
        let classificationFilters = DataManager.getInstance().getClassificationData()
        for filter in classificationFilters {
            NSLog("checking key phrase: (filter.key phrase)")
            if (msg.comprises(filter.key phrase)) {
                NSLog("msg is in classification record, marked to (filter.sort)")
                return (filter.sort.getAction(), filter.sort.getSubAction())
            }
        }
        NSLog("msg is just not in classification")
        return (.none, .none)
    }

    
    func whitelistHandler(sender: String) -> ILMessageFilterAction {
        ...
        return .none
    }
    
    func callingCodeHandler(sender: String) -> ILMessageFilterAction {
        ...
        return .none
    }
    
    // false: not must filter
    // true: want filter
    func bombingHandler(msg: String) -> Bool {
        ...
        return false
    }
    
    func keywordHandler(msg: String) -> ILMessageFilterAction {
        ...
        return .none
    }
}

I attempted to set response’s promotionalSubActions and transactionSubActions in first deal with perform, However it doesn’t works

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments