Skip to content

Commit 6be8cfb

Browse files
Added additional crash scenario to UI
1 parent 341aa83 commit 6be8cfb

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

samples/Sentry.Samples.Ios/AppDelegate.cs

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
3939
#pragma warning restore CA1416
4040
: UIColor.White;
4141
var buttonConfig = UIButtonConfiguration.TintedButtonConfiguration;
42+
var terminalButtonConfig = UIButtonConfiguration.TintedButtonConfiguration;
43+
terminalButtonConfig.BaseBackgroundColor = UIColor.SystemRed;
4244

4345
var vc = new UIViewController();
4446

@@ -50,7 +52,7 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
5052
AutoresizingMask = UIViewAutoresizing.All
5153
};
5254

53-
// UIButton for managed crash
55+
// UIButton for a managed exception that we'll catch and handle (won't crash the app)
5456
var managedCrashButton = new UIButton(UIButtonType.RoundedRect)
5557
{
5658
AutoresizingMask = UIViewAutoresizing.All,
@@ -62,31 +64,50 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
6264
Console.WriteLine("Managed Crash button clicked!");
6365
try
6466
{
65-
string s = null!;
66-
Console.WriteLine("Length: {0}", s.Length);
67+
throw new Exception("Catch this!");
6768
}
6869
catch (Exception e)
6970
{
7071
SentrySdk.CaptureException(e);
7172
}
7273
};
7374

75+
// UIButton for unhandled managed exception
76+
var unhandledCrashButton = new UIButton(UIButtonType.RoundedRect)
77+
{
78+
AutoresizingMask = UIViewAutoresizing.All,
79+
Configuration = terminalButtonConfig
80+
};
81+
unhandledCrashButton.SetTitle("Unhandled Crash", UIControlState.Normal);
82+
unhandledCrashButton.TouchUpInside += delegate
83+
{
84+
Console.WriteLine("Unhandled Crash button clicked!");
85+
string s = null!;
86+
// This will cause a NullReferenceException that will crash the app before Sentry can send the event.
87+
// Since we're using a caching transport though, the exception will be written to disk and sent the
88+
// next time the app is launched.
89+
Console.WriteLine("Length: {0}", s.Length);
90+
};
91+
7492
// UIButton for native crash
7593
var nativeCrashButton = new UIButton(UIButtonType.System)
7694
{
77-
Configuration = buttonConfig
95+
Configuration = terminalButtonConfig
7896
};
7997
nativeCrashButton.SetTitle("Native Crash", UIControlState.Normal);
8098
nativeCrashButton.TouchUpInside += delegate
8199
{
82100
Console.WriteLine("Native Crash button clicked!");
83101
#pragma warning disable CS0618 // Type or member is obsolete
102+
// This will cause a native crash that will crash the application before
103+
// Sentry gets a chance to send the event. Since we've enabled caching however,
104+
// the event will be written to disk and sent the next time the app is launched.
84105
SentrySdk.CauseCrash(CrashType.Native);
85106
#pragma warning restore CS0618 // Type or member is obsolete
86107
};
87108

88109
// create a UIStackView to hold the label and buttons
89-
var stackView = new UIStackView(new UIView[] { label, managedCrashButton, nativeCrashButton })
110+
var stackView = new UIStackView(new UIView[] { label, managedCrashButton, unhandledCrashButton, nativeCrashButton })
90111
{
91112
Axis = UILayoutConstraintAxis.Vertical,
92113
Distribution = UIStackViewDistribution.FillEqually,
@@ -112,21 +133,6 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
112133
// make the window visible
113134
Window.MakeKeyAndVisible();
114135

115-
AppDomain.CurrentDomain.UnhandledException += (_, _) =>
116-
{
117-
Console.WriteLine("In UnhandledException Handler");
118-
};
119-
120-
Runtime.MarshalManagedException += (_, _) =>
121-
{
122-
Console.WriteLine("In MarshalManagedException Handler");
123-
};
124-
125-
Runtime.MarshalObjectiveCException += (_, _) =>
126-
{
127-
Console.WriteLine("In MarshalObjectiveCException Handler");
128-
};
129-
130136
return true;
131137
}
132138
}

0 commit comments

Comments
 (0)