【问题标题】:HTTP Post help - iOS appHTTP Post 帮助 - iOS 应用
【发布时间】:2011-11-14 23:24:22
【问题描述】:

我已经在我的应用程序中成功地使用 Urban Airship 实现了推送通知,但我想在应用程序中创建一个受管理密码保护的部分,使人们能够从任何安装了该应用程序的授权 iPhone 发送推送通知。 我需要一种方法,以便我在文本框中输入的任何内容都通过 https POST 发送到https://go.urbanairship.com/api/push/broadcast。 任何人都可以推荐或告诉我这样做的方法吗?

【问题讨论】:

    标签: ios post https urbanairship.com


    【解决方案1】:

    您可以使用NSURLConnection 来执行此操作。设置一个NSMutableURLRequest 和post 数据和其他信息,然后使用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 Class Reference

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-23
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      相关资源
      最近更新 更多