iMessage extension crashes while trying to access UITextView in compact mode

≡放荡痞女 提交于 2019-12-19 11:55:28

问题


Below is the entirety of my code in an iMessage app.

class MessagesViewController: MSMessagesAppViewController {

@IBOutlet weak var messageView: UITextView!

fileprivate func setupMessageView() {
    messageView.delegate = self

    messageView.layer.cornerRadius = 10
    messageView.layer.borderColor = UIColor.black.cgColor
    messageView.layer.borderWidth = 5

    messageView.text = "Tap to enter a message"
    messageView.textColor = UIColor(red:0.80, green:0.81, blue:0.82, alpha:1.0)
    messageView.textAlignment = .center

    messageView.font = UIFont.systemFont(ofSize: 20)

    messageView.textContainerInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
}

func initialize() {
    setupMessageView()
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.view.endEditing(true)
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(self.initialize), userInfo: nil, repeats: false)
}

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

// MARK: - Conversation Handling

override func willBecomeActive(with conversation: MSConversation) {
    // Called when the extension is about to move from the inactive to active state.
    // This will happen when the extension is about to present UI.

    // Use this method to configure the extension and restore previously stored state.
}

override func didResignActive(with conversation: MSConversation) {
    // Called when the extension is about to move from the active to inactive state.
    // This will happen when the user dissmises the extension, changes to a different
    // conversation or quits Messages.

    // Use this method to release shared resources, save user data, invalidate timers,
    // and store enough state information to restore your extension to its current state
    // in case it is terminated later.
}

override func didReceive(_ message: MSMessage, conversation: MSConversation) {
    // Called when a message arrives that was generated by another instance of this
    // extension on a remote device.

    // Use this method to trigger UI updates in response to the message.
}

override func didStartSending(_ message: MSMessage, conversation: MSConversation) {
    // Called when the user taps the send button.
}

override func didCancelSending(_ message: MSMessage, conversation: MSConversation) {
    // Called when the user deletes the message without sending it.

    // Use this to clean up state related to the deleted message.
}

override func willTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
    // Called before the extension transitions to a new presentation style.

    // Use this method to prepare for the change in presentation style.
}

override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
    // Called after the extension transitions to a new presentation style.

    // Use this method to finalize any behaviors associated with the change in presentation style.
}

}

extension MessagesViewController: UITextViewDelegate {
func textViewDidBeginEditing(_ textView: UITextView) {
    if textView == messageView {
        requestPresentationStyle(.expanded)
        if textView.textColor == UIColor(red:0.80, green:0.81, blue:0.82, alpha:1.0) {
            textView.text = nil
            textView.textAlignment = .left
            textView.textColor = UIColor.black
            textView.font = UIFont.systemFont(ofSize: 20)
        }
    }
}

func textViewDidEndEditing(_ textView: UITextView) {
    if textView == messageView {
        if textView.text.isEmpty {
            textView.text = "Tap to enter a message"
            textView.textAlignment = .center
            textView.textColor = UIColor(red:0.80, green:0.81, blue:0.82, alpha:1.0)
            textView.font = UIFont.systemFont(ofSize: 20)
        }
    }
}
}

It has a UITextView, and I try enter data. I experience a weird problem while performing this action.

On initial load, if I tap the UITextView, expanded mode is called, but the keyboard doesn't slide up. It needs another tap for the keyboard to slide up.

In the logs, I was able to find, that the textViewDidBeginEditing and textViewDidEndEditing methods are called successively on the first tap. Not sure, why it is happening this way!?

Anyways, what intrigues me more, is what happens now. I can change the mode to compact manually, and back to expanded. If in expanded mode, once I tap, the keyboard slides up. But, if I tap while in compact mode, the app crashes!!

And this happens all the time. On simulator and a real device. I have no clue to explain this behaviour.

No matter how many times I change the mode from compact to expanded and back, I can enter text in expanded mode. But, after the first tap, it never happens again, while in the compact mode.

Does anyone have this issue? Or can you replicate this? Is this a bug wth Apple?

来源:https://stackoverflow.com/questions/44517995/imessage-extension-crashes-while-trying-to-access-uitextview-in-compact-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!