iosswiftiphonexcodemfmessagecomposeviewcontroller

MFMessageComposeViewController - disable adding/editing of the recipients?


I know this is an old question and I have read in the documents that we cannot customise the UI of the MFMessageController/MFMailController and doing such can increase the chance of the flagging of the applications from the AppStore if they found me using the private APIS's. So right now what I am doing is just for exploring the private api's

But still I followed some documents and I tried to do the following code by using some private API's

@IBAction func sendSmsClick(_ sender: AnyObject) {
    guard MFMessageComposeViewController.canSendText() else {
        return
    }
    
    let messageController = MFMessageComposeViewController()
    messageController body = "fhfhfkjhfdgggefgregfhrfhfrhggejhvghjkgevhjg"
    messageController.recipients = ["64688098"]
    messageController.messageComposeDelegate = self;
    
    
    // Disabling the recepient editing - not working
    // Here the _setCanEditRecipients: is a private API
    let disableRecepients = "_setCanEditRecipients:"
    print(disableRecepients)
    let selOne = NSSelectorFromString(disableRecepients)
    if messageController.responds(to: selOne) {
        print("Yes")
       messageController.perform(selOne, with: false)
    }
    
    self.present(messageController, animated: true) {
       
    }
    
    
    
}

The private APIs I got from the the list of iVars and Methods of MFMessageComposeViewController here.

https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/MessageUI.framework/MFMessageComposeViewController.h

But still I am able to edit the recipients list even if pass to one of the instance method _setCanEditRecipients which is in the private API . How can I disable the Add button and the Recipients editing in the MFMessageComposer controller? Any ideas will be appreciated

This image


Solution

  • Attempt 1

    MFMessageComposeViewController._setCanEditRecipients() - before presenting the screen to user (same as you tried) - did NOT work.


    Attempt 2

    MFMessageComposeViewController is a UINavigationController subclass and after loading you can access it's viewControllers.

    self.present(messageController, animated: true, completion: {
        let firstVC = messageController.viewControllers.first
        // This is a private class called `CKSMSComposeController` 
    })
    

    CKSMSComposeController.setCanEditRecipients() - after presenting the screen to the user (in the completion handler of presentation) - did NOT work.


    Attempt 3

    Try adding a view over all other views - that covers recepients area and doesn't allow interaction?

    self.present(messageController, animated: true, completion: {
        // Hard-coded frame value, needs to be tested thoroughly
        let view = UIView(frame: CGRect(x: 0, y: 70, width: messageController.view.bounds.width, height: 70))
        // Make alpha 0 after testing (added for showing what it covers)
        view.backgroundColor = UIColor.yellow.withAlphaComponent(0.3)
        view.autoresizingMask = [.flexibleWidth]
        view.isUserInteractionEnabled = false
        messageController.view.addSubview(view)
    })
    

    enter image description here

    It is working as of Xcode 12.5 & iOS 14.6. As always with these hacks, the fix is not reliable and can break in any iOS release.