Live activity request fails silently on UIKit while it works on SwiftUI

378 Views Asked by At

I have an issue with Live Activities. While i can start it with SwiftUI project and same configuration starts displays and updates Live Activity lock screen widget and Dynamic island, on UIKit it fails silently god knows why.

Particular Activity<UploadingAttributes.self>.request()fails silently no error or such.

Anyone knows how to fix this ?

My attribute class

struct UploadingAttributes : ActivityAttributes {

    struct ContentState : Codable, Hashable {
        var progressValue : CGFloat = 0
    }
}

My live activity widget

struct UploadingActivityLiveActivity: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(for: UploadingAttributes.self) { context in
           
           Rectangle()
                .fill(.mint)

        } dynamicIsland: { context in
            DynamicIsland {
                DynamicIslandExpandedRegion(.leading) {
                    Text("Uploading")
                }
                DynamicIslandExpandedRegion(.trailing) {
                   Image(systemName: "square.and.arrow.up.circle.fill")
                }
                DynamicIslandExpandedRegion(.center) {
                    ProgressView(value: context.state.progressValue)
                        .progressViewStyle(.linear)
                }
           
            } compactLeading: {
                Text("L")
            } compactTrailing: {
                Text("T")
            } minimal: {
                Text("Min")
            }
        
        }
    }
}

My view controller

class ViewController: UIViewController {
    
    lazy var button : UIButton = {
        let button = UIButton()
        button.setTitle("Activate Activity", for: .normal)
        button.backgroundColor = .systemMint
        button.layer.cornerRadius = 8
        button.addTarget(self, action: #selector(startActivities), for: .touchUpInside)
        return button
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .yellow
        view.addSubview(button)
        button.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            button.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            button.widthAnchor.constraint(equalToConstant: 200),
            button.centerXAnchor.constraint(equalTo: view.centerXAnchor)
        ])
    }
    
    @objc func startActivities(_ sender: Any?) {
        let attributes = UploadingAttributes()
        let state = UploadingAttributes.ContentState()
     
        do  {
            try Activity<UploadingAttributes>.request(
                attributes: attributes,
                content: .init(state: state, staleDate: .now) )
        } catch(let e) {
            print(e)
        }
        
    }
    
}

1

There are 1 best solutions below

0
On

It seems there is a delay between UIKit and SwiftUI transactions. So when you invoke request immediately its not working, 1 sec delay worked.

  do  {
            try Activity<UploadingAttributes>.request(
                attributes: attributes,
                content: .init(state: state, staleDate: .now + 1)) 
                                         // use .now + 1 instead of .now
        } catch(let e) {
            print(e)
        }