简体   繁体   中英

Delegate cannot be constructed because it has no accessible initializers?

So I have a class that has a function to send an SMS message. I tried making the class conform to the delegate protocol...it ends up causing the class to require a type alias which I thought would be of type MFMessageViewController but that didn't work

class functions: NSObject, ObservableObject, MFMessageComposeViewControllerDelegate {
    func sendInviteMessage(number: String) {
        if MFMessageComposeViewController.canSendText() {
            let message = MFMessageComposeViewController()
            message.messageComposeDelegate = self
            message.recipients = [number]
            message.body = "Hi, I'd like to invite you to join my app google.com"
        
            message.present(message, animated: true)
        }
    }

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
    }
}

I think you should have a look here to get deeper insights of delegation and protocols in swift. You cannot instantiate a protocol - you need a class that conforms to that protocol to accomplish what you want.

I think your class should look like this:

class functions: NSObject, ObservableObject, MFMessageComposeViewControllerDelegate {
    
    func sendInviteMessage() {
    
        if MFMessageComposeViewController.canSendText() {
            let message = MFMessageComposeViewController()
            message.messageComposeDelegate = self
            message.recipients = ["##########"]
            message.body = "Hi, I'd like to invite you to join my app google.com"

            // here we have a problem - You will need a ViewController or something that presents the MessageComposeViewController "message" --> It cannot present itself
            //message.present(message, animated: true)
        }
    }
    
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        controller.dismiss(animated: true)
    }
}

It highly depends on your needs how your solution for the presentation looks like. (IMO this code should be placed in a UIViewController )

Let me know if it helps!

If your project use SwiftUI then you can use UIViewControllerRepresentable

struct MessageComposeViewController: UIViewControllerRepresentable {
    
    var toRecipients: [String]
    var messageBody: String
    
    var didFinish: ()->()
    
    func makeCoordinator() -> Coordinator {
        return Coordinator(self)
    }
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<MessageComposeViewController>) -> MFMessageComposeViewController {
        
        let message = MFMessageComposeViewController()
        message.messageComposeDelegate = context.coordinator
        message.recipients = self.toRecipients
        message.body = self.messageBody
        
        return message
    }
    
    final class Coordinator: NSObject, MFMessageComposeViewControllerDelegate {
        
        var parent: MessageComposeViewController
        
        init(_ controller: MessageComposeViewController) {
            self.parent = controller
        }
        
        func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
            parent.didFinish()
            controller.dismiss(animated: true)
        }
    }
    
    func updateUIViewController(_ uiViewController: MFMessageComposeViewController, context: UIViewControllerRepresentableContext<MessageComposeViewController>) {
        
    }
}

usage:

struct MessageView: View {
    
    @State private var showingMessage = false
    
    var body: some View {
        VStack {
            Button("Open Message") {
                self.showingMessage.toggle()
            }
        }
        .sheet(isPresented: $showingMessage) {
            MessageComposeViewController(toRecipients: ["1234567890"], messageBody: "Hi, I'd like to invite you to join my app google.com") {
                // Did finish action
            }
        }
    }
}


Possible another solution. You can create one singleton class and present MFMessageComposeViewController on the root controller. Like this

class Functions: NSObject, MFMessageComposeViewControllerDelegate {

    static let shared = Functions()
    
    func sendInviteMessage(number: String) {
        if MFMessageComposeViewController.canSendText() {
            
            let message = MFMessageComposeViewController()
            message.messageComposeDelegate = self
            message.recipients = [number]
            message.body = "Hi, I'd like to invite you to join my app google.com"
            
            UIApplication.shared.windows.first?.rootViewController?.present(message, animated: true)
        }
    }
    
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        controller.dismiss(animated: true)
    }
}

usage:

struct MessageView: View {
    var body: some View {
        Button("Open Message") {
            Functions.shared.sendInviteMessage(number: "12345678")
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM