iphone - 如何在 ios 中将 sendSynchronousRequest 更改为 sendASynchronousRequest?

标签 iphone objective-c ios web-services ios5

我正在服务器上上传图片。 如何将同步请求更改为异步请求。因为我想在上传图片时使用进度条..

我的代码是。

+(NSDictionary *)UploadPhoto :(UIImage *)image{

    NSString *DeviceId;
    NSString *TokenValue=tokenTemp;

    DeviceId = DevID;

    NSString *Nonce=[self generateRandomString];
    NSString *currentTimeStamp = [self GetTimeStamp];


    NSString *Cipher=[NSString stringWithFormat:@"%@%@%@%@%@",mySecret,DeviceId,Nonce,currentTimeStamp,TokenValue];
    Cipher=[Cipher URLDecodedString];
    Cipher=[self GetCipher:Cipher];
    UIImage *TempPicture=image;//[UIImage imageNamed:imageName];

    NSData *PictureData=UIImageJPEGRepresentation(TempPicture, 1.0);
    NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"%@Messages.php",WebServiceUrl]];
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setTimeoutInterval:20.0f];
    [request setURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"multipart/form-data; boundary=*****" forHTTPHeaderField:@"Content-Type"];

    NSMutableData *postBody = [NSMutableData data];
    NSString *stringBoundary = [NSString stringWithString:@"*****"];

    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"%@",@"Upload"] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"Type\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"%@",@"0"] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"Name\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"%@",@"XYZ ****************"] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"MessageImage\"; filename=\"c.png\"\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    [postBody appendData:[NSData dataWithData:PictureData]];
    [postBody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",stringBoundary] dataUsingEncoding:NSASCIIStringEncoding]];
    [request setHTTPBody: postBody];

    [request setValue:Cipher forHTTPHeaderField:@"Cipher"];
    [request setValue:DeviceId forHTTPHeaderField:@"Deviceid"];
    [request setValue:Nonce forHTTPHeaderField:@"Nonce"];
    [request setValue:currentTimeStamp forHTTPHeaderField:@"Timestamp"];
    [request setValue:TokenValue forHTTPHeaderField:@"Token"];

    [request setHTTPBody:postBody];
    NSError *error;
    NSHTTPURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

    if (!data) {
          NSLog(@"nil");
    }
    NSDictionary *temp= [response allHeaderFields];

    NSString *temp2= [temp objectForKey:@"Responsecode"];
    if(temp2!=nil && [temp2 length]>0)
    {

    }
    else
    {

    }
    return temp;
}

谢谢.. 请帮助..

如何调用 - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite 在此代码中..

最佳答案

如果你想跟踪上传进度,你必须使用

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self]

并实现所有的 NSURLConnectionDelegate 方法。这是描述in this Apple documentation .

特别是 connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite: 委托(delegate)方法会通知您上传进度。

此方法的另一个优点是您可以取消一个请求,这是sendSynchronousRequestsendASynchronousRequest 做不到的。

关于iphone - 如何在 ios 中将 sendSynchronousRequest 更改为 sendASynchronousRequest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11912860/

相关文章:

ios - 根据 iPhone 屏幕尺寸增加字体大小

iphone - 如何通过在 iOS 中显示正数的 + 号将 int 格式化为 NSString

iphone - 以编程方式使用要设置动画的图像填充数组

ios - 固定标签在该位置且宽度相等

ios - 将 1347616929 转换成 dd/MM/yyyy hh :mm:ss format

ios - 调整 UILabel 的大小以容纳插图

iphone - 在 UIimageview 层的边框和内容之间添加空间

ios - ios 7 中的状态栏问题

iphone - NSMutableArray removeObjectAtIndex : throws invalid argument exception

ios - 如何使用 instagram 登录我的 iPhone 应用程序?