0

I am making an iPhone app that will need to communicate with the Sendy API. I believe that it uses some kind of JSON, but I'm not really sure, nor do I know where to start. I'm particularly interested in the subscribe portion of the API. Basically, I need to know how to talk to the Sendy API from my app.

Any help is appreciated.

My code:

- (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);
}

When the log happens, the string is empty. I know through breakpoints that the last method is called.

Undo
  • 25,519
  • 37
  • 106
  • 129

2 Answers2

2

Looking at the API it's all just plain text response.

Since it's a POST you can use an NSURLConnection to compose the request. See this question for information on formatting the response.

An alternative is to use something like AFNetworking or RestKit that might be a little more friendly if you're doing more work with APIs.

Community
  • 1
  • 1
Richard Brown
  • 11,346
  • 4
  • 32
  • 43
1

I'm guessing you've already resolved this but in case anyone else gets stuck here (as I did) I thought I'd post what I did to get it to work.

The first thing you need to do is create a category called NSString+URLEncoding (or whatever) which is going to take your email and name fields from blah@blah.com and turn it into blah%40blah.com. I modified this from the handy blog post found 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

Ok so now just import NSString+URLEncoding.h and add the following code and you'll be in business. This post helped me with this part

- (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];
}

You still include the delegate methods which you quoted in your question.

Hope it helps someone!

peten
  • 113
  • 8