Sharing Screenshot of Swiftui View Causes Crash

Sharing Screenshot of SwiftUI view causes crash

I've found a solution that seems to be working here. I start the variable where the questionScreenShot gets stored as nil to start:

    @State var questionScreenShot: UIImage? = nil

Then I just make sure to set it to 'render' when the view appears, which means it loads the UIImage so if the user clicks "Share Question" it will be ready to be loaded (I think there was an issue earlier where the UIImage wasn't getting loaded in time once the sharing was done).

It also sets that variable back to nil on disappear.

.onAppear {
self.currentQuestions = currentTopic.questions.shuffled()
self.featuredQuestion = currentQuestions.last!
self.questionScreenShot = render()
}
.onDisappear {
self.questionScreenShot = nil
self.featuredQuestion = nil
}

SwiftUI using ForEach and onTapGesture to update selected element causing crash

Your problem is likely due to the view diffing algorithm having a different outcome if you have the text value present, since that would be a difference in the root view. Since you have two pieces of state that represent the sheet, and you are force unwrapping one of them, you're leaving yourself open to danger.

You should get rid of showingSheet, then use the item binding of sheet instead. Make Cards conform to Identifiable, then rewrite the sheet modifier as:

.sheet(item: $selectedCard) { SpecificCardView(card: $0) } 

This will also reset your selected card to nil when the sheet is dismissed.

Make sure image is rendered before launching share sheet to share image?

This is related to my answer for my other question: Sharing Screenshot of SwiftUI view causes crash

But basically I just needed to make sure I added an OnAppear block of code to the view that also sets the shareQuestion image to render(). Then I just set it back to nil OnDisappear of the view.

SwiftUI - Intermittent crash when presenting sheet on a sheet

I ran into a similar problem a few weeks ago. Turns out that when I presented the new sheet with the keyboard open it would lead to a crash.

I found using UIApplication.shared.endEditing() before showing the second sheet would solve the problem

UPDATE

For iOS 14 I’ve created an extension because the above function is no longer available

extension UIApplication {

static func endEditing() {
let resign = #selector(UIResponder.resignFirstResponder)
UIApplication.shared.sendAction(resign, to: nil, from: nil, for: nil)
}

}

The usage is similar UIApplication.endEditing()

Sharing A Screenshot in Activity View Controller - Swift

This is how i handle sharing in my app.

    func socialShare(#sharingText: String?, sharingImage: UIImage?, sharingURL: NSURL?) {
var sharingItems = [AnyObject]()

if let text = sharingText {
sharingItems.append(text)
}
if let image = sharingImage {
sharingItems.append(image)
}
if let url = sharingURL {
sharingItems.append(url)
}

let activityViewController = UIActivityViewController(activityItems: sharingItems, applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypeCopyToPasteboard,UIActivityTypeAirDrop,UIActivityTypeAddToReadingList,UIActivityTypeAssignToContact,UIActivityTypePostToTencentWeibo,UIActivityTypePostToVimeo,UIActivityTypePrint,UIActivityTypeSaveToCameraRoll,UIActivityTypePostToWeibo]
self.presentViewController(activityViewController, animated: true, completion: nil)
}

I have excluded a number of sharing options using .excludedActvityTypes.

Then whenever you hit the share button have it call this

socialShare(sharingText: "Just hit \(highscore)! Beat it! #SwypI", sharingImage: UIImage(named: "The screenshot you are saving"), sharingURL: NSURL(string: "http://itunes.apple.com/app/"))

The reason you are not seeing Twitter and Facebook as sharing options is because you need to be signed into them within the settings on the IPhone. Not the individual apps.

Hope this helps.



Related Topics



Leave a reply



Submit