Saad Farooq
Saad Farooq

Reputation: 13402

Submit data to Google Spreadsheet Form from Objective-C

Is there a library, blog post, etc. that can be used to submit data to a Google Spreadsheet using Forms ?

I could probably write up the Http POST stuff but I was wondering if someone had done so already that I can utilize.

This question is like the iOS equivalent of this question for Android.

Upvotes: 5

Views: 4984

Answers (5)

Doug Voss
Doug Voss

Reputation: 1022

For it to work with restrict to certain email addresses and to send the Username (the email address) automatically then you need to submit the hidden fields in the form

fbzx fvv draftResponse pageHistory token (this is not the access token, it is a hidden input in the form called "token", use something like

NSString *authValue = [NSString stringWithFormat:@"Bearer %@", yourAccessTokenGottenFromGTMOAuth];
[request setValue:authValue forHTTPHeaderField:@"Authorization"]; 

to do the access token you need for it to work when restricted to certain emails

Use cocoapods

pod 'GTMOAuth2', '~> 1.1.2'
pod 'GTMSessionFetcher', '~> 1.1'

Do the auth as the OSX example shows (included in the cocoapod for GTMOAuth2)

You must first HTTP GET the form html and parse it (using NSScanner is a good way to parse it) for the hidden input fields such as fbzx and draftResponse and token.

Then when you have that and your access token gotten from google auth you do a post that looks something like:

draftResponse=stringGottenFromHTMLGETAndParse&entry.someNumber=someValue&fbzx=stringGottenFromHTMLGETAndParse2&fvv=stringGottenFromHTMLGETAndParse3&pageHistory=stringGottenFromHTMLGETAndParse4&token=stringGottenFromHTMLGETAndParse5

I needed to do the GTMOAuth2 for the HTTP Post to work when restricting submitters to certain email addresses in the form but didn't need the hidden inputs such as draftResponse and token.

I needed to add the hidden inputs such as fbzx for the HTTP POST to work when I had the option enabled on the form to automatically submit the users email with the form.

Upvotes: 0

Esqarrouth
Esqarrouth

Reputation: 39181

I did this task by using Swift. Check it out in this repo: https://github.com/goktugyil/QorumLogs

Here is the tutorial of how to set it up: https://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md

Heres the code to do it:

private static var googleFormLink: String!
private static var googleFormAppVersionField: String!
private static var googleFormUserInfoField: String!
private static var googleFormMethodInfoField: String!
private static var googleFormErrorTextField: String!

/// Setup Google Form links
static func setupOnlineLogs(#formLink: String, versionField: String, userInfoField: String, methodInfoField: String, textField: String) {
    googleFormLink = formLink
    googleFormAppVersionField = versionField
    googleFormUserInfoField = userInfoField
    googleFormMethodInfoField = methodInfoField
    googleFormErrorTextField = textField
}

private static func sendError(#text: String) {
    var url = NSURL(string: googleFormLink)
    var postData = googleFormAppVersionField + "=" + text
    postData += "&" + googleFormUserInfoField + "=" + "anothertext"                
    postData += "&" + googleFormMethodInfoField + "=" + "anothertext" 
    postData += "&" + googleFormErrorTextField + "=" + "anothertext" 

    var request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"
    request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding)
    var connection = NSURLConnection(request: request, delegate: nil, startImmediately: true)
}

Upvotes: 3

Robin Thuran Malhotra
Robin Thuran Malhotra

Reputation: 594

based on @Saad Farooq's answer, I actually tried writing the code

- (void)viewDidLoad
{
    [super viewDidLoad];

    //if there is a connection going on just cancel it.
    [self.connection cancel];

    //initialize new mutable data
    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData = data;

    //initialize url that is going to be fetched.
    NSURL *url = [NSURL URLWithString:@"https://docs.google.com/forms/d/1yffvViDKq7BHALtC7Om-ceFLWT5hb_cM9sBqndHG3aU/formResponse"];

    //initialize a request from url
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];

    //set http method
    [request setHTTPMethod:@"POST"];
    //initialize a post data
    NSString *postData = @"entry.154268020=iOS&entry.940479455=vajhcsd&entry.247556683=BJKSVDB";
    //set request content type we MUST set this value.

    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    //set post data of request
    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];

    //initialize a connection from request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.connection = connection;

    //start the connection
    [connection start];






}

and it actually works. You need to extract the URLs etc from @Saad's tutorial here. BTW, I was the one who tried posting that the previous links. A big thank you to the SO staff who informed him.

Upvotes: 11

Saad Farooq
Saad Farooq

Reputation: 13402

I found the Google Spreadsheet Forms method of submitting data to be much easier and blogged about here

A simple HTTP POST from iOS then takes care of it.

Upvotes: 4

sbridges
sbridges

Reputation: 25150

There is the gdata-objectivec-client. They have some example code for using spreadsheets here.

Upvotes: 3

Related Questions