iOS 13 Swiftui: App Crashes Upon Launch on Real Device

iOS 13 SwiftUI: app crashes upon launch on real device

Thanks to matt!

It turns out the issue was just a bug in Xcode 11 beta 1, preventing SwiftUI-based apps from being testing on real devices. Upgrading to Xcode 11 beta 2 resolved the issue.

SwiftUI app doesn't run when supporting iOS 13

The StateObject is available from SwiftUI 2.0 (iOS 14) and is designed to work in View, not in class (like SceneDelegate)

For provided scenario a possible solution is to use logicState as just property at top level (in delegate or main view), but inject it completely into child views (instead of just binding) and observe it internally.

So it should look like

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var loginStatus = UserLoginStatus() // << just hold here

// ...

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
let mainView = MainView(loginStatus: loginStatus) // << here !!

and

struct MainView: View {
@ObservedObject var loginStatus: UserLoginStatus // << injected here

// ...
}

similar can be done in WeatherApp

Why does my SwiftUI app crash when navigating backwards after placing a `NavigationLink` inside of a `navigationBarItems` in a `NavigationView`?

This was quite a pain point for me! I left it until most of my app was completed and I had the mind space to deal with the crashing.

I think we can all agree that there's some pretty awesome stuff with SwifUI but that the debugging can be difficult.

In my opinion, I would say that this is a BUG. Here is my rationale:

  • If you wrap the presentationMode dismiss call in an asynchronous delay of about a half-second, you should find that the program will no longer crash.

    DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
    self.presentationMode.wrappedValue.dismiss()
    }
  • This suggests to me that the bug is an unexpected behaviour way down deep in how SwiftUI interfaces with all the other UIKit code to manage the various views. Depending on your actual code, you might find that if there is some minor complexity in the view, the crash actually will not happen. For example, if you are dismissing from a view to one that has a list, and that list is empty, you will get a crash without the asynchronous delay. On the other hand, if you have even just one entry in that list view, forcing a loop iteration to generate the parent view, you'll see that the crash will not occur.

I'm not so sure how robust my solution of wrapping the dismiss call in a delay is. I have to test it much more. If you have ideas on this, please let me know! I'd be very happy to learn from you!

Can't use @ObservedObject on real iPhone

This isn't a problem with not conforming to ObservableObject. The code you provided works in the canvas and in the simulator, and should also work on the device. I have already come across this issue with iOS 13 Beta 6 in my own project and have spent a lot of time troubleshooting.

Other things (such as calling self.presentationMode.value.dismiss() to dismiss a modal view) are also currently broken when running projects built with Xcode Beta 5 on devices running iOS 13 Beta 6. There have been issues with previous betas of Xcode not working on newer betas of iOS, and this may be the same issue.

I would suggest that you wait until Xcode Beta 6 is released to make any significant structural changes to your code, as iOS 13 Beta 6 may have been developed in anticipation of handling changes that will be made in Xcode Beta 6.

That being said, if you absolutely must make changes to workaround this issue in the meantime, I've found that using @EnvironmentObject instead of @ObservedObject fixes this issue. In your example, that would mean declaring your property like this:

@EnvironmentObject private var networkManager: NetworkManager

Then, when you create your view, you can pass a NetworkManager as an environment object like this:

ContentView()
.environmentObject(NetworkManager())

iOS app with framework crashed on device, dyld: Library not loaded, Xcode 6 Beta

In the target's General tab, there is an Embedded Binaries field. When you add the framework there the crash is resolved.

Reference is here on Apple Developer Forums.

Swiftui app crashes with different SearchBar (ViewModifier) on iOS 14 / 15

UPDATE: This is fixed on XCode 13.3. Please update to that version. If you don't want to update, I will leave my answer below.

I also had the same issue with my app where users reported random crashes on iOS 14 and my crash log looked very similar to yours. The main line that helped me find the solution was __swift_instantiateConcreteTypeFromMangledName (which is on line 6 of your crash report and line 7 on mine).

My crash report from simulator:

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0 libswiftCore.dylib 0x18eb3a0f0 swift::ResolveAsSymbolicReference::operator()(swift::Demangle::SymbolicReferenceKind, swift::Demangle::Directness, int, void const*) + 64
1 libswiftCore.dylib 0x18eb519ac swift::Demangle::Demangler::demangleSymbolicReference(unsigned char) + 200
2 libswiftCore.dylib 0x18eb519ac swift::Demangle::Demangler::demangleSymbolicReference(unsigned char) + 200
3 libswiftCore.dylib 0x18eb4eda0 swift::Demangle::Demangler::demangleType(llvm::StringRef, std::__1::function<swift::Demangle::Node* (swift::Demangle::SymbolicReferenceKind, swift::Demangle::Directness, int, void const*)>) + 184
4 libswiftCore.dylib 0x18eb3f158 swift_getTypeByMangledNameImpl(swift::MetadataRequest, llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>) + 460
5 libswiftCore.dylib 0x18eb3cc88 swift::swift_getTypeByMangledName(swift::MetadataRequest, llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>) + 540
6 libswiftCore.dylib 0x18eb3ce80 swift_getTypeByMangledNameInContext + 128
7 Asobi 0x1040bef40 __swift_instantiateConcreteTypeFromMangledName + 52
8 Asobi 0x1040c9eb0 thunk for @escaping @callee_guaranteed (@guaranteed Bookmark) -> (@out _ConditionalContent<AnyView, <<opaque return type of View.contextMenu<A>(menuItems:)>>.0>) + 8 [inlined]
9 Asobi 0x1040c9eb0 partial apply for thunk for @escaping @callee_guaranteed (@guaranteed Bookmark) -> (@out _ConditionalContent<AnyView, <<opaque return type of View.contextMenu<A>(menuItems:)>>.0>) + 24
10 SwiftUI 0x1b40abee8 ForEachChild.updateValue() + 852
11 SwiftUI 0x1b3c8bd5c partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 28
12 AttributeGraph 0x1aa00b1f4 AG::Graph::UpdateStack::update() + 484
13 AttributeGraph 0x1aa00b5fc AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 320
14 AttributeGraph 0x1aa010544 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 380
15 AttributeGraph 0x1aa021210 AGGraphGetValue + 220
16 SwiftUI 0x1b426ab34 _ConditionalContent<>.ChildView.content.getter + 60
17 SwiftUI 0x1b426ac5c _ConditionalContent<>.ChildView.value.getter + 260
18 SwiftUI 0x1b426ad44 protocol witness for Rule.value.getter in conformance _ConditionalContent<A, B><>.ChildView + 40
19 SwiftUI 0x1b3a76728 implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 68
20 AttributeGraph 0x1aa00b1f4 AG::Graph::UpdateStack::update() + 484
21 AttributeGraph 0x1aa00b5fc AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 320
22 AttributeGraph 0x1aa010544 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 380
23 AttributeGraph 0x1aa021210 AGGraphGetValue + 220
24 SwiftUI 0x1b3f45b14 AnyViewList.updateValue() + 56
25 SwiftUI 0x1b3c2a9a8 partial apply for specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 20
26 AttributeGraph 0x1aa00b1f4 AG::Graph::UpdateStack::update() + 484
27 AttributeGraph 0x1aa00b5fc AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 320
28 AttributeGraph 0x1aa0104a8 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 224
29 AttributeGraph 0x1aa021018 AGGraphGetInputValue + 268
30 SwiftUI 0x1b40a6e44 closure #1 in ForEachState.traitKeys.getter + 120
31 SwiftUI 0x1b40a729c ForEachState.forEachItem(from:style:do:) + 976
32 SwiftUI 0x1b40a6db8 ForEachState.traitKeys.getter + 52
33 SwiftUI 0x1b40ab6cc ForEachList.traitKeys.getter + 20
34 SwiftUI 0x1b4207b34 Sections.init(from:reifyEmptyHeaders:useFooters:) + 132
35 SwiftUI 0x1b3e02fb0 SystemListDataSource.init(_:style:minRowHeight:minHeaderHeight:) + 108
36 SwiftUI 0x1b4134278 InsettableGroupedListBody.Body.body.getter + 564
37 SwiftUI 0x1b3c72d7c partial apply for closure #1 in ViewBodyAccessor.updateBody(of:changed:) + 28
38 SwiftUI 0x1b3f41f88 closure #1 in BodyAccessor.setBody(_:) + 44
39 SwiftUI 0x1b3c6e0e4 ViewBodyAccessor.updateBody(of:changed:) + 1340
40 SwiftUI 0x1b3f42304 DynamicBody.updateValue() + 268
41 SwiftUI 0x1b3c8bd5c partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 28
42 AttributeGraph 0x1aa00b1f4 AG::Graph::UpdateStack::update() + 484
43 AttributeGraph 0x1aa00b5fc AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 320
44 AttributeGraph 0x1aa010544 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 380
45 AttributeGraph 0x1aa021210 AGGraphGetValue + 220
46 SwiftUI 0x1b3e929e4 ListCore.Child.core.getter + 68
47 SwiftUI 0x1b3e92b1c ListCore.Child.value.getter + 268
48 SwiftUI 0x1b3a76728 implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 68
49 AttributeGraph 0x1aa00b1f4 AG::Graph::UpdateStack::update() + 484
50 AttributeGraph 0x1aa00b5fc AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 320
51 AttributeGraph 0x1aa010544 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 380
52 AttributeGraph 0x1aa021210 AGGraphGetValue + 220
53 SwiftUI 0x1b3f41a60 StaticBody.container.getter + 76
54 SwiftUI 0x1b3f42070 StaticBody.updateValue() + 116
55 SwiftUI 0x1b3c8bd5c partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 28
56 AttributeGraph 0x1aa00b1f4 AG::Graph::UpdateStack::update() + 484
57 AttributeGraph 0x1aa00b5fc AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 320
58 AttributeGraph 0x1aa010544 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 380
59 AttributeGraph 0x1aa021210 AGGraphGetValue + 220
60 SwiftUI 0x1b3977568 specialized UnaryPositionAwareChildGeometry.layout.getter + 24
61 SwiftUI 0x1b3977648 specialized UnaryPositionAwareChildGeometry.value.getter + 112
62 SwiftUI 0x1b3a228d4 specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 36
63 SwiftUI 0x1b3a3d15c partial apply for specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 24
64 AttributeGraph 0x1aa00b1f4 AG::Graph::UpdateStack::update() + 484
65 AttributeGraph 0x1aa00b5fc AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 320
66 AttributeGraph 0x1aa010544 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 380
67 AttributeGraph 0x1aa021210 AGGraphGetValue + 220
68 SwiftUI 0x1b3a6a2e0 LayoutPositionQuery.localPosition.getter + 24
69 SwiftUI 0x1b3a6a34c LayoutPositionQuery.updateValue() + 36
70 SwiftUI 0x1b3c2aae4 partial apply for specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 20
71 AttributeGraph 0x1aa00b1f4 AG::Graph::UpdateStack::update() + 484
72 AttributeGraph 0x1aa00b5fc AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 320
73 AttributeGraph 0x1aa010544 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, bool*, long) + 380
74 AttributeGraph 0x1aa021210 AGGraphGetValue + 220
75 SwiftUI 0x1b41833f0 AnimatableFrameAttribute.updateValue() + 52
76 SwiftUI 0x1b3c2b0dc partial apply for specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 20
77 AttributeGraph 0x1aa00b1f4 AG::Graph::UpdateStack::update() + 484
78 AttributeGraph 0x1aa00b5fc AG::Graph::update_attribute(AG::data::ptr<AG::Node>, bool) + 320
79 AttributeGraph 0x1aa013e60 AG::Subgraph::update(unsigned int) + 864
80 SwiftUI 0x1b41e66a0 GraphHost.runTransaction() + 164
81 SwiftUI 0x1b3d80790 ViewGraph.updateOutputs(at:) + 96
82 SwiftUI 0x1b4153d84 closure #1 in ViewRendererHost.render(interval:updateDisplayList:) + 1232
83 SwiftUI 0x1b414c45c ViewRendererHost.render(interval:updateDisplayList:) + 308
84 SwiftUI 0x1b429d934 _UIHostingView.layoutSubviews() + 168
85 SwiftUI 0x1b429d960 @objc _UIHostingView.layoutSubviews() + 24
86 UIKitCore 0x1848c603c -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 2540
87 QuartzCore 0x1875fb374 -[CALayer layoutSublayers] + 288
88 QuartzCore 0x187601860 CA::Layer::layout_if_needed(CA::Transaction*) + 504
89 UIKitCore 0x1848b2b34 -[UIView(Hierarchy) layoutBelowIfNeeded] + 524
90 UIKitCore 0x183c132bc -[_UISheetPresentationController _sheetLayoutInfoLayout:] + 48
91 UIKitCore 0x183c0f6bc -[_UISheetLayoutInfo _layout] + 312
92 UIKitCore 0x183c150fc __54-[_UISheetPresentationController transitionWillBegin:]_block_invoke_2 + 52
93 UIKitCore 0x1848b9408 +[UIView(Animation) performWithoutAnimation:] + 96
94 UIKitCore 0x183c15030 __54-[_UISheetPresentationController transitionWillBegin:]_block_invoke.362 + 128
95 UIKitCore 0x183d391cc -[_UIViewControllerTransitionCoordinator _applyBlocks:releaseBlocks:] + 264
96 UIKitCore 0x183d35cbc -[_UIViewControllerTransitionContext __runAlongsideAnimations] + 256
97 UIKitCore 0x1848b91ec __63+[UIView(Animation) _setAlongsideAnimations:toRunByEndOfBlock:]_block_invoke + 28
98 UIKitCore 0x18488e748 -[UIViewAnimationState _runAlongsideAnimations] + 32
99 UIKitCore 0x18488d638 -[UIViewAnimationState pop] + 52
100 UIKitCore 0x18488adf8 +[UIViewAnimationState popAnimationState] + 64
101 UIKitCore 0x1848bab84 +[UIView _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 648
102 UIKitCore 0x1848baff8 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 96
103 UIKitCore 0x184886940 __50-[UITransitionView _startTransition:withDuration:]_block_invoke.164 + 76
104 UIKitCore 0x1848bb4f4 +[UIView(UIViewAnimationWithBlocks) conditionallyAnimate:withAnimation:layout:completion:] + 88
105 UIKitCore 0x184886788 -[UITransitionView _startTransition:withDuration:] + 1156
106 UIKitCore 0x18488607c -[UITransitionView transition:fromView:toView:removeFromView:] + 2416
107 UIKitCore 0x183d31d34 -[UIViewControllerBuiltinTransitionViewAnimator animateTransition:] + 1476
108 UIKitCore 0x183d39dc8 ___UIViewControllerTransitioningRunCustomTransition_block_invoke_2 + 72
109 UIKitCore 0x183e66d6c +[UIInputResponderController _pinInputViewsForInputResponderController:onBehalfOfResponder:duringBlock:] + 104
110 UIKitCore 0x183d39d48 ___UIViewControllerTransitioningRunCustomTransition_block_invoke.641 + 176
111 UIKitCore 0x1848b90f0 +[UIView(Animation) _setAlongsideAnimations:toRunByEndOfBlock:] + 172
112 UIKitCore 0x183d39bb0 _UIViewControllerTransitioningRunCustomTransition + 580
113 UIKitCore 0x183c00b00 __56-[UIPresentationController runTransitionForCurrentState]_block_invoke.466 + 2180
114 UIKitCore 0x184867084 -[_UIAfterCACommitBlock run] + 64
115 UIKitCore 0x1843d4728 _runAfterCACommitDeferredBlocks + 296
116 UIKitCore 0x1843c48e8 _cleanUpAfterCAFlushAndRunDeferredBlocks + 200
117 UIKitCore 0x1843f4cb4 _afterCACommitHandler + 76
118 CoreFoundation 0x180362190 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
119 CoreFoundation 0x18035c90c __CFRunLoopDoObservers + 556
120 CoreFoundation 0x18035ce7c __CFRunLoopRun + 992
121 CoreFoundation 0x18035c58c CFRunLoopRunSpecific + 572
122 GraphicsServices 0x18b634740 GSEventRunModal + 160
123 UIKitCore 0x1843c5cbc -[UIApplication _run] + 964
124 UIKitCore 0x1843caa9c UIApplicationMain + 112
125 SwiftUI 0x1b41859d8 closure #1 in KitRendererCommon(_:) + 108
126 SwiftUI 0x1b4185968 runApp<A>(_:) + 160
127 SwiftUI 0x1b3d7a890 static App.main() + 80
128 Asobi 0x1040be9cc static AsobiApp.$main() + 28 (AsobiApp.swift:10) [inlined]
129 Asobi 0x1040be9cc main + 40
130 dyld 0x104f150f4 start + 520

After some digging, I found that this crash report is due to a bug in the Swift compiler on Xcode 13.2 and 13.2.1.

Forum post: https://developer.apple.com/forums/thread/697070

The main issue here is that using #if available (with any iOS 15 specific API which is searchable in your case) in a release build breaks the application for iOS 14 and below with SwiftUI. Apple is aware of this bug and there are no official workarounds at the moment.

Some user-suggested workarounds from the forum post are:

  1. Use Xcode 13.1
  2. Change the swift compiler to use -Onone on release builds
  3. Use this workaround https://github.com/gongzhang/swiftui-availability-check-crash

I used the workaround provided in the Github link, but if you want to go the -Onone route, I've provided instructions below.

How to set -Onone

  1. Go to your app target
  2. Go to that target's build settings
  3. Select All in the top bar
  4. Search for optimization
  5. Under Swift Compiler - Code Generation, set the Optimization Level to -Onone under the release section.

Here's my solution to your problem if you don't want to set compiler options, you'll get an Xcode warning, but ignore it for now:

struct CompatibleSearchBarModifier: ViewModifier {
@Binding var text: String

@ViewBuilder
func body(content: Content) -> some View {
if #available(iOS 15.0, *) {
Group {
if #available(iOS 15.0, *) {
content.searchable(text: self.$text, prompt: "Placeholder")
}
}
} else {
content.navigationBarSearch(self.$text, placeholder: "Placeholder", hidesSearchBarWhenScrolling: true, cancelClicked: {text = ""})
}
}
}

I'd recommend sending a Feedback Assistant report to Apple so they can take a closer look at this bug.

I hope Apple fixes this issue in Xcode 13.3 or another 13.2.x update.



Related Topics



Leave a reply



Submit