2015-01-10 89 views

回答

0
  1. 代碼服務器接受查詢。
  2. 代碼查詢服務器的iOS應用程序。也許使用AFNetworking。
  3. 從任何地方訪問數據。
0

您可以創建一個用php編寫的頁面,它接受從ios應用發送的數據,然後php將數據發送到服務器。服務器包含一個包含要填充的行的表。

或者您可以使用後端提供程序來爲您做這件事,下面是一個link來解釋它是如何工作的(來自raywenderlich)。

2

我強烈推薦的是Google Apps Script,無需服務器(使用Google的服務器)即可輕鬆完成,而且它的免費的。但是,你必須知道JavaScript(我可以幫助一個很好的數額,因爲我剛剛完成這項工作)

我將假設你在POST時(當你給一個服務器數據時,它被稱爲簡化事物),你想要退出(即一些JSON),說'成功'或提供更多的信息。另外,我現在要預先警告您:這不適用於企業軟件 - 因爲您需要真正的數據庫和服務器(即SQL或noSQL),但是要與其他非腳本編寫人員共享,這會爲您提供內置的可共享GUI (圖形用戶界面)。

好的,放到代碼上。轉到script.google.com並選擇左側的「空白項目」。現在3/4的代碼將處理請求b/c向谷歌電子表格添加一行實際上是一行(這是多麼強大)。

首先,在添加此功能:

function doGet(e) { 
    if (e.parameter.get) { 
     Logger.log("GETing") 
     //return getAsJSON() 
    } else if (e.parameter.post) { 
     Logger.log("POSTing") 
     //return dopost(e) 
    } else { 
     //If don't do anything, get as JSON 
     Logger.log("GETing") 
     //return getAsJSON() 
    } 
} 

去跑 - > doGet和接受權限的彈出(現在做到這一點,並在年底或任何時候,它提供了一個錯誤「授權才能執行「)

現在,轉到文件 - >管理版本 - >保存新版本 然後發佈 - >部署爲Web應用程序 - >使項目版本成爲最新的數字,並確保它是」執行應用程序如:「我和」誰有權訪問該應用程序:「任何人(甚至匿名)。

打開下方的「當前網站應用網址:和最新的代碼」鏈接。

第二個鏈接(最新的代碼)應該看起來像https://script.google.com/macros/s/AKfycbxHk_GXziSAwSH6umVyz3LnnbgpkA9BnqvL2ILeF/dev。在其末尾添加「?post = true」,然後刷新頁面。返回到文本編輯器頁面並按CTRL + 輸入。你應該看到「發佈」;將URL中的「post」替換爲「get」並刷新。退出「記錄輸出」,然後按CTRL + 再次輸入。你應該看到「正在接受」。

幾乎在那裏;現在,函數doPost(首先取消上述doGet函數中的所有內容,除了'//如果不做任何事情,請以JSON形式提交')並將您的URL更改回?從?得到。你非常不清楚你想添加到你的工作表,所以我會假設你想要添加一行2個單元格。添加到URL這樣的:

&column1="TESTING"&column2="WOW" 

這是什麼東西做的是告訴我們的代碼(1)我們想張貼和(2)的新行應該測試,WOW(在不同的細胞)。製作新的Google電子表格,並複製「d /」和「/ edit」之間的內容,將其放入電子表格和ID並放入電子郵件;如果出現問題,它會通過電子郵件發送給您,請保持開放。該代碼是

function doPost(e) { 
    try { 
     Logger.log(e) 
     if (!(e.parameter.column1) || !(e.parameter.column2)) { 
      MailApp.sendEmail("YOUR EMAIL HERE", "Problem with POST to gSheet", "Not correct parameters need column1 and column2 and both need to be '=something' ") 
     } else { 
      var allVals = SpreadsheetApp.openById("YOUR SPREADSHEET ID HERE").getActiveSheet().appendRow([e.parameter.column1, e.parameter.column2]) 
      return getAsJSON() 
     } 
    } catch (e) { 
     MailApp.sendEmail("YOUR EMAIL HERE", "Problem with POST to gSheet", e) 
    } 
} 

getASJSON

function getAsJSON(){ 
    output = ContentService.createTextOutput() 
    output.append("DONE") 
    return output 
} 

這是非常可能來回報您的電子表格的JSON但這已經超出了這個範圍,並很好的鍛鍊。

注意:它會將您重定向到「googleusercontent」域。如果是這樣,請不要使用該URL或刷新該URL:返回到/ dev或/ exec URL。


在iOS方面,您所要做的就是使用上面構建的URL進行POST請求,這非常簡單。你提到Objective-C,但我也會提供Swift。

Objective-C的

- (NSURLResponse *)POST:(NSString *)parameters { 
    NSString *baseURL = @"https://script.google.com/macros/s/your_ID/dev?post=true"; 
    NSURL *url = [NSURL URLWithString: [baseURL stringByAppendingString:parameters]]; // Set your URL here - don't forget to escape quotation marks when you pass the request parameters to the method 

    NSURLSession *session = [NSURLSession sharedSession]; 

    __block NSURLResponse *postResponse; 
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
     // What to do after the POST has completed (if the POST returns any data, it'll be stored in the response parameter specified above); use dispatch_async to wait until the request has completed 
     dispatch_async(dispatch_get_main_queue(), ^(void) { 
      postResponse = response; 
     }); 
    }]; 

    [dataTask resume]; 
    return postResponse; 
} 

斯威夫特

func POST(parameters: String) -> NSURLResponse? { 
    let url: NSURL = NSURL(string:"https://script.google.com/macros/s/your_ID/dev?post=true\(parameters)")! // Set your URL here - don't forget to escape quotation marks when you pass the request parameters to the function 

    let session: NSURLSession = NSURLSession.sharedSession()  

    var postResponse: NSURLResponse? 
    let dataTask: NSURLSessionDataTask = session.dataTaskWithURL(url, completionHandler: {(data: NSData!, response: NSURLResponse!, error: NSError!) -> Void in // Closure 
    // What to do after the POST has completed (if the POST returns any data, it'll be stored in the response parameter specified above); use dispatch_async to wait until the request has completed 
     dispatch_async(dispatch_get_main_queue(), { 
      postResponse = response 
     }) 

    }) 

    dataTask.resume() 
    return postResponse 
} 
+1

是的,任何困惑的讀者,這是一個聯合的努力。 AstroCB和我在一個非常類似的項目上合作,在那裏我做了後端,他做了應用前端。我們認爲我們每個人都可以幫助 – JZL003

+0

哇感謝您的幫助!我不確定這是什麼意思,但是當我嘗試運行doGet時,我得到這個錯誤「TypeError:Can not read property」from undefined。(第2行,文件「Code」)「 – Jonathon

+0

也只是爲了澄清,on在工作表中,我想要在每個人姓名旁邊的單元格中包含一個數字的人員名稱列。我希望應用每次在應用中發生某個事件時都能夠將1添加到某個人的號碼。我不確定何時能夠測試應用程序,因爲我還沒有註冊到蘋果開發人員計劃中,並且該應用程序依賴於使用無法模擬的設備相機。無論如何,再次感謝! – Jonathon

相關問題