Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios SwiftUI中未触发onReceive_Ios_Swift_Swiftui_Combine - Fatal编程技术网

Ios SwiftUI中未触发onReceive

Ios SwiftUI中未触发onReceive,ios,swift,swiftui,combine,Ios,Swift,Swiftui,Combine,当我处理用户拒绝通知的情况时,不会触发onReceive块。如果用户接受通知提示,那么它就起作用了。任何帮助都将不胜感激。以下是必要的代码片段: import SwiftUI struct UIView: View { @ObservedObject var loginViewModel = LogInViewModel() ... var body: some View { VStack { ... Bu

当我处理用户拒绝通知的情况时,不会触发onReceive块。如果用户接受通知提示,那么它就起作用了。任何帮助都将不胜感激。以下是必要的代码片段:

import SwiftUI

struct UIView: View {
    @ObservedObject var loginViewModel = LogInViewModel()
    ...
    var body: some View {
        VStack {
            ...
            Button(action: {
                UNUserNotificationCenter.current().requestAuthorization(options: [ .alert, .badge, .sound ]) { (success, error) in
                        if error == nil {
                            DispatchQueue.main.async {
                                if success {
                                    UIApplication.shared.registerForRemoteNotifications()
                                } else {
                                    self.loginViewModel.loginCall(...)
                                }
                            }
                        }
                    }
            }) {
                ...
            }.onReceive(loginViewModel.success) {
                print("Login successful!") // <- This doesn't get called if a user doesn't allow the notifications
            }
            ...
        }
    }
}

import Combine

class LogInViewModel: ObservableObject {
    var success = PassthroughSubject<Void, Never>()
    ...
    func loginCall(...) {
        async API call here { (result: Result<..., ...>) in
            if case .success(...) = result {
                DispatchQueue.main.async {
                    self.success.send()
                }
            }
        }
    }
}
导入快捷界面
结构UIView:视图{
@ObservedObject变量loginViewModel=loginViewModel()
...
var body:一些观点{
VStack{
...
按钮(操作:{
UNUserNotificationCenter.current().requestAuthorization(选项:[.alert、.badge、.sound]){(成功、错误)
如果错误==nil{
DispatchQueue.main.async{
如果成功{
UIApplication.shared.registerForRemoteNotifications()
}否则{
self.loginViewModel.loginCall(…)
}
}
}
}
}) {
...
}.onReceive(loginViewModel.success){

打印(“登录成功!”)//与Xcode 11.2.1/iOS 13.2一起使用。2@SecretBit你找到解决办法了吗?