2013-03-11 91 views
0

我正在嘗試將解析API集成到我爲學校所做的OS X應用程序中。該應用程序應發送一個推送通知,所以我想我會使用Parse的REST API來完成此操作。使用Objective-C爲OS X應用程序發送CURL命令

下面的代碼是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等價物?或者你有什麼建議?

在此先感謝。

回答

2

您可以在Foundation框架中使用URL-handling APIs。下面是一個例子:

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協議所要求的方法。

相關問題