2

私は現在、ユーザーが分類されたリストを投稿して他のユーザーが見られるようにする iOS アプリを開発しています。SVProgressHUDすべてのデバイスで、そのアプリは「サーバーが一時的に利用できません。後でもう一度やり直してください」と表示される前に、しばらくの間「O% アップロード済み」でハングします。サーバ側。ただし、iOS シミュレーターでは、すべてがスムーズに動作します。

ネットワーク リクエストに関連する他のすべての機能は、これを除いて完全に機能しているため、実際のアップロード プロセスと何らかの関係があるはずです。以下に対応するコードを投稿しました。これを理解するためにさらに何か必要な場合は、お知らせください。

ネットワーク コード

- (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