可以在Swift中发送短信

发布于 2025-01-22 20:53:48 字数 4785 浏览 1 评论 0原文

我尝试通过MFMESSAgeComposeViewControllerDelegate发送SMS,但是,每次尝试发送消息时,我都会收到此错误:

    2022-04-20 21:05:16.400980+0200 MyProject[10685:773575] [Presentation] Attempt to present <MFMessageComposeViewController: 0x117839600> on <MyProject.SMSHandler: 0x11570fb50> (from <MyProject.SMSHandler: 0x11570fb50>) whose view is not in the window hierarchy.
2022-04-20 21:05:16.402930+0200 MyProject[10685:773910] [AXRuntimeCommon] Unknown client: MyProject
2022-04-20 21:05:16.404912+0200 MyProject[10685:773790] [AXRuntimeCommon] AX Lookup problem - errorCode:1100 error:Permission denied portName:'com.apple.iphone.axserver' PID:9979 (
    0   AXRuntime                           0x00000001c98e6a9c 7A975793-0055-365F-A61A-C16EF91F55E2 + 338588
    1   AXRuntime                           0x00000001c989be30 _AXGetPortFromCache + 704
    2   AXRuntime                           0x00000001c989d75c AXUIElementPerformFencedActionWithValue + 564
    3   UIKit                               0x000000021473d234 2EAAE3FE-BFB1-3E62-B103-6B14136D43AE + 934452
    4   libdispatch.dylib                   0x0000000104cccc6c _dispatch_call_block_and_release + 32
    5   libdispatch.dylib                   0x0000000104cce7bc _dispatch_client_callout + 20
    6   libdispatch.dylib                   0x0000000104cd68a4 _dispatch_lane_serial_drain + 984
    7   libdispatch.dylib                   0x0000000104cd75e0 _dispatch_lane_invoke + 428
    8   libdispatch.dylib                   0x0000000104ce4168 _dispatch_workloop_worker_thread + 908
    9   libsystem_pthread.dylib             0x000000021440e0bc _pthread_wqthread + 288
    10  libsystem_pthread.dylib             0x000000021440de5c start_wqthread + 8
)
2022-04-20 21:05:16.405381+0200 MyProject[10685:773790] [AXRuntimeCommon] AX Lookup problem - errorCode:1100 error:Permission denied portName:'com.apple.iphone.axserver' PID:9979 (
    0   AXRuntime                           0x00000001c98e6a9c 7A975793-0055-365F-A61A-C16EF91F55E2 + 338588
    1   AXRuntime                           0x00000001c989be30 _AXGetPortFromCache + 704
    2   AXRuntime                           0x00000001c989d75c AXUIElementPerformFencedActionWithValue + 564
    3   UIKit                               0x000000021473d234 2EAAE3FE-BFB1-3E62-B103-6B14136D43AE + 934452
    4   libdispatch.dylib                   0x0000000104cccc6c _dispatch_call_block_and_release + 32
    5   libdispatch.dylib                   0x0000000104cce7bc _dispatch_client_callout + 20
    6   libdispatch.dylib                   0x0000000104cd68a4 _dispatch_lane_serial_drain + 984
    7   libdispatch.dylib                   0x0000000104cd75e0 _dispatch_lane_invoke + 428
    8   libdispatch.dylib                   0x0000000104ce4168 _dispatch_workloop_worker_thread + 908
    9   libsystem_pthread.dylib             0x000000021440e0bc _pthread_wqthread + 288
    10  libsystem_pthread.dylib             0x000000021440de5c start_wqthread + 8
)
2022-04-20 21:05:17.706401+0200 MyProject[10685:773575] [TraitCollection] Class CKBrowserSwitcherViewController overrides the -traitCollection getter, which is not supported. If you're trying to override traits, you must use the appropriate API.

我只是复制并粘贴了一个剪切的代码,这里是:

import UIKit
import MessageUI

class SMSHandler: UIViewController, MFMessageComposeViewControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func send(phoneNumber: String, msg: String) {
        if (MFMessageComposeViewController.canSendText()) {
            let controller = MFMessageComposeViewController()
            controller.body = msg
            controller.recipients = [phoneNumber]
            controller.messageComposeDelegate = self
            self.present(controller, animated: true, completion: nil)
        }
    }
    
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        switch (result.rawValue) {
            case MessageComposeResult.cancelled.rawValue:
            print("Message was cancelled")
            self.dismiss(animated: true, completion: nil)
        case MessageComposeResult.failed.rawValue:
            print("Message failed")
            self.dismiss(animated: true, completion: nil)
        case MessageComposeResult.sent.rawValue:
            print("Message was sent")
            self.dismiss(animated: true, completion: nil)
        default:
            break;
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = false
    }
}

我还查看看来他们被弃用了...它说您应该使用self.presentviewController(...),当我键入它时,Xcode告诉我将其更改为self.present(...)。有什么新方法可以做到吗? 任何帮助!

I've tried to send an SMS through the MFMessageComposeViewControllerDelegate, however, every time I try to send a message, I get this error:

    2022-04-20 21:05:16.400980+0200 MyProject[10685:773575] [Presentation] Attempt to present <MFMessageComposeViewController: 0x117839600> on <MyProject.SMSHandler: 0x11570fb50> (from <MyProject.SMSHandler: 0x11570fb50>) whose view is not in the window hierarchy.
2022-04-20 21:05:16.402930+0200 MyProject[10685:773910] [AXRuntimeCommon] Unknown client: MyProject
2022-04-20 21:05:16.404912+0200 MyProject[10685:773790] [AXRuntimeCommon] AX Lookup problem - errorCode:1100 error:Permission denied portName:'com.apple.iphone.axserver' PID:9979 (
    0   AXRuntime                           0x00000001c98e6a9c 7A975793-0055-365F-A61A-C16EF91F55E2 + 338588
    1   AXRuntime                           0x00000001c989be30 _AXGetPortFromCache + 704
    2   AXRuntime                           0x00000001c989d75c AXUIElementPerformFencedActionWithValue + 564
    3   UIKit                               0x000000021473d234 2EAAE3FE-BFB1-3E62-B103-6B14136D43AE + 934452
    4   libdispatch.dylib                   0x0000000104cccc6c _dispatch_call_block_and_release + 32
    5   libdispatch.dylib                   0x0000000104cce7bc _dispatch_client_callout + 20
    6   libdispatch.dylib                   0x0000000104cd68a4 _dispatch_lane_serial_drain + 984
    7   libdispatch.dylib                   0x0000000104cd75e0 _dispatch_lane_invoke + 428
    8   libdispatch.dylib                   0x0000000104ce4168 _dispatch_workloop_worker_thread + 908
    9   libsystem_pthread.dylib             0x000000021440e0bc _pthread_wqthread + 288
    10  libsystem_pthread.dylib             0x000000021440de5c start_wqthread + 8
)
2022-04-20 21:05:16.405381+0200 MyProject[10685:773790] [AXRuntimeCommon] AX Lookup problem - errorCode:1100 error:Permission denied portName:'com.apple.iphone.axserver' PID:9979 (
    0   AXRuntime                           0x00000001c98e6a9c 7A975793-0055-365F-A61A-C16EF91F55E2 + 338588
    1   AXRuntime                           0x00000001c989be30 _AXGetPortFromCache + 704
    2   AXRuntime                           0x00000001c989d75c AXUIElementPerformFencedActionWithValue + 564
    3   UIKit                               0x000000021473d234 2EAAE3FE-BFB1-3E62-B103-6B14136D43AE + 934452
    4   libdispatch.dylib                   0x0000000104cccc6c _dispatch_call_block_and_release + 32
    5   libdispatch.dylib                   0x0000000104cce7bc _dispatch_client_callout + 20
    6   libdispatch.dylib                   0x0000000104cd68a4 _dispatch_lane_serial_drain + 984
    7   libdispatch.dylib                   0x0000000104cd75e0 _dispatch_lane_invoke + 428
    8   libdispatch.dylib                   0x0000000104ce4168 _dispatch_workloop_worker_thread + 908
    9   libsystem_pthread.dylib             0x000000021440e0bc _pthread_wqthread + 288
    10  libsystem_pthread.dylib             0x000000021440de5c start_wqthread + 8
)
2022-04-20 21:05:17.706401+0200 MyProject[10685:773575] [TraitCollection] Class CKBrowserSwitcherViewController overrides the -traitCollection getter, which is not supported. If you're trying to override traits, you must use the appropriate API.

I just copy and pasted a code snipped, here it is:

import UIKit
import MessageUI

class SMSHandler: UIViewController, MFMessageComposeViewControllerDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func send(phoneNumber: String, msg: String) {
        if (MFMessageComposeViewController.canSendText()) {
            let controller = MFMessageComposeViewController()
            controller.body = msg
            controller.recipients = [phoneNumber]
            controller.messageComposeDelegate = self
            self.present(controller, animated: true, completion: nil)
        }
    }
    
    func messageComposeViewController(_ controller: MFMessageComposeViewController, didFinishWith result: MessageComposeResult) {
        switch (result.rawValue) {
            case MessageComposeResult.cancelled.rawValue:
            print("Message was cancelled")
            self.dismiss(animated: true, completion: nil)
        case MessageComposeResult.failed.rawValue:
            print("Message failed")
            self.dismiss(animated: true, completion: nil)
        case MessageComposeResult.sent.rawValue:
            print("Message was sent")
            self.dismiss(animated: true, completion: nil)
        default:
            break;
        }
    }

    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = false
    }
}

I also looked at the apple developer docs and it seems they are deprecated... It says that you should use self.presentViewController(...), when I typed it tho, Xcode told me to change it to self.present(...). Is there a newer way to do this?
Any help appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤君无依 2025-01-29 20:53:48

Smshandler类是视图控制器。
您的send(PhonEnumb:msg:)函数试图从smshandler的实例中模仿视图控制器。这只有在当前在屏幕上的smshandler的实例上调用它。如果您希望smshandler类能在某些其他视图控制器的顶部显示VC,则需要对其进行重构。

The SMSHandler class is a view controller.
Your send(phoneNumb:msg:) function tries to present a view controller modally from an instance of SMSHandler. That will only work if you call it on an instance of SMSHandler that is currently on-screen. If you want the SMSHandler class to display a VC modally on top of some other view controller then you’ll need to refactor it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文