Welcome to the Friendly Chat codelab. In this codelab, you'll learn how to use the Firebase platform to create iOS applications. You will implement a chat client, and monitor its performance using Firebase.
This codelab is also available in Swift.
Clone the GitHub repository from the command line.
$ git clone https://github.com/firebase/friendlychat-ios
To build the starter app:
grow-ios-starter/objc-starter
directory from your sample code download pod install
From Firebase console select Add Project.
Call the project FriendlyChat
, then click on Create Project.
com.google.firebase.codelab.FriendlyChatObjC
".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 FriendlyChatObjC 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.
We will now add a rule to require authentication before reading or writing any messages. To do this we add the following rules to our messages data object. From within the Database section of Firebase console select the RULES tab. Then update the rules so they look like this:
{
"rules": {
"messages": {
".read": "auth != null",
".write": "auth != null"
}
}
}
For more information on how this works (including documentation on the "auth" variable) see the Firebase security documentation.
Before your application can access the Firebase Authentication APIs on behalf of your users, you will have to enable it
If you get errors later in this codelab with the message "CONFIGURATION_NOT_FOUND", come back to this step and double check your work.
You'll need to add a custom URL scheme to your XCode project.
Firebase In-App Messaging helps you engage your app's active users by sending them targeted, contextual messages that encourage them to use key app features.
Confirm the pod 'Firebase/InAppMessagingDisplay'
dependency exists in your Podfile
file.
Device testing requires a FirebaseInstanceID. Find your testing app's Instance ID by running the app with the runtime command argument -FIRDebugEnabled, and looking for the following line in the Xcode console's logs:
[Firebase/InAppMessaging][I-IAM180017] Starting InAppMessaging runtime with Instance ID YOUR_APP_ID
Go to Product > Scheme > Edit Scheme. Then select the Arguments tab and add the flag to `Arguments passed on launch`
Click the Run button. Then search for the line with "Starting InAppMessaging" in the Xcode console. Make note of the Instance ID.
From the Firebase console, select `In-App Messaging` from the navigation panel. Select `Start your first campaign`. Enter "Welcome back" for the title and "Good to see you again" for the body. Then click Test on your Device. Enter the Instance ID in the available field and click the + sign to add it. Then click Test.
Firebase Analytics provides a way for you to understand the way users move through your application, where they succeed and where they get stuck and turn back. It can also be used to understand the most used parts of your application.
Add measurement helper methods.
+ (void)sendLoginEvent {
[FIRAnalytics logEventWithName:kFIREventLogin parameters:nil];
}
+ (void)sendLogoutEvent {
[FIRAnalytics logEventWithName:@"logout" parameters:nil];
}
+ (void)sendMessageEvent{
[FIRAnalytics logEventWithName:@"message" parameters:nil];
}
If you want to view this activity in your Firebase console, select Product ... Scheme... Edit Scheme in Xcode. In the Arguments Passed on Launch section, click the + to add a new argument and add add -FIRAnalyticsDebugEnabled as the new argument.
Firebase Crashlytics allows your application to report when crashes occur and log the events leading up to the crash.
Confirm that pod 'Fabric', '~> 1.7.2' and pod 'Crashlytics', '~> 3.9.3' dependencies exist in your Podfile file.
${PODS_ROOT}/Fabric/run
$(BUILT_PRODUCTS_DIR)/$(INFOPLIST_PATH)
- (IBAction)didPressCrash:(id)sender {
[[Crashlytics sharedInstance] crash];
}
Firebase Remote Config allows you to remotely configure your active clients. FriendlyChat messages are restricted to a maximum length. While this length can be defined directly in the client, defining this maximum length with Firebase Remote Config allows an update to the maximum length to be applied to active clients.
In Firebase console, select the "Remote Config" panel and click "Add your first parameter". Set the parameter key to friendly_msg_length and the parameter value to 10. Select Publish Changes to apply the updates.
Confirm the pod 'Firebase/RemoteConfig'
dependency exists in your Podfile
file.
- (void)configureRemoteConfig {
_remoteConfig = [FIRRemoteConfig remoteConfig];
// Create Remote Config Setting to enable developer mode.
// Fetching configs from the server is normally limited to 5 requests per hour.
// Enabling developer mode allows many more requests to be made per hour, so developers
// can test different config values during development.
FIRRemoteConfigSettings *remoteConfigSettings = [[FIRRemoteConfigSettings alloc] initWithDeveloperModeEnabled:YES];
self.remoteConfig.configSettings = remoteConfigSettings;
}
Create a fetch request for config and add a completion handler to pick up and use the config parameters.
- (void)fetchConfig {
long expirationDuration = 3600;
// If in developer mode cacheExpiration is set to 0 so each fetch will retrieve values from
// the server.
if (self.remoteConfig.configSettings.isDeveloperModeEnabled) {
expirationDuration = 0;
}
// cacheExpirationSeconds is set to cacheExpiration here, indicating that any previously
// fetched and cached config would be considered expired because it would have been fetched
// more than cacheExpiration seconds ago. Thus the next fetch would go to the server unless
// throttling is in progress. The default expiration duration is 43200 (12 hours).
[self.remoteConfig fetchWithExpirationDuration:expirationDuration completionHandler:^(FIRRemoteConfigFetchStatus status, NSError *error) {
if (status == FIRRemoteConfigFetchStatusSuccess) {
NSLog(@"Config fetched!");
[_remoteConfig activateFetched];
FIRRemoteConfigValue *friendlyMsgLength = _remoteConfig[@"friendly_msg_length"];
if (friendlyMsgLength.source != FIRRemoteConfigSourceStatic) {
_msglength = friendlyMsgLength.numberValue.intValue;
NSLog(@"Friendly msg length config: %d", _msglength);
}
} else {
NSLog(@"Config not fetched");
NSLog(@"Error %@", error);
}
}];
}
Firebase A/B Testing helps you optimize your app experience by making it easy to run, analyze, and scale product and marketing experiments. Here you will use Firebase A/B testing to try different message lengths.
Click the Run button. Then search for the line with "Remote instance ID token" in the Xcode console. Make note of the token.
You have used Firebase to take Friendly Chat to the next level.