Lloyd18
Lloyd18

Reputation: 1682

How to upload several images via HTTP request in iOS?

I'm developing client-server app. I found few tutorials how to upload one image. But what the best way to upload several images using single request? I'm new in web programming so I don't no the common way.

Any help appreciated.

Upvotes: 0

Views: 192

Answers (2)

demon9733
demon9733

Reputation: 854

You should to use ASIHTTPRequest library.

Then create your own class to manage all request you want. This is mine:

#import "ASIFormDataRequest.h"

@interface PostRequest : NSObject {
    id localCopy; // to avoid exec_bad_access with arc
    ASIHTTPRequest *getRequest;
    ASIFormDataRequest *postRequest;
}

@property (nonatomic, retain) id delegate;
@property (nonatomic, readwrite) SEL callback;
@property (nonatomic, readwrite) SEL errorCallback;

- (void)performGetRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector;
- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector;

@end

//

#import "PostRequest.h"

@implementation PostRequest

@synthesize delegate;
@synthesize callback, errorCallback;

- (void)performGetRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector {

    localCopy = self;

    self.delegate = requestDelegate;
    self.callback = requestSelector;
    self.errorCallback = errorSelector;

    NSMutableString *requestStringData = [[NSMutableString alloc] init];
    if (stringDictionary)
        for (NSString *key in [stringDictionary allKeys])
            [requestStringData appendFormat:@"%@=%@&", key, [stringDictionary objectForKey:key]];
    NSString *resultString = [requestStringData substringToIndex:[requestStringData length]-1];

    getRequest = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", string, [resultString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
    [getRequest setDelegate:self];
    [getRequest setRequestMethod:@"GET"];

    //NSLog(@"request url = %@", [getRequest.url absoluteString]);
    [getRequest startAsynchronous];
}

- (void)performPostRequestWithString:(NSString *)string stringDictionary:(NSDictionary *)stringDictionary dataDictionary:(NSDictionary *)dataDictionary delegate:(id)requestDelegate requestSelector:(SEL)requestSelector errorSelector:(SEL)errorSelector {

    localCopy = self;

    self.delegate = requestDelegate;
    self.callback = requestSelector;
    self.errorCallback = errorSelector;

    NSURL *url = [NSURL URLWithString:string];

    postRequest = [[ASIFormDataRequest alloc] initWithURL:url];
    [postRequest setDelegate:self];
    [postRequest setRequestMethod:@"POST"];

    if (stringDictionary)
        for (NSString *key in [stringDictionary allKeys])
            [postRequest setPostValue:[stringDictionary objectForKey:key] forKey:key];

    if (dataDictionary)
        for (NSString *key in [dataDictionary allKeys])
            [postRequest setData:[dataDictionary objectForKey:key] forKey:key];

    //NSLog(@"request url = %@", [postRequest.url absoluteString]);
    [postRequest startAsynchronous];
}

#pragma mark - ASIHTTPRequest Delegate Implementation

- (void)requestFinished:(ASIHTTPRequest *)crequest {
    NSString *status = [crequest responseString];

    if (self.delegate && self.callback) {
        if([self.delegate respondsToSelector:self.callback])
            [self.delegate performSelectorOnMainThread:self.callback withObject:status waitUntilDone:YES];
        else
            NSLog(@"No response from delegate");
    }
    localCopy = nil;
}
- (void)requestFailed:(ASIHTTPRequest *)crequest {
    if (self.delegate && self.errorCallback) {
        if([self.delegate respondsToSelector:self.errorCallback])
            [self.delegate performSelectorOnMainThread:self.errorCallback withObject:crequest.error waitUntilDone:YES];
        else
            NSLog(@"No response from delegate");
    }
    localCopy = nil;
}

@end

To use it, just import PostRequest.h in your UIViewController and do smth like:

[requestManager performGetRequestWithString:tempString stringDictionary:stringDictionary dataDictionary:dataDictionary delegate:self requestSelector:@selector(requestSucceeded:) errorSelector:@selector(requestFailed:)];

Parameters:

  • (NSString *)string - url string, where to post your request;
  • (NSDictionary *)stringDictionary - dictionary, which contains all the text information (such as name, id etc.);
  • (NSDictionary *)dataDictionary - dictionary, which contains all data information (such as photos, files, etc.);
  • (id)requestDelegate - delegate to perform selectors below;
  • (SEL)requestSelector - selector, which will be executed while successfully request;
  • (SEL)errorSelector - selector, which will be executed, while error occurred.

Upvotes: 1

Lefteris
Lefteris

Reputation: 14667

I would suggest using either ASIHTTPRequest or AFNetworking

Both have a network queue which you can use. ASIHTTP is no longer being developed, so you might look into AFNetworking if you would like to use a library that is being developed...

Upvotes: 1

Related Questions