# UIApplication.shared is undefined during app launch, but it works!

**URL:** <https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859>\
**Category:** Using Swift\
**Tags:** uikit\
**Created:** [October 14, 2022, 6:02pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859 "2022-10-14T18:02:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![hmlongco](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hmlongco/32/5729_2.png) [@hmlongco](https://forums.swift.org/u/hmlongco)\
**Post date:** [October 14, 2022, 6:02pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859/1 "2022-10-14T18:02:23Z")

</div>

So do the following. We're only concerned about the init statement here, body is standard and unimportant.

```swift
@main
struct SomeApp: App {
    init() {
        print(UIApplication.shared) // Crash = EXC_BAD_ACCESS (code=1, address=0x0)
    }
    var body: some Scene {
        ...
    }
}

```

App crashes at the print statement. Okay. Fine.

Now try this...

```swift
@main
struct SomeApp: App {
    init() {
        print(UIApplication.shared.isNetworkActivityIndicatorVisible) // prints false!!!
    }
    var body: some Scene {
        ...
    }
}

```

The app DOESN'T crash and the print statement prints false.

Breakpoint the print statement and check in the debugger....

```swift
(lldb) po UIApplication.shared
 <uninitialized>
(lldb) po UIApplication.shared.isNetworkActivityIndicatorVisible
false

```

Can someone explain the black magic that seems to be going on here??? Does dynamic dispatch have some sort of secret understanding with UIApplication???

This was found when discussing an issue with Factory: [Crash when using singleton · Issue #30 · hmlongco/Factory · GitHub](https://github.com/hmlongco/Factory/issues/30)

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [October 14, 2022, 6:25pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859/2 "2022-10-14T18:25:33Z")

</div>

`UIApplication.shared` is declared to return `UIApplication`, so if you're accessing that value in a context where it's actually giving you a `nil` reference from the UIKit side, that's going to cause problems for Swift (because on the Swift side of things, we won't be performing any `nil` checks).

But `UIApplication` is also an Objective-C class, and so an access like `UIApplication.shared.isNetworkActivityIndicatorVisible` is going to become an Objective-C message send to `UIApplication.shared`. It's perfectly valid in Objective-C to send messages to `nil`, and you'll get back 'reasonable' values for many return types (such as `false` for `Bool` values). More info [here](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html#//apple_ref/doc/uid/TP40011210-CH4-SW22).

---

<div class="post-metadata">

**Author:** ![hmlongco](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hmlongco/32/5729_2.png) [@hmlongco](https://forums.swift.org/u/hmlongco)\
**Post date:** [October 14, 2022, 6:31pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859/3 "2022-10-14T18:31:50Z")

</div>

Relevant quote: " **Note:** If you expect a return value from a message sent to `nil` , the return value will be `nil` for object return types, `0` for numeric types, and `NO` for `BOOL` types. Returned structures have all members initialized to zero."

I knew this in another lifetime, but I've lived in Swift for so long that my Objective-C-fu is weak.

Thanks!

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [October 14, 2022, 6:40pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859/4 "2022-10-14T18:40:58Z")

</div>

Working in a mixed Objective-C/Swift codebase for long enough will give you a strong spidey-sense that whenever you see `EXC_BAD_ACCESS (code=1, address=0x0000000000000xx)` (i.e., any small address) it's probably because you've returned a `nil` object from Objective-C land to a Swift API that was marked as non-optional. 😄

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [October 14, 2022, 6:42pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859/5 "2022-10-14T18:42:22Z")

</div>

The root of the problem is that:

1. either "sharedApplication" shall never be nil, even that early during the app startup, or
2. sharedApplication shall be annotated with "nullable".

This is what it currently is:

```swift
@property(class, nonatomic, readonly) UIApplication *sharedApplication

```

within "NS\_ASSUME\_NONNULL\_BEGIN" brackets. Which leads to this weird situation:

 ![image](https://global.discourse-cdn.com/swift/original/3X/d/2/d2f0d984378492b457b1268b0b0c864d500b76c4.png)

---

<div class="post-metadata">

**Author:** ![hmlongco](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hmlongco/32/5729_2.png) [@hmlongco](https://forums.swift.org/u/hmlongco)\
**Post date:** [October 14, 2022, 6:57pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859/6 "2022-10-14T18:57:53Z")

</div>

And which can lead to other strange bugs if you assume that the methods or functions you call are in fact working and returning correct values.

---

<div class="post-metadata">

**Author:** ![harlanhaskins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/harlanhaskins/32/333_2.png) [@harlanhaskins](https://forums.swift.org/u/harlanhaskins)\
**Post date:** [October 14, 2022, 7:00pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859/7 "2022-10-14T19:00:27Z")

</div>

I think this would be worth feedback to Apple to add documentation that it's not valid to call `UIApplication.shared` prior to `UIApplicationMain` (which SwiftUI calls on your behalf _after_ `App.init` is called).

And maybe separately, documentation that `App.init` is called before `UIApplicationMain` or `NSApplicationMain`.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [October 14, 2022, 7:02pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859/8 "2022-10-14T19:02:19Z")

</div>

> [@harlanhaskins](#):
>
> And maybe separately, documentation that `App.init` is called before `UIApplicationMain` or `NSApplicationMain`.

Yeah this seems like a very subtle and crucial detail to be aware of.

---

<div class="post-metadata">

**Author:** ![harlanhaskins](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/harlanhaskins/32/333_2.png) [@harlanhaskins](https://forums.swift.org/u/harlanhaskins)\
**Post date:** [October 14, 2022, 7:03pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859/9 "2022-10-14T19:03:45Z")

</div>

It has, at least, been true for most, if not all, of UIKit's lifetime. However prior to the SwiftUI app lifecycle support, it was not as easy to accidentally call into `UIApplication.shared` prior to `UIApplicationMain`.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [October 14, 2022, 7:16pm UTC](https://forums.swift.org/t/uiapplication-shared-is-undefined-during-app-launch-but-it-works/60859/10 "2022-10-14T19:16:25Z")

</div>

> [@harlanhaskins](#):
>
> However prior to the SwiftUI app lifecycle support, it was not as easy to accidentally call into `UIApplication.shared` prior to `UIApplicationMain`.

Yeah, this is the part that's surprising to me. "No `UIApplication.shared` before `UIApplicationMain`" makes sense.
