无法构造委托,因为它没有可访问的初始值设定项?

Delegate cannot be constructed because it has no accessible initializers?

所以我有一个 class 具有发送 SMS 消息的功能。我尝试使 class 符合委托协议...它最终导致 class 需要一个类型别名,我认为它是 MFMessageViewController 类型,但没有用

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)
    }
}

我认为您应该看一看 here 以更深入地了解 swift 中的委派和协议。您无法实例化协议 - 您需要一个符合该协议的 class 来完成您想要的。

我认为你的 class 应该是这样的:

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)
    }
}

演示解决方案的外观在很大程度上取决于您的需求。 (IMO 这段代码应该放在 UIViewController

如果有帮助请告诉我!

如果你的项目使用 SwiftUI 那么你可以使用 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>) {
        
    }
}

用法:

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
            }
        }
    }
}


可能的另一种解决方案。您可以创建一个单例 class 并在根控制器上呈现 MFMessageComposeViewController。 像这样

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)
    }
}

用法:

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