LoginSignup
49
49

More than 5 years have passed since last update.

Parseにサーバーサイドのコードを書いてiOSアプリから呼ぶ

Last updated at Posted at 2013-10-05

Parse の Cloud Code をはじめて触ってみた際に書いたメモです。 ローカルで生成した JavaScript のコードをデプロイし、iOSアプリから呼んでみる までの手順。

1. 準備

  • Parseダッシュボードからアプリをつくっておく
  • コマンドラインツールをインストールする
$ curl -s https://www.parse.com/downloads/cloud_code/installer.sh | sudo /bin/bash

2. Parse newコマンドを実行する

$ parse new MyCloudCode

EmailとPasswordを求められるので、入力する。

GitHubとかでサインアップした場合、Passwordがないので、ParseのダッシュボードのAccount->Editで設定しておく。

認証に成功すると、

1:MyAppA
2:MyAppB
Select an App: 

みたいな感じでどのアプリにするか聞かれるので、番号を入力する。

成功すると、下記フォルダ/ファイルが自動生成される。

-config/
  global.json
-cloud/
  main.js
-public/
  index.html

3. デプロイする

cloud/main.js にはHello world的なコードが入ってるので、すぐにデプロイして試せる。

$ cd MyCloudCode
$ parse deploy

成功すると、ダッシュボードの Cloud Code タブで main.js の内容を確認できる。

ちなみにこんなコード。

Parse.Cloud.define("hello", function(request, response) {
  response.success("Hello world!");
});

4. コマンドラインからたたいてみる

curlコマンドからmain.jsに実装されているhello関数を呼ぶ。

curl -X POST \
  -H "X-Parse-Application-Id: {Application ID}" \
  -H "X-Parse-REST-API-Key: {REST API Key}" \
  -H "Content-Type: application/json" \
  -d '{}' \
  https://api.parse.com/1/functions/hello

こういうレスポンスが返ってくる。

{
  "result": "Hello world!"
}

5. iOSアプリからたたいてみる

iOSアプリへParse SDKを導入する

この詳しい手順は省略します。

クイックスタートガイド

Parse.frameworkを追加して、依存フレームワークを追加して、APIキーとかをセットするメソッドを呼ぶ、それだけです。

callFunctionInBackground:withParameters: メソッドで実装した関数を呼ぶ

[PFCloud callFunctionInBackground:@"hello"
                   withParameters:@{}
                            block:^(NSString *result, NSError *error) {

                                if (error) {

                                    NSLog(@"error:%@", error);
                                }
                                else {

                                    NSLog(@"result:%@", result);
                                }
                            }];

参考資料

49
49
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
49
49