From dbe8e5706db9fb63e50be7360cc6594c5c7a53b7 Mon Sep 17 00:00:00 2001
From: Michael Kirk <michael@signal.org>
Date: Wed, 21 Nov 2018 22:12:40 -0600
Subject: [PATCH] avoid crash on iOS9/10

---
 SignalMessaging/Views/OWSNavigationBar.swift | 31 ++++++++++++++++++--
 1 file changed, 28 insertions(+), 3 deletions(-)

diff --git a/SignalMessaging/Views/OWSNavigationBar.swift b/SignalMessaging/Views/OWSNavigationBar.swift
index a65ebd1de..b812fb1a8 100644
--- a/SignalMessaging/Views/OWSNavigationBar.swift
+++ b/SignalMessaging/Views/OWSNavigationBar.swift
@@ -59,13 +59,14 @@ public class OWSNavigationBar: UINavigationBar {
 
     private func applyTheme() {
         guard respectsTheme else {
-            self.blurEffectView?.removeFromSuperview()
+            removeBlurEffectView()
+
             self.setBackgroundImage(nil, for: .default)
             return
         }
 
         if UIAccessibilityIsReduceTransparencyEnabled() {
-            self.blurEffectView?.removeFromSuperview()
+            removeBlurEffectView()
             let color = Theme.navbarBackgroundColor
             let backgroundImage = UIImage(color: color)
             self.setBackgroundImage(backgroundImage, for: .default)
@@ -80,6 +81,7 @@ public class OWSNavigationBar: UINavigationBar {
 
             let blurEffectView: UIVisualEffectView = {
                 if let existingBlurEffectView = self.blurEffectView {
+                    existingBlurEffectView.isHidden = false
                     return existingBlurEffectView
                 }
 
@@ -194,6 +196,29 @@ public class OWSNavigationBar: UINavigationBar {
         self.setBackgroundImage(UIImage(), for: .default)
         self.shadowImage = UIImage()
         self.clipsToBounds = true
-        self.blurEffectView?.isHidden = true
+        removeBlurEffectView()
+    }
+
+    // MARK:
+
+    private func removeBlurEffectView() {
+        // Avoid crash on iOS 10.3.3 and iOS 9.4.
+        //
+        // Last Exception Backtrace:
+        // 0   CoreFoundation                    0x1b6bfb38 __exceptionPreprocess + 124 (NSException.m:165)
+        // 1   libobjc.A.dylib                   0x1a947062 objc_exception_throw + 34 (objc-exception.mm:521)
+        // 2   CoreFoundation                    0x1b6bfa80 +[NSException raise:format:] + 104 (NSException.m:140)
+        // 3   UIKit                             0x20bfc9fe -[UINavigationBar removeConstraint:] + 84 (UINavigationBar.m:4618)
+        // 4   UIKit                             0x208ee3d4 _UIViewRemoveConstraintsMadeDanglyByChangingSuperview + 534 // (NSLayoutConstraint_UIKitAdditions.m:4716)
+        // 5   UIKit                             0x208ede90 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 40 // (UIView.m:9467)
+        // 6   UIKit                             0x208edd6c -[UIView(Hierarchy) _postMovedFromSuperview:] + 710 (UIView.m:361)
+        // 7   UIKit                             0x20bd19e8 __UIViewWasRemovedFromSuperview + 154 (UIView.m:8889)
+        // 8   UIKit                             0x208ecf6a -[UIView(Hierarchy) removeFromSuperview] + 528 (UIView.m:8961)
+        // 9   SignalMessaging                   0x1d42b64 OWSNavigationBar.applyTheme() + 218 (OWSNavigationBar.swift:62)
+        if #available(iOS 11, *) {
+            self.blurEffectView?.removeFromSuperview()
+        } else {
+            self.blurEffectView?.isHidden = true
+        }
     }
 }