0

I am new in iOS and I am facing problem to send image to web service. My code is like this

UIGraphicsBeginImageContext(self.drawSignView.bounds.size);
    [[self.drawSignView.layer presentationLayer] renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // NSData *postData = UIImageJPEGRepresentation(viewImage, 1.0);

    NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((viewImage), 0.5)];
    SignString = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    int imageSize   = imgData.length;
    NSLog(@"size of image in KB: %d ", imageSize/1024);

I am using signature view and I convert it into image and then convert it into string and then send it to web service.

My web service is like this

POST /webservice.asmx HTTP/1.1
Host: url
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Method"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Method xmlns="http://tempuri.org/">
      <ID>string</ID>
      <NameId>long</NameId>
      <Sign1>string</Sign1>
      <Sign2>string</Sign2>
      <Email>string</Email>
      <ClientName>string</ClientName>
      <UserId>int</UserId>
      <CreatedDate>string</CreatedDate>
    </Method>
  </soap:Body>
</soap:Envelope>

I am sending it to webservice like this

    -(void)serverconnectionPost{


        NSString *MethodString =@"?op=Method";
        NSString *ServerfullString =[NSString stringWithFormat:@"%@%@",ServerString,MethodString];

        NSString *id=@"0";
        NSString *EmailString=emailtxt.text;
        NSString *NameString=clientnametxt.text;
        long nameid=[AuditNameId longLongValue];
        int UserId=[userid intValue];


        NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                                 "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                                 "<soap:Body>"
                                 "<Method xmlns=\"http://tempuri.org/\">"
                                 "<Id>%@</Id>"
                                 "<NameId>%ld</NameId>"
                                 "<Sign1>%@</Sign1>"
                                 "<Sign2>%@</Sign2>"
                                 "<Email>%@</Email>"
                                 "<ClientName>%@</ClientName>"
                                 "<UserId>%d</UserId>"
                                 "<CreatedDate>%@</CreatedDate>"
                                 "</Insert_Audit_Signs>"
                                 "</soap:Body>"
                                 "</soap:Envelope>",id,nameid,Sign1,Sign2,EmailString,ClientNameString,UserId,AuditStartDate];

        NSData *postData = [soapMessage dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

        NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[soapMessage length]];
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:[NSURL URLWithString:ServerfullString]];
        [request setHTTPMethod:@"POST"];
        [request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        NSLog(@"URL = %@",request);
        [request setHTTPBody:postData];
        myNSUConnectionPOSTObj = [[NSURLConnection alloc] initWithRequest:request delegate:self];
        NSLog(@"Connection link =%@",myNSUConnectionPOSTObj);
        if(myNSUConnectionPOSTObj) {
            myNSMDataFromPOSTServer =[[NSMutableData alloc] init];
            NSLog(@"Connection Successful");

        } else {
            NSLog(@"Connection could not be made");
        }
    }

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
 [myNSMDataFromPOSTServer setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[myNSMDataFromPOSTServer appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{

NSXMLParser *myNSXMLParserPostObj=[[NSXMLParser alloc]initWithData:myNSMDataFromPOSTServer];
        myNSXMLParserPostObj.delegate=self;
        [myNSXMLParserPostObj parse];
        NSLog(@"%@",myNSXMLParserPostObj.parserError);
        NSString *responseStringWithEncoded = [[NSString alloc] initWithData: myNSMDataFromPOSTServer encoding:NSUTF8StringEncoding];
        //NSLog(@"Response from Server : %@", responseStringWithEncoded);
        NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
        serverResponse.attributedText = attrStr;
        NSString *Str =serverResponse.text;
        NSLog(@"Server Response =%@",Str);

        alert3 = [[UIAlertView alloc] initWithTitle:@"Insert Data"
                                            message:@"Check List Save Successfully"
                                           delegate:self
                                  cancelButtonTitle:nil
                                  otherButtonTitles:@"OK",nil];
        //[alert3 setTag:1];

        [alert3 show];
}

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    myMutableStringPOSTObj=[[NSMutableString alloc]initWithString:string];
    NSLog(@"Array String: %@",myMutableStringPOSTObj);
    NSData *dataPost = [myMutableStringPOSTObj dataUsingEncoding:NSUTF8StringEncoding];
    responsePOSTdict = [NSJSONSerialization JSONObjectWithData:dataPost options:0 error:nil];
    NSLog(@"JSON DATA = %@",responsePOSTdict);
}

I have added so much code because I am not getting where I and doing wrong.Other web service work fine. But in this web service I am getting response as 0. Thanks in Advance!

Muju
  • 884
  • 20
  • 54

0 Answers0