iOS将Sendy API集成到应用程序中

时间:2013-04-04 20:50:42

标签: ios api networking

我正在制作一款需要与Sendy API通信的iPhone应用。我相信它使用某种JSON,但我不确定,也不知道从哪里开始。我对API的subscribe部分特别感兴趣。 基本上,我需要知道如何通过我的应用与Sendy API进行对话。

感谢任何帮助。

我的代码:

- (IBAction)submitButtonPressed:(id)sender
{
    self.data = [[NSMutableData alloc] initWithLength:0];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.erwaysoftware.com/sendy/subscribe"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"john@test.com" forHTTPHeaderField:@"email"];
    [request setValue:@"john" forHTTPHeaderField:@"name"];
    [request setValue:@"LxxxxxxxxxxxxxxxxxxxxQ" forHTTPHeaderField:@"list"];
    [request setValue:@"true" forHTTPHeaderField:@"boolean"];

    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
    [conn start];

}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.data setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)d {
    [self.data appendData:d];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"")
                                 message:[error localizedDescription]
                                delegate:nil
                       cancelButtonTitle:NSLocalizedString(@"OK", @"")
                       otherButtonTitles:nil] show];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *responseText = [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding];

    // Do anything you want with it

    NSLog(@"%@", responseText);
}

当日志发生时,字符串为空。我通过断点知道调用了最后一个方法。

2 个答案:

答案 0 :(得分:2)

查看API,它只是纯文本响应。

由于它是POST,您可以使用NSURLConnection撰写请求。有关格式化响应的信息,请参阅this question

另一种方法是使用类似AFNetworkingRestKit的内容,如果您正在使用API​​进行更多工作,那可能会更友好一些。

答案 1 :(得分:1)

我猜你已经解决了这个问题,但万一其他人被困在这里(正如我所做的那样)我以为我会发布我所做的事情以使其发挥作用。

您需要做的第一件事是创建一个名为NSString + URLEncoding(或其他)的类别,它将从blah@blah.com获取您的电子邮件和名称字段,并将其转换为blah%40blah.com。我从发现here

的方便博客文章中对此进行了修改
@interface NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;
@end

#import "NSString+URLEncoding.h"

@implementation NSString (URLEncoding)
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding {
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
                                                           (CFStringRef)self,
                                                           NULL,
                                                           (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                                             CFStringConvertNSStringEncodingToEncoding(encoding)));}
@end

好的,现在只需导入NSString + URLEncoding.h并添加以下代码即可开展业务。这个post帮助了我这个部分

- (IBAction)submitButtonPressed:(id)sender
{
NSMutableURLRequest *newRequest = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:@"http://stashdapp.com/sendy/subscribe"]];
[newRequest setHTTPMethod:@"POST"];
[newRequest setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

NSString *email = @"name@domain.com";
NSString *name = @"First Lastname";
NSString *list = @"XXXXXXXXXXXXXXXXXX";

NSString *postData = [NSString stringWithFormat:@"email=%@&boolean=true&name=%@&list=%@", [email urlEncodeUsingEncoding:NSUTF8StringEncoding],[name urlEncodeUsingEncoding:NSUTF8StringEncoding],list];

[newRequest setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *conn = [NSURLConnection connectionWithRequest:newRequest delegate:self];
[conn start];
}

您仍然包含您在问题中引用的委托方法。

希望它有所帮助!