Chapter 2. Native iOS (Objective-C)

Download

API Documentation

2.1. CocoaPods

Note

CocoaPods is available only on OSX.

CocoaPods is a Swift And Objective-C dependency manager for Xcode projects. It is built with Ruby, which comes pre-installed with OSX. It is used to distribute the RHMAP iOS Swift and iOS Objective-C Client SDKs.

2.1.1. Install CocoaPods Using RubyGems

To install CocoaPods using the RubyGems package manager, execute the following command. This requires the sudo command to be enabled in OSX.

sudo gem install cocoapods

As an optional part of the CocoaPods setup, you can store your Podspec metadata locally at ~/.cocoapods/repos. This helps to increase the dependency resolution speed and shortens the build time for your apps. To clone the spec-repo and create the directory, execute the following command:

pod setup

2.1.2. Install CocoaPods Without Using sudo.

Alternatively, to install CocoaPods without using sudo, follow the Sudo-less Installation section of the CocoaPods Getting Started Guide.

2.1.3. Install the Required Plugins

The RHMAP iOS Objective-C and Swift SDK packages rely on the cocoapods-packager and cocapods-appledoc plugins. To install both plugins, execute the following command:

[sudo] gem install cocoapods-packager cocoapods-appledoc

2.1.4. Enable CocoaPods in an Xcode Project

To enable Cocoapods Xcode app project after installation execute the following commands:

  1. To navigate to the folder of your Xcode project, use:

    cd <project_directory>
  2. To create a podfile (if it does not already exist) in your project folder and automatically populate it with targets specified within the project, execute the following command:

    pod init

2.1.5. Install Dependencies Using CocoaPods

To install the dependencies defined in the podfile of Xcode project using CocoaPods, execute the following command:

pod install

2.2. Get Started

This SDK lets you use RHMAP APIs in Objective-C apps for all iOS versions supported by RHMAP.

The RHMAP iOS Objective-C SDK is an open-source project hosted in the FeedHenry iOS SDK repository on Github. Feel free to fork it and make a contribution to this project.

Before using this SDK, make sure you have the Xcode IDE installed. You can download Xcode from the Apple Developer Portal.

If you plan to use CocoaPods to manage dependencies in your project, you must install it first:

sudo gem install cocoapods

2.2.1. New App

When starting with a new template app, you have two options:

  • either use CocoaPods (the recommended approach and the default in Studio)
  • or use FH framework.

2.2.1.1. Using CocoaPods

Clone the sample app to get started with a new iOS application which has the RHMAP SDK included as CocoaPods dependency.

git clone https://github.com/feedhenry-templates/blank-ios-app.git
cd blank-ios-app

Fetch the dependencies defined in the Podfile:

pod install

Open the blank-ios-app.xcworkspace workspace in Xcode. The required dependencies are located in the Pods group.

2.2.1.2. Using FH Framework

Clone the sample app to get started with a new iOS application which has the RHMAP SDK already included.

git clone https://github.com/feedhenry-templates/blank-ios-app.git

Open the blank-ios-app.xcworkspace workspace in Xcode.

2.2.2. Existing App

When you are integrating an existing app with RHMAP, you have two options:

  • if your app uses CocoaPods, add the FH SDK as a new pod,
  • otherwise add the FH framework to your Xcode project.

2.2.2.1. Using CocoaPods

Open the Podfile and add the following dependency:

pod FH, '3.1.1'

Replace 3.1.1 with the version of RHMAP iOS Objective-C SDK you are targeting. If you do not specify a version number, the latest version in the CocoaPods central repository will be used.

2.2.2.2. Using FH Framework

Download the SDK and save it as fh-framework-latest.zip.

To integrate the SDK, extract the FH.framework file from the Zip file you downloaded above.

image

Once downloaded, drag the FH.framework file to your Xcode Project.

image

In the dialog, accept the defaults and click Finish.

image

2.2.3. Integration

The RHMAP iOS SDK has a number of framework and library dependencies — you must re-configure your Build Phases to link against these frameworks and libraries at build time for the SDK to compile in your project correctly. The libraries you must link against are:

  • libxml2.dylib
  • libz.dylib
  • SystemConfiguration.framework
  • CFNetwork.framework
  • MobileCoreServices.framework

To add these, go to Build Phases > Link Binary with Libraries and add the above dependencies as link dependencies.

image

You will also need to add a Linker Flag to your app. To add this, go to the Build Settings screen of your project’s target. Use the search to find Other linker flags, and add the following:

-Objective-C

image

Finally, add a Property List file called fhconfig.plist to your Project.

image

image

image

2.2.4. Setup

You must define the properties which allow your app to communicate with RHMAP servers. Add the following values to the fhconfig.plist configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>host</key>
  <string>__RHMAP_Core_host__</string>
  <key>appid</key>
  <string>__Client_App_ID__</string>
  <key>projectid</key>
  <string>__Project_ID__</string>
  <key>appkey</key>
  <string>__Client_App_ API_key___</string>
  <key>connectiontag</key>
  <string>__Connection_tag__</string>
</dict>
</plist>

See Projects - Connections for more information on connections between Client Apps and Cloud Apps.

2.2.5. Initialization

Before invoking any cloud requests from the RHMAP iOS Swift SDK, you must first initialize it. Copy the following code snippet to your App Delegate’s didFinishLaunchingWithOptions: method, or any other location which ensures the code is called before any cloud requests:

#import <FH/FH.h>
#import <FH/FHResponse.h>

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.

  // Call a cloud side function when init finishes
  void (^success)(FHResponse *)=^(FHResponse * res) {
    // Initialisation is now complete, you can now make FHActRequest's
    NSLog(@"SDK initialised OK");
  };

  void (^failure)(id)=^(FHResponse * res){
    NSLog(@"Initialisation failed. Response = %@", res.rawResponse);
  };

  //View loaded, init the library
  [FH initWithSuccess:success AndFailure:failure];

  return YES;
}