我已经在我的应用程序中使用 Urban Airship 成功实现了推送通知,但我想在应用程序中创建一个受管理密码保护的部分,使人们能够从任何安装了该应用程序的授权 iPhone 发送推送通知。我需要一种方法,以便我在文本框中输入的任何内容都通过 https POST 发送到https://go.urbanairship.com/api/push/broadcast。任何人都可以推荐或告诉我这样做的方法吗?
问问题
211 次
1 回答
0
您可以使用它NSURLConnection
来执行此操作。使用发布数据和其他信息设置一个NSMutableURLRequest
,然后使用NSURLConnection
发布它并获得响应:
NSString * postString = [NSString stringWithFormat:@"field1=%@", myItem];
NSData * postData = [postString dataUsingEncoding:NSUTF8StringEncoding];
NSURL * myURL = [NSURL URLWithString:@"https://go.urbanairship.com/api/push/broadcast"];
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
NSData * returnedData = [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
您还可以将 NSURLConnection 用于异步请求。请参阅NSURLConnection 类参考。
于 2011-09-12T00:24:17.007 回答