0

我正在尝试将 Parse API 集成到我为学校做的 OS X 应用程序中。应用程序应该发送一个推送通知,所以我想我会使用 Parse 的 REST API 来完成这个。

下面的代码是 Parse 提供的用于发送通知的示例 CURL 代码。

curl -X POST \
  -H "X-Parse-Application-Id: XXXXXXXXXXXXXXXXXXXXXX" \
  -H "X-Parse-REST-API-Key: XXXXXXXXXXXXXXXXXXXXXX" \
  -H "Content-Type: application/json" \
  -d '{
        "channels": [
          "Giants",
          "Mets"
        ],
        "data": {
          "alert": "The Giants won against the Mets 2-3."
        }
      }' \
  https://api.parse.com/1/push

完成过去 CURL 代码的 Objective-C 等效项是什么?或者你有什么建议?

提前致谢。

4

1 回答 1

2

您可以使用 Foundation 框架中的URL 处理 API。这是一个例子:

NSURL *url = [NSURL URLWithString:@"https://api.parse.com/1/push"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request addValue:@"XXXXXXXXXXXXXXXXXXXXXX" forHTTPHeaderField:@"X-Parse-Application-Id"];
[request addValue:@"XXXXXXXXXXXXXXXXXXXXXX" forHTTPHeaderField:@"X-Parse-REST-API-Key"];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:@"{\"channels\": [\"Giants\",\"Mets\"], \"data\": {\"alert\": \"The Giants won against the Mets 2-3.\"}}"];
NSURLConnection *connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

然后实现协议要求的方法NSURLConnectionDelegate

于 2013-03-11T03:46:09.887 回答