1+ using ObjCRuntime ;
2+
13namespace Sentry . Samples . Ios ;
24
35[ Register ( "AppDelegate" ) ]
@@ -16,84 +18,122 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
1618 {
1719 options . Dsn = "https://[email protected] /5428537" ; 1820 options . Debug = true ;
21+ options . SampleRate = 1.0F ;
1922 options . TracesSampleRate = 1.0 ;
2023 options . ProfilesSampleRate = 1.0 ;
2124
2225 // All the native iOS SDK options are available below
2326 // https://docs.sentry.io/platforms/apple/guides/ios/configuration/
2427 // Enable Native iOS SDK App Hangs detection
2528 options . Native . EnableAppHangTracking = true ;
29+
30+ options . CacheDirectoryPath = Path . GetTempPath ( ) ;
2631 } ) ;
2732
2833 // create a new window instance based on the screen size
2934 Window = new UIWindow ( UIScreen . MainScreen . Bounds ) ;
3035
31- // determine the background color for the view (SystemBackground requires iOS >= 13.0)
36+ // determine control colours (SystemBackground requires iOS >= 13.0)
3237 var backgroundColor = UIDevice . CurrentDevice . CheckSystemVersion ( 13 , 0 )
3338#pragma warning disable CA1416
3439 ? UIColor . SystemBackground
3540#pragma warning restore CA1416
3641 : UIColor . White ;
42+ var buttonConfig = UIButtonConfiguration . TintedButtonConfiguration ;
43+ var terminalButtonConfig = UIButtonConfiguration . TintedButtonConfiguration ;
44+ terminalButtonConfig . BaseBackgroundColor = UIColor . SystemRed ;
3745
38- // create a UIViewController with a single UILabel
3946 var vc = new UIViewController ( ) ;
40- vc . View ! . AddSubview ( new UILabel ( Window ! . Frame )
47+
48+ var label = new UILabel
4149 {
4250 BackgroundColor = backgroundColor ,
4351 TextAlignment = UITextAlignment . Center ,
4452 Text = "Hello, iOS!" ,
53+ AutoresizingMask = UIViewAutoresizing . All
54+ } ;
55+
56+ // UIButton for a managed exception that we'll catch and handle (won't crash the app)
57+ var managedCrashButton = new UIButton ( UIButtonType . RoundedRect )
58+ {
4559 AutoresizingMask = UIViewAutoresizing . All ,
46- } ) ;
47- Window . RootViewController = vc ;
60+ Configuration = buttonConfig
61+ } ;
62+ managedCrashButton . SetTitle ( "Managed Crash" , UIControlState . Normal ) ;
63+ managedCrashButton . TouchUpInside += delegate
64+ {
65+ Console . WriteLine ( "Managed Crash button clicked!" ) ;
66+ try
67+ {
68+ throw new Exception ( "Catch this!" ) ;
69+ }
70+ catch ( Exception e )
71+ {
72+ SentrySdk . CaptureException ( e ) ;
73+ }
74+ } ;
4875
49- // make the window visible
50- Window . MakeKeyAndVisible ( ) ;
76+ // UIButton for unhandled managed exception
77+ var unhandledCrashButton = new UIButton ( UIButtonType . RoundedRect )
78+ {
79+ AutoresizingMask = UIViewAutoresizing . All ,
80+ Configuration = terminalButtonConfig
81+ } ;
82+ unhandledCrashButton . SetTitle ( "Unhandled Crash" , UIControlState . Normal ) ;
83+ unhandledCrashButton . TouchUpInside += delegate
84+ {
85+ Console . WriteLine ( "Unhandled Crash button clicked!" ) ;
86+ string s = null ! ;
87+ // This will cause a NullReferenceException that will crash the app before Sentry can send the event.
88+ // Since we're using a caching transport though, the exception will be written to disk and sent the
89+ // next time the app is launched.
90+ Console . WriteLine ( "Length: {0}" , s . Length ) ;
91+ } ;
5192
93+ // UIButton for native crash
94+ var nativeCrashButton = new UIButton ( UIButtonType . System )
95+ {
96+ Configuration = terminalButtonConfig
97+ } ;
98+ nativeCrashButton . SetTitle ( "Native Crash" , UIControlState . Normal ) ;
99+ nativeCrashButton . TouchUpInside += delegate
100+ {
101+ Console . WriteLine ( "Native Crash button clicked!" ) ;
102+ #pragma warning disable CS0618 // Type or member is obsolete
103+ // This will cause a native crash that will crash the application before
104+ // Sentry gets a chance to send the event. Since we've enabled caching however,
105+ // the event will be written to disk and sent the next time the app is launched.
106+ SentrySdk . CauseCrash ( CrashType . Native ) ;
107+ #pragma warning restore CS0618 // Type or member is obsolete
108+ } ;
52109
53- // Try out the Sentry SDK
54- SentrySdk . CaptureMessage ( "From iOS" ) ;
110+ // create a UIStackView to hold the label and buttons
111+ var stackView = new UIStackView ( new UIView [ ] { label , managedCrashButton , unhandledCrashButton , nativeCrashButton } )
112+ {
113+ Axis = UILayoutConstraintAxis . Vertical ,
114+ Distribution = UIStackViewDistribution . FillEqually ,
115+ Alignment = UIStackViewAlignment . Center ,
116+ Spacing = 10 ,
117+ TranslatesAutoresizingMaskIntoConstraints = false ,
118+ } ;
55119
56- // Uncomment to try these
57- // throw new Exception("Test Unhandled Managed Exception") ;
58- // SentrySdk.CauseCrash(CrashType.Native );
120+ // add the stack view to the view controller's view
121+ vc . View ! . BackgroundColor = backgroundColor ;
122+ vc . View . AddSubview ( stackView ) ;
59123
60- {
61- var tx = SentrySdk . StartTransaction ( "app" , "run" ) ;
62- var count = 10 ;
63- for ( var i = 0 ; i < count ; i ++ )
64- {
65- FindPrimeNumber ( 100000 ) ;
66- }
124+ // set constraints for the stack view
125+ NSLayoutConstraint . ActivateConstraints ( [
126+ stackView . CenterXAnchor . ConstraintEqualTo ( vc . View . CenterXAnchor ) ,
127+ stackView . CenterYAnchor . ConstraintEqualTo ( vc . View . CenterYAnchor ) ,
128+ stackView . WidthAnchor . ConstraintEqualTo ( vc . View . WidthAnchor , 0.8f ) ,
129+ stackView . HeightAnchor . ConstraintEqualTo ( vc . View . HeightAnchor , 0.5f )
130+ ] ) ;
67131
68- tx . Finish ( ) ;
69- }
132+ Window . RootViewController = vc ;
70133
71- return true ;
72- }
134+ // make the window visible
135+ Window . MakeKeyAndVisible ( ) ;
73136
74- private static long FindPrimeNumber ( int n )
75- {
76- int count = 0 ;
77- long a = 2 ;
78- while ( count < n )
79- {
80- long b = 2 ;
81- int prime = 1 ; // to check if found a prime
82- while ( b * b <= a )
83- {
84- if ( a % b == 0 )
85- {
86- prime = 0 ;
87- break ;
88- }
89- b ++ ;
90- }
91- if ( prime > 0 )
92- {
93- count ++ ;
94- }
95- a ++ ;
96- }
97- return ( -- a ) ;
137+ return true ;
98138 }
99139}
0 commit comments