Search code examples
swiftuivideo-playeravkit

SwiftUI macOS VideoPlayer View Only Crashes In Notarized (Distributed) App


In brief, my macOS app written purely in SwiftUI uses VideoPlayer to present a locally stored video (not bundled). The pertinent code is as follows:

import SwiftUI
import AVKit

struct CustomizedPlayerView: View {
    @State var player: AVPlayer?

    var body: some View {
        VStack {
            if let avPlayer = self.player {
                VideoPlayer(player: avPlayer).frame(minWidth: 320, minHeight: 320)
            }
            Text("Problematic VideoPlayer View")
        }
    }
}

When building for development in Xcode, there are no issues (no crashes, no issues playing).

However, opening the view in the notarized app, causes the entire app to crash with Exception Type: EXC_CRASH (SIGABRT). This signals to me that the system is aborting my app because of an improper assignment of a nil value, but the player does seem to pass a non-nil value at the time of crash.

I figured out the issue after some debugging and researching. I wanted to share what worked for me and hope it will save someone time.


Solution

  • The answer is to add AVKit.framework manually to Link Binary With Libraries in the app target's Build Phases in Xcode.

    Link AVKit Framework with Libraries

    It turns out the SwiftUI AVKit's VideoPlayer view is nil in the archived app, presumably not linked or built with AVKit.

    This blog post is the source: Source. All credit to Javier at The SwiftUI Lab. Surprisingly, the issue seems to be present since around June 2020.

    Bug Report: FB8928032