2

I am currently developing an iOS app that allows a user to post a classified listing for other users to see. On all devices, that app hangs in a "O% Uploaded" SVProgressHUD for a while before displaying "Our server is temporarily unavailable. Please try again later.", which is what I have coded in the case of a Status Code 500 error from the server side. However, on the iOS simulator, everything works smoothly.

All other functionality involving network requests is working perfectly except for this, so it must have something to do with the actual upload process. I've posted the corresponding code below; if you require anything additional to help figure this out, please let me know.

Networking Code

- (IBAction)publishPressed:(UIBarButtonItem *)sender {
    [SVProgressHUD showWithStatus:@"Loading..." maskType:SVProgressHUDMaskTypeBlack];

    // Set the params for the API call in an NSMutableDictionary called mutableParams.

    // Create the form request with the post parameters and image data to pass to the API.
    NSMutableURLRequest *request = [[UAPIClient sharedClient] multipartFormRequestWithMethod:@"POST" 
                                                              path:@"mobPost.php" 
                                                              parameters:mutableParams 
                                                              constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (self.imageHasBeenSet) {
            [self.listingImageView.image fixOrientation];
            NSData *imageData = UIImagePNGRepresentation(self.listingImageView.image);
            [formData appendPartWithFileData:imageData name:@"userfile" 
                      fileName:@"postImage.png" mimeType:@"image/png"]; 
        }
    }];

    // Create the `AFJSONRequestOperation` from the form request, with appropriate success and failure blocks.
    AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseJSON) {
        if ([[responseJSON objectForKey:@"status"] intValue] == 1) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showSuccessWithStatus:@"Success!"];
            });
            // Pass a message back to the delegate so that the modal view controller
            // can be dismissed successfully.
            [self.delegate uCreateListingTableViewController:self didCreateListing:YES];
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD showErrorWithStatus:@"Post failed. Please try again."];
            });
            NSLog(@"Status %@", [responseJSON objectForKey:@"status"]);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showErrorWithStatus:@"Our server is temporarily unavailable. Please try again later."];
        });
    }];

    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD showSuccessWithStatus:[[NSString stringWithFormat:@"%lli", (totalBytesWritten / totalBytesExpectedToWrite)] stringByAppendingString:@"% Uploaded"]];
        });
    }];

    [[UAPIClient sharedClient] enqueueHTTPRequestOperation:operation];
}

UAPIClient.m

#import "UAPIClient.h"
#import "AFJSONRequestOperation.h"

static NSString * const kUAPIBaseURLString = @"hiding string for privacy";

@implementation UAPIClient

+ (UAPIClient *)sharedClient {
    static UAPIClient *_sharedClient;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedClient = [[UAPIClient alloc] initWithBaseURL:[NSURL URLWithString:kUAPIBaseURLString]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (self) {
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];

        // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
        [self setDefaultHeader:@"Accept" value:@"application/json"];
    }

    return self;
}

@end
4

0 回答 0