Welcome to the Firebase App Quality codelab. In this codelab, you'll learn how to use the Firebase platform to create iOS applications. You will implement a simple app, monitor its performance, and report a crash using Firebase.

This codelab is also available in Swift.

What you'll learn

What you'll need

How will you use this tutorial?

Read it through only Read it and complete the exercises

How would rate your experience with building iOS apps?

Novice Intermediate Proficient

Clone the GitHub repository from the command line.

$ git clone https://github.com/firebase/appquality-codelab-ios

To build the starter app:

  1. In a terminal window, navigate to the android_studio_folder.pngios-starter/objc-starter directory from your sample code download
  2. Run pod update
  3. Open the AppQualityObjc.xcworkspace file to open the project in Xcode.
  4. Click the Run button.

You should see the App Quality home screen appear after a few seconds. The UI should appear. However, at this point you can't track specific performance or get crash reports.

Create project

From Firebase console select Add Project.

Call the project AppQuality, then click on Create Project.

Screenshot from 2015-11-06 14:13:39.png

Connect your iOS app

  1. From the welcome screen of your new project, click Add Firebase to your iOS app.
  2. Enter the bundle ID, e.g. ""com.google.firebase.codelab.AppQualityObjC".
  3. Enter the App Store id as "123456".
  4. Click Register App.

Add GoogleService-Info.plist file to your app

On the second screen click Download GoogleService-Info.plist to download a configuration file that contains all the necessary Firebase metadata for your app. Copy that file to your application and add it to the AppQualityObjC target.

You can now click the "x" in the upper right corner of the popup to close it -- skipping steps 3 and 4 -- as you will perform those steps here.

Import Firebase module

Start by making sure the Firebase module is imported.

AppDelegate.m, AQViewController.m

@import Firebase;

Configure Firebase in AppDelegate

Use the "configure" method in FIRApp inside the application:didFinishLaunchingWithOptions function to configure underlying Firebase services from your .plist file.

AppDelegate.m

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [FIRApp configure];
  return YES;
}

Firebase Performance Monitoring helps you collect performance data from your app, and then review and analyze that data in the Firebase console.

Confirm Firebase Performance Monitoring dependency

Confirm that pod 'Firebase/Performance' dependency exists in your Podfile file.

Automatic traces and network requests

Recompile your app. Automatic traces and HTTP/S network requests are now monitored.

Define a custom trace in your app

A custom trace is a report of performance data associated with some of the code in your app. To learn more about custom traces, see the Performance Monitoring overview. You can have multiple custom traces in your app, and it is possible to have more than one custom trace running at a time.

Start the trace before reading the file.

AQViewController.m

 NSString *fileName = [NSString stringWithFormat:@"%@/perfsamplelog.txt",
                        documentsDirectory];

  // Start tracing
  FIRTrace *trace = [FIRPerformance startTraceWithName:@"request_trace"];

  NSError *fileReadError;

Stop the trace at the completion of the request.

AQViewController.m

      dispatch_async(dispatch_get_main_queue(), ^{
        _imageView.image = [UIImage imageWithData:data];
      });

      [trace stop];

      NSString *contentToWrite = [contents stringByAppendingString:response.URL.absoluteString];

Add counters to your custom trace

Each custom trace can have one or more counters to count performance-related events in your app, and those counters are associated with the traces that create them.

Increment a counter with the log file's size.

AQViewController.m

 if (contents) {
    fileLength = contents.length;
  }

  [trace incrementCounterNamed:@"log_file_size" by:fileLength];

  NSString *target = @"https://www.gstatic.com/mobilesdk/160503_mobilesdk/logo/2x/firebase_96dp.png";

Use another counter for the number of requests.

AQViewController.m

- (void)viewDidLoad {
.
.
 [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:
.
.
    }] resume];

  [trace incrementCounterNamed:@"request_sent"];
}

Test Firebase Performance

  1. Click Build and then run the current scheme in Xcode to build your app on a device or simulator.
  2. Confirm that Performance Monitoring results appear in the Firebase console. Results should appear within 24 hours.

Firebase Crashlytics allows your application to report when crashes occur and log the events leading up to the crash.

Confirm Firebase Crashlytics dependency

Confirm that pod 'Fabric', '~> 1.7.6' and pod 'Crashlytics', '~> 3.9.3' dependencies exist in your Podfile file.

Add the run script build phase

  1. Open your project in Xcode and select its project file in the Navigator.
  2. Open the Build Phases tab.
  3. Click + Add a new build phase, and select New Run Script Phase.
  4. Add the following line to the Type a script... text box:
"${PODS_ROOT}/Fabric/run"

Add the code to initiate a crash

AQViewController.m

- (IBAction)didPressCrash:(id)sender {
  NSLog(@"Crash button pressed!");
//  assert(NO);
  [[Crashlytics sharedInstance] crash];
}

Adjust your project's debug settings

Crashlytics uses dSYM (debugging symbol) files to generate human-readable crash reports. Adjust your build settings to change the project's debug information format:

  1. With your project still selected in the Xcode Navigator, open the Build Settings tab.
  2. Click All at the top of the tab to display all build settings.
  3. Search for "debug information format".
  4. Set the Debug Information Format setting to DWARF with dSYM File.

Set up Crashlytics in the Firebase Console

You need to perform a one-time setup in the console before your results will appear.

In the Firebase console:

  1. Click Crashlytics (you may need to expand the Stability tab)
  2. Click Set up Crashlytics
  3. You will see a button to open the documentation. Click it; the documentation opens in a new tab.
  4. Come back to the Firebase console tab and you will see that Crashlytics is now waiting for your first crash report.

Test Firebase Crashlytics

Crashlytics can't capture crashes if your build attaches a debugger at launch

  1. Click Build and then run the current scheme in Xcode to build your app on a device or simulator.
  2. Click Stop running the scheme or action in Xcode to close the initial instance of your app. (This initial instance includes a debugger that interferes with Crashlytics.)
  3. Open your app again from the simulator or device.
  4. Touch Crash the App to crash the app.
  5. Open your app once more to let the Crashlytics API report the crash. Your crash should show up in the Firebase console within 5 minutes.

You have used Firebase to monitor the quality of an app.

What we've covered

Learn More