LetsDuck
LetsDuck

Reputation: 41

can't send SMS in swift

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!

Upvotes: 0

Views: 156

Answers (1)

Duncan C
Duncan C

Reputation: 131408

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.

Upvotes: 2

Related Questions