从Objective-C向Google电子表格表单提交数据

时间:2012-09-10 19:13:55

标签: objective-c ios google-sheets

是否有图书馆,博客文章等可用于使用表单向Google电子表格提交数据?

我可能会写出Http POST的东西,但我想知道是否有人已经这样做,我可以利用。

这个问题类似于iOS的this问题的iOS等等。

5 个答案:

答案 0 :(得分:11)

根据@Saad Farooq的回答,我实际上尝试编写代码

- (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];






}

它确实有效。您需要从@ Saad的教程here中提取URL等。顺便说一句,我是那个尝试发布以前链接的人。非常感谢通知他的SO工作人员。

答案 1 :(得分:4)

我发现提交数据的Google Spreadsheet Forms方法更加轻松,并在博客上发表了here

来自iOS的简单HTTP POST会处理它。

答案 2 :(得分:3)

gdata-objectivec-client。他们有一些使用电子表格here的示例代码。

答案 3 :(得分:3)

我使用Swift完成了这项任务。请在此回购中查看:https://github.com/goktugyil/QorumLogs

以下是如何设置它的教程: https://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md

继承代码:

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)
}

答案 4 :(得分:0)

为了使用限制到某些电子邮件地址并自动发送用户名(电子邮件地址),您需要在表单中提交隐藏字段

fbzx 通气量 draftResponse pageHistory token(这不是访问令牌,它是名为“token”的形式的隐藏输入,使用类似

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

执行限制某些电子邮件时所需的访问令牌

使用cocoapods

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

在OSX示例中显示auth(包含在GTMOAuth2的cocoapod中)

首先必须HTTP格式化表单html并解析它(使用NSScanner是一种解析它的好方法),用于隐藏的输入字段,如fbzx和draftResponse以及令牌。

然后当你拥有它并且你的访问令牌来自google auth时,你会发一个看起来像这样的帖子:

draftResponse = stringGottenFromHTMLGETAndParse&安培; entry.someNumber = someValue中&安培; fbzx = stringGottenFromHTMLGETAndParse2&安培;通气量= stringGottenFromHTMLGETAndParse3&安培; pageHistory = stringGottenFromHTMLGETAndParse4&安培;标记= stringGottenFromHTMLGETAndParse5

当将提交者限制在表单中的某些电子邮件地址但不需要隐藏的输入(例如draftResponse和token)时,我需要为HTTP Post执行GTMOAuth2工作。

当我在表单上启用了选项以自动提交带有表单的用户电子邮件时,我需要为HTTP POST添加隐藏的输入(例如fbzx)。