UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do I save input inside a UITextView?

Forums > SwiftUI

How do I save input in a UITextView once my application is double tapped out/force quitted? Is there some code I can write on the textViewDidChange call that can automatically save the input?

The following is my code:

@EnvironmentObject var obj : observed
func makeUIView(context: UIViewRepresentableContext<MultiTextField>) -> UITextView {

    let view = UITextView()
    view.font = .systemFont(ofSize: 16)
    view.text = " "
    view.textColor = UIColor.black.withAlphaComponent(0.350)
    view.backgroundColor = .clear
    view.delegate = context.coordinator
    self.obj.size = view.contentSize.height
    view.isEditable = true
    view.isUserInteractionEnabled = true
    view.isScrollEnabled = true
    view.isSelectable = true
    return view
}

func updateUIView(_ uiView: UITextView, context: UIViewRepresentableContext<MultiTextField>) {
        }

class Coordinator : NSObject,UITextViewDelegate {

    var parent : MultiTextField

    init(parent1 : MultiTextField) {

        parent = parent1
    }

    func textViewDidBeginEditing(_ textView: UITextView) {
        textView.textColor = .black

    }

    func textViewDidChange(_ textView: UITextView) {
        self.parent.obj.size = textView.contentSize.height

    }

    func textViewDidEndEditing(_ textView: UITextView) {
        print("Editing is done")
    }

}

2      

Yes, you can. But where want you it save to?

2      

@Hatsushira I want to save it to my customized UITEXTVIEW. So if I double tap out of the application (essentially quitting it) and enter back into it, the input is still there. (Kind of like using userdefaults with normal textfields)

2      

This doesn't work. You have to save it physically on your device. UserDefaults is one possibility. When your app starts you have to read it back from your device and put it into your TextView.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.