close
use-gravity-forms-api-as-backend-for-ios
I’m very excited to share this code here. I’ve been working on wordpress theme customization for 4 years. In this period I’ve used many plugins to perform different tasks.
I started working with iOS from approx. 1.5 years now and since then I had a lot of ideas which I wanted to make but I always felt bad as I don’t know much about backend so I was stuck with a lot of things. Previously I found that there is a plugin called JSON API which gives you CRUD for your posts. Here is the tutorial for that.

As far as this is concerned so what I knew was that the backend actually takes basic CRUD functions. So, recently I was working with Gravity Forms. I’ve been working with gravity Forms since two years and since then I’ve found a lot of great things we can do with it. It’s truly a remarkable plugin for WordPress. I discovered that there is an API option for gravity forms. So it’s been 1 day since I was searching and learning about it. And now finally I’ve been able to GET the JSON of my form entries.A complete URL will look like this:
http://mydomain.com/gravityformsapi/forms/1?api_key=fd144510ac&signature=Nj6zoDHF0wAxuFynQPFF29U3%2FEE%3D&expires=1424785599
Notice the URL has 3 query arguments:
  1. api_key: the public key
  2. expires: the expiration time expressed as a UNIX UTC timestamp
  3. signature: a url-encoded HMAC-SHA1 hash of a combination of the public api key, requested API route, HTTP method and expiration time. Example:
    {api_key}:{http method}:{route}:{expires}
    1234:GET:forms/1/entries:1369749344

We will implement it all.

Here is the step by step guide:

I’m using Gravity Form plugin Version 1.9.14
I’ve created a simple form with three fields:

Let add a sample entry into it.
Let’s activate the API: Select Settings in Gravity Forms
Select API and Check the box which says enable access to the API

Here are your keys which you’ll be using later on:

That’s it for the web part.

How to GET using GFAPI:


Open xcode and create a new project.

In your ViewController.m

After that make a method: -(void)gravityAPI
and inside this method:


Run the code and the URL you’ll get in NSLog copy and paste it in your browser and You’ll get the response 200. So now use this data as you like and create awesome things. 🙂

To get the code of this project click here.

I’ve very excited to take this thing further and start creating my apps.

How to POST using GFAPI:


Create a new project just for better understanding. Add afnetworking into it by using pods or manually.
Before we get any further lets understand how the POST request works. So the end points of form submission has the end point as:

POST /forms/[Form ID]/submissions

This is the only endpoint which does not require authentication. Well if any of the other end point requires it you know how to make signature now as it was explained in the GET request above.
So in order to make this call you need two things:

  • URI: /gravityformsapi/forms/[Form ID]/submissions
  • Input: A JSON object containing each of the name-value pairs.

Let dive into that. Open your ViewController.m and #import “AFNetworking.h” below that initialize static NSString * const BaseURLString = @”http://YOUR_DOMAIN.com/gravityformsapi/”; and NSString *http_method;

NSString *route;

Make a method -(void)afNetworkingPost and we will do everything inside of it. So before you start coding it just call [self afNetworkingPost]; in your viewDidLoad method.

Okay so inside afNetworkingPost method set http_method = @”POST”;

    route = @”forms/2/submissions”; and set your base URL as NSString *string = [NSString stringWithFormat:@”%@%@”,BaseURLString,route];
 
after that

Now till now you’ve completed the first part of it which is the URI. You’ve successfully created that. Now the second part is a little tricky but it’s very simple. Here I’m sharing a chunk of code which I’ll explain what is happening in these lines:


1: AFJSONRequestSerializer is a subclass of AFHTTPRequestSerializer that encodes parameters as JSON using NSJSONSerialization. You don’t need to change anything there just copy/paste it as it is.

2: This might be tricky. If you hover over a field in form editor of your gravity forms you can see the field ID as shown in the picture.

So for example if your field ID is 1 you will write it as input_1 and similarly with 2 it’ll be input_2 and so on. My form have 3 fields with ID’s 1,2,3 so that is why I’ve used input_1 to input_3.
3: As in the official documentation it was required to pass JSON Object in input_values so the modify dictionary is doing that. Putting our parameters inside another key value.
and with this we are done with the second step too! So now we have all the two things required to make this call so lets make this call. (Make sure you’ve added AFNetworking) library.

Run the code and it’ll work like a charm. Here is the full code with breaking:

How to GET a single entry (search parameter):


This is a great approach to get just a single entry. It can be used in login or many other useful things. The good thing is that it is same as GET request discussed above but with a new parameter.
You might have used search on web part of gravity forms while working on them. It’s the similar

We are going to implement this.

So lets assume we have a login form in iOS app as

and on gravity forms I’ve the similar fields

and lets make a new method as we have been doing throughout this tutorial

This step is similar you fill your information here. As my form ID is 3 so I’m using it accordingly.
I’ve created a method where I pass all the data and it returns me signature. So I’m going to use it here. You can make the signature as the same way you did in the GET request earlier.
Now this is the most important part while doing a search query. Here three things are important:
  • KEY
  • VALUE
  • OPERATOR
    (All of these should be in small letters. i’ve just user uppercase to emphasize on the words.)

Where as field_filters is like a box or wrapping paper in which you wrap the three important things. KEY is the ID of the field. Notice in the last image above I use the Email field has the Field ID 2 so that is why I’ve used 2 as KEY. Next comes the VALUE which will be the search term so I’ve entered the email and similarly the OPERATOR which is contains. It can be is, isnot and may others like they appear on the web.

Now comes the final part which is the URL. In URL you just need to add a parameter search which will be equal to your NSString you created above.
Rest of the stuff is same.
If you run the code now and if it finds the value it’ll show it. Otherwise there will be response 200 but the total_count will be 0.

Possible Errors:

I faced a lot of error while doing these tasks. So after much searching and consulting experts I found solutions for them. So here are some of the possible errors you might face:

1. App Transport security Error

If you get this error App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file.
Open your info.plist as source code and add this code in it.

2. Permission Denied 401 Error

SOLUTION 1

This is the error that took a lot of my time to figure out. But as always the solution was very simple. This happened when I tried to pass parameters with GET in search method.

Now imagine you have created the URL following this tutorial and it didn’t work and you got 401. Then after a little time you realized the error that Opps! The parameters I was passing needed a bracket and you run the code again and you again got 401.
Why is that?

This is because the parameters which are apiKey, Signature and Expire time are the same and you only changed the other parameters with your GET request. However these three parameters are used to authenticate the user so that means the old signature which was generated to deny the permission will deny it again no matter what.

So to fix that I just changed the expire time from 1577922200 to 1577933200. I could’ve changed it to anything but the thing is I just need to give something new so that a new signature can be generated. So when I changed it started working.

SOLUTION 2

While making the signature using SHA1 you use NSString *string_to_sign = [NSString stringWithFormat:@"%@:%@:%@:%@",api_key,http_method,route,expires]; as per the documentation. But in order to make CCHmac you have to pass it two things:

  1. Key
  2. Data
and based on the link it is created as

So what I was mistaking is that I was using API Key in cKey instead of API Private Key. So I change it as per tutorial said and it worked. Otherwise I was getting 401 not matter what I try.
SOLUTION 3

Select Web API in GFAPI settings on wordpress.

Select Open Developer Tools

Here you just put the route. Not the parameters at the moment. The point to note here is that I was using version 2.0.6 while creating this tutorial so the Generate URL button works on it. I came to know that on version 1.9.6 I think it was not working. So you need to update your gravity form plugin.

After the URL is generated copy it and paste it in the browser, if it works correctly then you have to repeat solution 1 and 2 again just to make sure, if the problem is still there the that means you are mentioning the new parameter wrong. For example, if you’re doing search you need to provide a JSON string to the search parameter. The correct form of it is:

If you’re even missing a bracket or a comma it’ll show 401. Make sure you’re providing the correct solution.

If none of the solutions works for you feel free to leave that in comments and I’ll try my best to answer the query.

Tags : gfapigravity formsios
Chaudhary Talha

The author Chaudhary Talha

I'm an iOS developer by profession and UI/UX designer by passion. All my tutorials are based on my personal experiences and whenever I learn something new I share it with the world.

1 Comment

Comments are closed.