iOS14 Crash Fatal Unexpectedly found nil while unwrapping an Optional value

I'm getting into a crash only with iOS14 and Xcode 14 simulator. I don't see this issue when I was on iOS13 or Xcode 13 simulator.

I'm using SafariServices to open a URL and the crash is happening at

SafariView(url: URL(string: self.urlString)!)

I felt fine forcing the unwrap because I know that self.urlString = self.dogood.link has a value from an Identifiable that I'm using. Am I looking at an iOS 14 bug possibly?

Code Block
struct SafariView: UIViewControllerRepresentable {
  typealias UIViewControllerType = SFSafariViewController
  var url: URL?
  func makeUIViewController(context: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
    return SFSafariViewController(url: url!)
  }
  func updateUIViewController(_ safariViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<SafariView>) {
  }
}


Code Block
struct DoGoodListView: View {
  @State private var showSafari = false
  @State private var urlString = ""
  var dogood: DoGood
  var body: some View {
      VStack(spacing: 8.0) {
        HStack {
          Text(dogood.name)
            .font(.title).bold()
          Spacer()
          Image(dogood.logo)
          .resizable()
          .aspectRatio(contentMode: .fit)
          .cornerRadius(15)
            .frame(width: 50, height: 50)
        }
        Text(dogood.info)
        Button(action: {
          self.urlString = self.dogood.link
          self.showSafari = true
          print(self.urlString)
        }) {
        Text("Visit")
        .font(.system(size: 18, weight: .medium))
        .frame(width: 250, height: 50)
        .background(Color("card3"))
        .clipShape(RoundedRectangle(cornerRadius: 30, style: .continuous))
          .shadow(color: Color("card3").opacity(0.1), radius: 1, x: 0, y: 1)
          .shadow(color: Color("card3").opacity(0.2), radius: 10, x: 0, y: 10)
        .padding()
        }
        .sheet(isPresented: $showSafari) {
          SafariView(url: URL(string: self.urlString)!)
        }
      }
  }
}

Answered by OOPer in 632124022
Seems you have hit the same issue shown in this thread

Try passing a Binding.
Code Block
struct SafariView: UIViewControllerRepresentable {
typealias UIViewControllerType = SFSafariViewController
@Binding var urlString: String
func makeUIViewController(context: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
guard let url = URL(string: urlString) else {
fatalError("Invalid urlString: \(urlString)")
}
return SFSafariViewController(url: url)
}
func updateUIViewController(_ safariViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<SafariView>) {
}
}


Code Block
struct DoGoodListView: View {
@State private var showSafari = false
@State private var urlString = ""
var dogood: DoGood
var body: some View {
VStack(spacing: 8.0) {
...
.sheet(isPresented: $showSafari) {
SafariView(urlString: self.$urlString)
}
}
}
}


What do you get from           
Code Block
print(self.urlString)


Could you show the DoGood struct definition ?

Maybe the URL is not initialised yet when you unwrap.

In any case it is always safer to test URL for nil before unwrapping:

What do you get from          
print(self.urlString)

It prints the URL.

I'm fairly new to developing. Would I test it in within Button(action: {} or elsewhere?
Of course you get the URL. My question is to see the exact content of URL (as printed)
Sorry misunderstood the question.

It prints https://runningworks.org/


Code Block
struct DoGood: Identifiable {
  var id = UUID()
  var name: String
  var info: String
  var link: String
  var logo: String
}
let doGoodData = [
  DoGood(name: "Running Works", info: "Running Works is a non-profit running program founded to use sport to empower individuals and families to break cycles of abuse, neglect, ******* and homelessness – one stride at a time.", link: "https://runningworks.org/", logo: "runningworks"),
  DoGood(name:"PARA GUIDE", info:"PARA GUIDE enables Para-Visually impaired individuals to experience a more enriched life through guided physical activities.", link:"https://www.paraguide.org/", logo: ""),
  DoGood(name:"Share Charlotte", info:"Shre Charlotte offers simple ways for neighbors, nonprofits, and businesses to come together and do good to support our local community.", link:"https://sharecharlotte.org/", logo:""),
  DoGood(name:"Speed for Need", info:"Speed For Need pushes riders with special needs in customized racing chairs to help them compete in fitness events that they would not be able to do on their own.",link:"https://speedforneed.org/", logo:"speedforneed")
]


I see the crash in IOS14 and not iOS 13, you're right.

When I replaced
Code Block
Text("Visit")

by
Code Block
Text("Visit \(self.urlString)")


Crash does not occur anymore.
But first time, Visit label does not show url (empty)
So it sems that URL is not initialized when you define the sheet.
And I have to tap the button at top right of navigation bar to make the site appear…

Maybe worth a bug report (at least against documentation).

I think you're right about it not getting passed or purged. When I updated @State private var urlString = "" to be  @State private var urlString = "https://apple.com" apple.com site is getting served and the crash doesn't happen.
Accepted Answer
Seems you have hit the same issue shown in this thread

Try passing a Binding.
Code Block
struct SafariView: UIViewControllerRepresentable {
typealias UIViewControllerType = SFSafariViewController
@Binding var urlString: String
func makeUIViewController(context: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
guard let url = URL(string: urlString) else {
fatalError("Invalid urlString: \(urlString)")
}
return SFSafariViewController(url: url)
}
func updateUIViewController(_ safariViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<SafariView>) {
}
}


Code Block
struct DoGoodListView: View {
@State private var showSafari = false
@State private var urlString = ""
var dogood: DoGood
var body: some View {
VStack(spacing: 8.0) {
...
.sheet(isPresented: $showSafari) {
SafariView(urlString: self.$urlString)
}
}
}
}


OOPer if I could buy you a drink of your choice I would. That fixed it!
iOS14 Crash Fatal Unexpectedly found nil while unwrapping an Optional value
 
 
Q