【问题标题】:Sending message programmatically with swift使用 swift 以编程方式发送消息
【发布时间】:2024-01-07 16:17:01
【问题描述】:

此代码打开消息应用程序并发送消息。不幸的是,消息传递应用程序没有关闭。总是要杀死应用程序。消息应用程序被窃听。取消按钮不起作用。任何帮助表示赞赏。

图 1:这是初始屏幕。可以取消,一切正常

图 2:这是在点击返回后发送消息。 Noice 取消选项是灰色的

图 3:这是崩溃。键盘没了,取消还是灰显,无法关闭这个窗口

import SwiftUI
import MessageUI

struct Services: View {


@Binding var name: String
let urlString = URL (string: "tel://1234567")!

var body: some View {
    
    NavigationView{
        VStack {
            
            VStack(alignment: .leading){
            
            Text("text1" )
            .padding(.vertical, 5.0)                
            }
            
            Spacer()

            HStack {
                Button(action: {
                    
                    UIApplication.shared.open(self.urlString)
                    
                }) {
                    Text("Call phone")
                }
                .frame(minWidth: 0, maxWidth: 150)
                .font(.subheadline)
                
                Button(action: {
                    presentMessageCompose(name: self.name)

                }) {
                    Text("Text phone")
                    .font(.subheadline)
                }
                .frame(minWidth: 0, maxWidth: 150)
            }
            
            Spacer()
        }
        .navigationBarTitle("Title")
    }
}

}

func presentMessageCompose(name:String) {

let messageComposeDelegate = MessageComposeDelegate()

guard MFMessageComposeViewController.canSendText() else {
    return
}
let vc = UIApplication.shared.windows.filter {$0.isKeyWindow}.first?.rootViewController

let composeVC = MFMessageComposeViewController()
composeVC.messageComposeDelegate = messageComposeDelegate
composeVC.recipients = ["1234567"]
composeVC.body = "Message to send to \(name))"
vc?.present(composeVC, animated: true, completion: nil)
}

我必须单独创建这个类:MessageComposeDelegate

 import Foundation
 import MessageUI


 class MessageComposeDelegate: NSObject, MFMessageComposeViewControllerDelegate {
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    // Customize here
    controller.dismiss(animated: true)
}

}

我将打开消息相关的代码移到了一个新的类中:

 import UIKit
 import MessageUI

 class DisplayMessageInterface: UIViewController, MFMessageComposeViewControllerDelegate {
   
func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    switch (result) {
        case .cancelled:
            print("Message was cancelled")
            dismiss(animated: true, completion: nil)
        case .failed:
            print("Message failed")
            dismiss(animated: true, completion: nil)
        case .sent:
            print("Message was sent")
            dismiss(animated: true, completion: nil)
        default:
        break
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func displayMessageInterface() {
    let topVC = topMostController()
    let composeVC = MFMessageComposeViewController()
    composeVC.messageComposeDelegate = self
    
    // Configure the fields of the interface.
    composeVC.recipients = ["1234567"]
    composeVC.body = "I love Swift!"
    
    // Present the view controller modally.
    if MFMessageComposeViewController.canSendText() {
        topVC.present(composeVC, animated: true, completion: nil)
    } else {
        print("Can't send messages.")
    }
}
func topMostController() -> UIViewController {
var topController: UIViewController = UIApplication.shared.keyWindow!.rootViewController!
    while (topController.presentedViewController != nil) {
        topController = topController.presentedViewController!
    }
    return topController
}

}

并将按钮操作更新为:

 Button(action: {
                    let displaymessageinterface = DisplayMessageInterface()
                    
                    displaymessageinterface.displayMessageInterface()
                })

但我仍然遇到同样的问题

当我点击取消按钮时,会出现一个新错误:“线程 1:EXC_BAD_ACCESS (code=1, address=0x1c)”。图4

【问题讨论】:

  • 您不能让邮件应用程序自动发送消息。如果你能在用户不知情的情况下做到这一点,那就太可怕了。
  • 不只是输入垃圾,为什么不解释一下您为尝试解决问题所做的工作?
  • 我不知道如何解决这个问题,因为我不知道为什么代码会与消息应用程序混淆并使其崩溃
  • 此代码无法打开电子邮件。它打开手机拨打电话并且工作正常。它打开短信 iphone 应用程序但崩溃(使用方法 presentmessagecompose)

标签: swift message


【解决方案1】:

您是否添加了messageComposeViewController:didFinishWithResult: 方法来知道用户何时完成撰写消息?

https://www.ioscreator.com/tutorials/send-imessage-ios-tutorial

func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
    switch (result) {
        case .cancelled:
            print("Message was cancelled")
            dismiss(animated: true, completion: nil)
        case .failed:
            print("Message failed")
            dismiss(animated: true, completion: nil)
        case .sent:
            print("Message was sent")
            dismiss(animated: true, completion: nil)
        default:
        break
    }
}

【讨论】:

    【解决方案2】:

    我在这里找到了解决方案

    https://medium.com/@florentmorin/messageui-swiftui-and-uikit-integration-82d91159b0bd

    keywindow 已弃用:

    替换:让 vc = UIApplication.shared.keyWindow?.rootViewController

    with: let vc = UIApplication.shared.windows.filter { $0.isKeyWindow }.first?.rootViewController!

    【讨论】: