This document explains how to start developing with the Nearby Messages API on iOS.
Step 1: Get the latest version of Xcode
To build a project using the Google Nearby Messages API for iOS, you need version 6.3 or later of Xcode.
Step 2: Get CocoaPods
The Google Nearby Messages API for iOS is available as a CocoaPods pod. CocoaPods is an open source dependency manager for Swift and Objective-C Cocoa projects.
If you don't already have the CocoaPods tool, install it on OS X by running the following command from the terminal. For details, see the CocoaPods Getting Started guide.
$ sudo gem install cocoapods
Step 3: Install the API using CocoaPods
Create a Podfile
for the Google Nearby Messages API for iOS and use it to
install the API and its dependencies.
- If you don't have an Xcode project yet, create one now and save it to your local machine. (If you're new to iOS development, create a Single View Application, and ensure that Use Automatic Reference Counting is turned on.)
- Create a file named
Podfile
in your project directory. This file defines your project's dependencies. Edit the
Podfile
and add your dependencies. Here is a simple Podspec, including the name of the pod you need for the Google Nearby Messages API for iOS:source 'https://github.com/CocoaPods/Specs.git' platform :ios, '7.0' pod 'NearbyMessages'
Save the
Podfile
.Open a terminal and go to the directory containing the
Podfile
:$ cd
Run the
pod install
command. This will install the APIs specified in the Podspec, along with any dependencies they may have.$ pod install
Close Xcode, and then open (double-click) your project's
.xcworkspace
file to launch Xcode. From this time onwards, you must use the.xcworkspace
file to open the project.
Step 4: Create a bridging header (Swift only)
If you created your project using Swift, you must add a bridging header to your project so that it can use the libraries that were installed by the pod file. Take these steps to add a bridging header:
- In the same directory as your project's
.xcworkspace
file, add a new header file (name it whatever you like and use a filename extension of ".h"). - Paste the following code into the file you created:
#import <GNSMessages.h>
. - Save the file.
- In the project editor left pane, select the main project.
- Click Build Settings at the top of the build editor.
- In Build Settings, navigate to "Swift Compiler - Code Generation" (type "Swift Compiler" into the search box to locate it quickly).
- Click the arrow to expand the Objective-C Bridging Header section.
- Under Debug and Release, add the path to the header file you created.
- Build the project to make the changes take effect.
Step 5: Get a Google account
To use the Nearby Messages APIs, you need a Google Account. If you already have an account, then you're all set. You may also want a separate Google Account for testing purposes.
Step 6: Get an API key
Take these steps to enable the Google Nearby Messages API for iOS and get an API key:
- Go to the Google Developers Console.
- Create or select a project to register your application with.
- Click Continue to Enable the API.
- On the Credentials page, create a new iOS key (and set the API
Credentials).
Note: If you have an existing iOS key, you may use that key. - In the resulting dialog, enter your app's bundle identifier. For example:
com.example.nearbyexample
- Your new iOS API key appears in the list of API keys for your project.
An API key is a string of characters, something like this:
AIzaSyBdVl-cTICSwYKrZ95SuvNw7dbMuDt1KG0
- To prevent quota theft, secure your API key following these best practices.
Step 7: Create a message manager object
The message manager object lets you publish and subscribe. Message exchange is unauthenticated, so you must supply the API key you created in the previous step.
Objective-C
#import <GNSMessages.h>
GNSMessageManager *messageManager =
[[GNSMessageManager alloc] initWithAPIKey:@"API_KEY"];
Swift
let messageManager = GNSMessageManager(APIKey: "API_KEY")