I have a view in which I add the observer in onAppear and remove that observer in onDisappear method. But the observer does not get removed. I read the documentation but did not find any solution. Looking for help. thanks
struct MainListView: View {
let NC = NotificationCenter.default
var body: some View {
VStack(alignment: .center, spacing: 1) {
.......
}
.onDisappear{
self.NC.removeObserver(self, name: Notification.Name(rawValue: "redrawCategories"), object: self)
}
.onAppear {
self.NC.addObserver(forName: Notification.Name(rawValue: "redrawCategories"), object: nil, queue: nil) { (notification) in
.......
}
}
}
}
Here is SwiftUI approach to observe notifications
struct MainListView: View {
let redrawCategoriesPublisher = NotificationCenter.default.publisher(for:
Notification.Name(rawValue: "redrawCategories"))
var body: some View {
VStack(alignment: .center, spacing: 1) {
Text("Demo")
}
.onReceive(redrawCategoriesPublisher) { notification in
// do here what is needed
}
}
}
Related
I would like to subscribe to UIPasteboard changes in SwiftUI with onReceive.
pHasStringsPublisher will not be updated as soon as something in the clipboard changes and I don't understand why.
import SwiftUI
struct ContentView: View {
let pasteboard = UIPasteboard.general
#State var pString: String = "pString"
#State var pHasStrings: Bool = false
#State var pHasStringsPublisher: Bool = false
var body: some View {
VStack{
Spacer()
Text("b: '\(self.pString)'")
.font(.headline)
Text("b: '\(self.pHasStrings.description)'")
.font(.headline)
Text("p: '\(self.pHasStringsPublisher.description)'")
.font(.headline)
Spacer()
Button(action: {
self.pString = self.pasteboard.string ?? "nil"
self.pHasStrings = self.pasteboard.hasStrings
}, label: {
Text("read pb")
.font(.largeTitle)
})
Button(action: {
self.pasteboard.items = []
}, label: {
Text("clear pb")
.font(.largeTitle)
})
Button(action: {
self.pasteboard.string = Date().description
}, label: {
Text("set pb")
.font(.largeTitle)
})
}
.onReceive(self.pasteboard
.publisher(for: \.hasStrings)
.print()
.receive(on: RunLoop.main)
.eraseToAnyPublisher()
, perform:
{ hasStrings in
print("pasteboard publisher")
self.pHasStringsPublisher = hasStrings
})
}
}
As far as I know, none of UIPasteboard's properties are documented to support Key-Value Observing (KVO), so publisher(for: \.hasStrings) may not ever publish anything.
Instead, you can listen for UIPasteboard.changedNotification from the default NotificationCenter. But if you are expecting the user to copy in a string from another application, that is still not sufficient, because a pasteboard doesn't post changedNotification if its content was changed while your app was in the background. So you also need to listen for UIApplication.didBecomeActiveNotification.
Let's wrap it all up in an extension on UIPasteboard:
extension UIPasteboard {
var hasStringsPublisher: AnyPublisher<Bool, Never> {
return Just(hasStrings)
.merge(
with: NotificationCenter.default
.publisher(for: UIPasteboard.changedNotification, object: self)
.map { _ in self.hasStrings })
.merge(
with: NotificationCenter.default
.publisher(for: UIApplication.didBecomeActiveNotification, object: nil)
.map { _ in self.hasStrings })
.eraseToAnyPublisher()
}
}
And use it like this:
var body: some View {
VStack {
blah blah blah
}
.onReceive(UIPasteboard.general.hasStringsPublisher) { hasStrings = $0 }
}
I am woking on an iOS Custom Keyboard Extension. SwiftUI buttons are showing properly but never gets called!
import SwiftUI
class KeyboardViewController: UIInputViewController {
override func viewDidLoad() {
super.viewDidLoad()
let vc = UIHostingController(rootView: MyKeyButtons())
vc.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
view.addSubview(vc.view)
}
}
struct MyKeyButtons: View {
let data: [String] = ["A", "B", "C"]
var body: some View {
HStack {
ForEach(data, id: \.self) { aData in
Button(action: {
print("button pressed!") // Not working!
}) {
Text(aData).fontWeight(.bold).font(.title)
.foregroundColor(.white).padding()
.background(Color.purple)
}
}
}
}
}
For easier understanding, here is the full: https://github.com/ask2asim/KeyboardTest1
How do I add NotificationCenter.default.addObserve in SwiftUI?
When I tried adding observer I get below error
Argument of '#selector' refers to instance method 'VPNDidChangeStatus'
that is not exposed to Objective-C
But when I add #objc in front of func I get below error
#objc can only be used with members of classes, #objc protocols, and
concrete extensions of classes
Here is my code
let NC = NotificationCenter.default
var body: some View {
VStack() {
}.onAppear {
self.NC.addObserver(self, selector: #selector(self.VPNDidChangeStatus),
name: .NEVPNStatusDidChange, object: nil)
}
}
#objc func VPNDidChangeStatus(_ notification: Notification) {
// print("VPNDidChangeStatus", VPNManager.shared.status)
}
The accepted answer may work but is not really how you're supposed to do this. In SwiftUI you don't need to add an observer in that way.
You add a publisher and it still can listen to NSNotification events triggered from non-SwiftUI parts of the app and without needing combine.
Here as an example, a list will update when it appears and when it receives a notification, from a completed network request on another view / controller or something similar etc.
If you need to then trigger an #objc func for some reason, you will need to create a Coordinator with UIViewControllerRepresentable
struct YourSwiftUIView: View {
let pub = NotificationCenter.default
.publisher(for: NSNotification.Name("YourNameHere"))
var body: some View {
List() {
ForEach(userData.viewModels) { viewModel in
SomeRow(viewModel: viewModel)
}
}
.onAppear(perform: loadData)
.onReceive(pub) { (output) in
self.loadData()
}
}
func loadData() {
// do stuff
}
}
I have one approach for NotificationCenter usage in SwiftUI.
For more information Apple Documentation
Notification extension
extension NSNotification {
static let ImageClick = Notification.Name.init("ImageClick")
}
ContentView
struct ContentView: View {
var body: some View {
VStack {
DetailView()
}
.onReceive(NotificationCenter.default.publisher(for: NSNotification.ImageClick))
{ obj in
// Change key as per your "userInfo"
if let userInfo = obj.userInfo, let info = userInfo["info"] {
print(info)
}
}
}
}
DetailView
struct DetailView: View {
var body: some View {
Image(systemName: "wifi")
.frame(width: 30,height: 30, alignment: .center)
.foregroundColor(.black)
.onTapGesture {
NotificationCenter.default.post(name: NSNotification.ImageClick,
object: nil, userInfo: ["info": "Test"])
}
}
}
I use this extension so it's a bit nicer on the call site:
/// Extension
extension View {
func onReceive(
_ name: Notification.Name,
center: NotificationCenter = .default,
object: AnyObject? = nil,
perform action: #escaping (Notification) -> Void
) -> some View {
onReceive(
center.publisher(for: name, object: object),
perform: action
)
}
}
/// Usage
struct MyView: View {
var body: some View {
Color.orange
.onReceive(.myNotification) { _ in
print(#function)
}
}
}
extension Notification.Name {
static let myNotification = Notification.Name("myNotification")
}
It is not SwiftUI-native approach, which is declarative & reactive. Instead you should use NSNotificationCenter.publisher(for:object:) from Combine.
See more details in Apple Documentation
This worked for me
let NC = NotificationCenter.default
self.NC.addObserver(forName: .NEVPNStatusDidChange, object: nil, queue: nil,
using: self.VPNDidChangeStatus)
func VPNDidChangeStatus(_ notification: Notification) {
}
exchange this
self.NC.addObserver(self, selector: #selector(self.VPNDidChangeStatus),
name: .NEVPNStatusDidChange, object: nil)
to
self.NC.addObserver(self, selector: #selector(VPNDidChangeStatus(_:)),
name: .NEVPNStatusDidChange, object: nil)
I'm trying to have every object in the view be shifted up when a text field is present, and to reduce amount of code, I tried making the modifiers into a modifier class, but the issue is when the y value gets changed for the keyboard handlers, it doesn't move. This exact code works if you don't use the separate function, so I don't know what's wrong.
I've tried making the value of the y a state in the modifier, but it still doesn't update - and like I said, it works if it's not in the custom modifier.
ContentView.swift
struct ContentView: View {
#State private var name = Array<String>.init(repeating: "", count: 3)
#ObservedObject private var kGuardian = KeyboardGuardian(textFieldCount: 1)
var body: some View {
Background {
VStack {
Group {
Text("Some filler text").font(.largeTitle)
Text("Some filler text").font(.largeTitle)
}
TextField("enter text #1", text: self.$name[0])
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("enter text #2", text: self.$name[1])
.textFieldStyle(RoundedBorderTextFieldStyle())
TextField("enter text #3", text: self.$name[2])
.textFieldStyle(RoundedBorderTextFieldStyle())
.background(GeometryGetter(rect: self.$kGuardian.rects[0]))
}
}.modifier(BetterTextField(kGuardian: kGuardian, slide: kGuardian.slide))
}
}
KeyboardModifications.swift
struct GeometryGetter: View {
#Binding var rect: CGRect
var body: some View {
GeometryReader { geometry in
Group { () -> AnyView in
DispatchQueue.main.async {
self.rect = geometry.frame(in: .global)
}
return AnyView(Color.clear)
}
}
}
}
final class KeyboardGuardian: ObservableObject {
public var rects: Array<CGRect>
public var keyboardRect: CGRect = CGRect()
// keyboardWillShow notification may be posted repeatedly,
// this flag makes sure we only act once per keyboard appearance
public var keyboardIsHidden = true
#Published var slide: CGFloat = 0
var appearenceDuration : Double = 0
var showField: Int = 0 {
didSet {
updateSlide()
}
}
init(textFieldCount: Int) {
self.rects = Array<CGRect>(repeating: CGRect(), count: textFieldCount)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
#objc func keyBoardWillShow(notification: Notification) {
guard let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double else {return}
appearenceDuration = duration
if keyboardIsHidden {
keyboardIsHidden = false
if let rect = notification.userInfo?["UIKeyboardFrameEndUserInfoKey"] as? CGRect {
keyboardRect = rect
updateSlide()
}
}
}
#objc func keyBoardWillHide(notification: Notification) {
keyboardIsHidden = true
updateSlide()
}
func updateSlide() {
if keyboardIsHidden {
slide = 0
} else {
let tfRect = self.rects[self.showField]
let diff = keyboardRect.minY - tfRect.maxY
if diff > 0 {
slide += diff
} else {
slide += min(diff, 0)
}
}
}
}
struct Background<Content: View>: View {
private var content: Content
init(#ViewBuilder content: #escaping () -> Content) {
self.content = content()
}
var body: some View {
Color.white
.frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
.overlay(content)
}
}
The custom modifier class:
struct BetterTextField: ViewModifier {
public var kGuardian : KeyboardGuardian
#State public var slide : CGFloat
func body(content: Content) -> some View {
content
.offset(y: slide).animation(.easeIn(duration: kGuardian.appearenceDuration))
.onTapGesture {
let keyWindow = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
keyWindow!.endEditing(true)
}
}
}
What should happen is the entire screen should shift up with the keyboard, but it stays as it is. I've been trying things for around 2 hours to no avail.
I guess your slide variable is not updated. Try with the following change code in BetterTextField modifier:
struct BetterTextField: ViewModifier {
#ObservedObject var kGuardian : KeyboardGuardian
func body(content: Content) -> some View {
content
.offset(y: kGuardian.slide).animation(.easeIn(duration: kGuardian.appearenceDuration))
.onTapGesture {
let keyWindow = UIApplication.shared.connectedScenes
.filter({$0.activationState == .foregroundActive})
.map({$0 as? UIWindowScene})
.compactMap({$0})
.first?.windows
.filter({$0.isKeyWindow}).first
keyWindow!.endEditing(true)
}
}
}
I understand there is PresentationButton and NavigationButton in order to change views in the latest SwiftUI. However I want to do a simple operation like below. When user clicks on SignIn button if credentials are correct it will sign them in but also do a segue (in this case change the view). However I could not check if they are correct in PresentationButton and I could not change the view in a normal button.
Is there another way to do that?
#IBAction func signInClicked(_ sender: Any) {
if emailText.text != "" && passwordText.text != "" {
Auth.auth().signIn(withEmail: emailText.text!, password: passwordText.text!) { (userdata, error) in
if error != nil {
//error
} else {
performSegue(withIdentifier: "toFeedActivity", sender: nil)
}
}
} else {
//error
}
}
Here's one way.
struct AppContentView: View {
#State var signInSuccess = false
var body: some View {
return Group {
if signInSuccess {
AppHome()
}
else {
LoginFormView(signInSuccess: $signInSuccess)
}
}
}
}
struct LoginFormView : View {
#State private var userName: String = ""
#State private var password: String = ""
#State private var showError = false
#Binding var signInSuccess: Bool
var body: some View {
VStack {
HStack {
Text("User name")
TextField("type here", text: $userName)
}.padding()
HStack {
Text(" Password")
TextField("type here", text: $password)
.textContentType(.password)
}.padding()
Button(action: {
// Your auth logic
if(self.userName == self.password) {
self.signInSuccess = true
}
else {
self.showError = true
}
}) {
Text("Sign in")
}
if showError {
Text("Incorrect username/password").foregroundColor(Color.red)
}
}
}
}
struct AppHome: View {
var body: some View {
VStack {
Text("Hello freaky world!")
Text("You are signed in.")
}
}
}
I had the same need in one of my app and I've found a solution...
Basically you need to insert your main view in a NavigationView, then add an invisible NavigationLink in you view, create a #state var that controls when you want to push the view and change it's value on your login callback...
That's the code:
struct ContentView: View {
#State var showView = false
var body: some View {
NavigationView {
VStack {
Button(action: {
print("*** Login in progress... ***")
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
self.showView = true
}
}) {
Text("Push me and go on")
}
//MARK: - NAVIGATION LINKS
NavigationLink(destination: PushedView(), isActive: $showView) {
EmptyView()
}
}
}
}
}
struct PushedView: View {
var body: some View {
Text("This is your pushed view...")
.font(.largeTitle)
.fontWeight(.heavy)
}
}
Try with state & .sheet
struct ContentView: View {
#State var showingDetail = false
var body: some View {
Button(action: {
self.showingDetail.toggle()
}) {
Text("Show Detail")
}.sheet(isPresented: $showingDetail) {
DetailView()
}
}
}
You can use navigation link with tags so,
Here is the code:
first of all, declare tag var
#State var tag : Int? = nil
then create your button view:
Button("Log In", action: {
Auth.auth().signIn(withEmail: self.email, password: self.password, completion: { (user, error) in
if error == nil {
self.tag = 1
print("success")
}else{
print(error!.localizedDescription)
}
})
So when log in success tag will become 1 and when tag will become 1 your navigation link will get executed
Navigation Link code:
NavigationLink(destination: HomeView(), tag: 1, selection: $tag) {
EmptyView()
}.disabled(true)
if you are using Form use .disabled because here the empty view will be visible on form and you don't want your user to click on it and go to the homeView.