App is not updating database when string contains spaces - mysql

I ran into a weird problem. I built a mysql database where the user can store his contacts through an app. He just adds his contact information by clicking on a button. Then, the data is added to the database.
However, when the contact's information has spaces between his first name, last name or number the app refuses to accept the data. I dont get an error message or anything like that, but I also dont get any confirmation. The record does not exist in the database.
There is no problem with my php file (The file is definitely correct, I got it checked here, twice).
Here is my code:
NSString *strURL = [NSString stringWithFormat:#"http://www.abc.com/phpFile.php?number=%#&name=%#&lastname=%#", number, firstName, lastName];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(#"%#", strResult);

The space character is not allowed in a URI. Use %20 to encode the space. There are other characters that are also not allowed as part of the URI. Look up URI encoding.

NSString *strURL = [NSString stringWithFormat:#"http://www.abc.com/phpFile.php?number=%#&name=%#&lastname=%#", number, firstName, lastName];
strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(#"%#", strResult);

Related

How to pass an HTML string in a Query String in IOS

I have a simple IOS application that I created for consultants where I work. It has a custom keyboard with notation that they use that includes superscript and subscript characters. The easiest way that I found to handle those characters was using HTML and a WebView, which works just fine.
I am now attempting to send this information to a web application that will insert it into a SQL database. Sending all of the other information works just fine, until I get to the transcript with HTML in it. I am encoding each of the values that I send through the query string with stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding but the query string fails when HTML elements are included. I believe this is due to the / that is not removed during the escape encoding. For example the HTML break tag becomes %3Cbr%20/%3E. So the < > and space are replaced, but not the /.
Should I be using a different method? Is there an easy way to send HTML over Query String? Am I correct that the / character is what is breaking my query string?
Here is the code I have now:
NSString *datePE =[dateString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *studentPE =[student stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *teacherPE =[teacher stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *transcriberPE =[transcriber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *vrPE =[vrTextField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *totalTimePE =[totalTimeString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *transcriptPE =[transcriptString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
parameters = [NSString stringWithFormat:#"dateQS=%#&studentQS=%#&teacherQS=%#&transcriberQS=%#&vrQS=%#&totalTimeQS=%#&transcriptQS=%#",datePE ,studentPE,teacherPE,transcriberPE,vrPE,totalTimePE, transcriptPE];
request = [NSMutableURLRequest
requestWithURL:[NSURL URLWithString:#"http://server.domain.net/folder/subfolder/Default.aspx?"]];
[request setHTTPMethod:#"POST"];
[request setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];
connection = [[NSURLConnection alloc] initWithRequest:request
delegate:self
startImmediately:YES];

XCode GET POST WebServer

I'm beginner from Xcode. I have to do an application that get and send data from embedded webserver that I designed in a microcontroller MICROCHIP.
Now I'm able to get information from webserver ( In my webserver I have a /status.xml file where I have all the dynamic variables)
Now I am not able to click a button. The HTML code to click a button is
onmousedown="newAJAXCommand('buttons.cgi?btn=1')"
In my webserver I have a file buttons.cgi.
My target is designed a button in Xcode that he does this action
I tried to use NSMutableURLRequest class and SetHTTPMethod:#"GET" or #"POST" but this code doesn't work
NSString *post = #"btn=1";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES ];
NSString *Get_lenght = [NSString stringWithFormat:#"%d",[postData length]];
NSURL *serviceURL = [NSURL URLWithString:#"http://mywebserver.com/buttons.cgi?"];
NSMutableURLRequest *serviceRequest = [NSMutableURLRequest requestWithURL:serviceURL];
[serviceRequest setValue:Get_lenght forHTTPHeaderField:#"Content-type"];
[serviceRequest setHTTPMethod:#"POST"];
[serviceRequest setHTTPBody:postData];
There are multiple errors in the codes:
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES ];
Why don't you use UTF-8 ?
NSString *Get_lenght = [NSString stringWithFormat:#"%d",[postData length]];
[serviceRequest setValue:Get_lenght forHTTPHeaderField:#"Content-type"];
You can't set Content-Type as an integer.
Last, you didn't send out the NSMutableURLRequest. To send out the request, use :
NSString *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:serviceRequest returningResponse:&response error:&error];
where response will contain the response data, if you need it; and the error will contain error during request & response, if there is any error.

Encoded NSData for a url not saving to the documents directory

I'm trying to save a html page locally on the iPhone's documents directory so I can load the page if the phone is in an offline state
My issue is when I call my NSData object directly to be written to file all the raw data is saved in a file in the documents directory.
But as soon as I cast my NSData to a NSString using encoding it stops writing the file in the documents directory. But when I print my content object it logs out the correct data. I just want that to be saved to a file.
NSURL *url = [NSURL URLWithString:#"http://anyurl"];
NSData *urlHtmlData = [NSData dataWithContentsOfURL:url];
NSString *myString = [[NSString alloc]initWithData:urlHtmlData encoding:NSUTF8StringEncoding];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:#"index.html"];
[myString writeToFile:fileName atomically:NO encoding:NSStringEncodingConversionAllowLossy error:nil];
What could be causing the content to stop writing to my file?
Or if anyones got a better solution to view offline webpages rather than saving it to the documents directory?
Thanks
There could be a few things wrong with this. One: You don't use stringWithFormat: to append a path component. NSString has a method for that:
NSString *fileName = [documentsDirectory stringByAppendingPathComponent:#"index.html"];
Two: NSStringEncodingConversionAllowLossy is part of an enum that is not defined for writeToFile:, so by using it, you're actually specifying NSASCIIStringEncoding without realizing it. In my opinion, just use NSUTF8StringEncoding instead.
Also, NSString *content = [[NSString alloc]initWithFormat:#"%#", myString]; does absolutely nothing except duplicate myString. Erase that line and just use [myString writeToFile:....

If Statement Always Going to Else Method?

The following code works almost perfectly. I am connecting to a mysql server on my localhost from Xcode using php in the middle. This in the end will be a login system:
NSString *strURL = [NSString stringWithFormat:#"http://localhost:8888/Check.php?user=%#&pass=%#",txtName.text,passName.text];
// to execute php code
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:strURL]];
NSError *e;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&e];
// to receive the returned value
NSString *strResult = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]autorelease];
NSLog(#"%#",strResult);
NSString *success = #"Success";
if ([strResult isEqualToString:success]) {
NSLog(#"I realize that the two strings are the same");
}
else {
NSLog(#"The two strings are not the same");
}
The strResult prints out in the debugger the following items that I am telling the php file to echo back to me for the different conditions, (if the username and password is right or wrong)
However, for some reason the if statement part of the code is always going to the else method even though in the output it specifically says that the string, strResult, is containing the word "Success".
This is so irritating because I can see that both strings, (strResult and success), are equal to each other but for some reason Xcode cannot.
Your strResult might contain whitespace at the end. Try logging like this to get a hex dump of the characters in the string:
NSLog(#"strResult = %#", [strResult dataUsingEncoding:NSUTF8StringEncoding]);
NSLog(#"success = %#", [success dataUsingEncoding:NSUTF8StringEncoding]);
OR
NSLog(#"%u",strResult.length);
If it is a whitespace problem, you can trim it using the answer here: What's the best way to trim whitespace from a string in Cocoa Touch?

App is STILL not updating database when string contains spaces

Slowly but surely I think I'm going mad. After getting the solution to my problem and the app now also accepting spaces, it still won't add the data that contains spaces to my mysql database. Here is the code:
NSString *strURL = [NSString stringWithFormat:#"http://www.bbc.com/phpFile.php?number=%#&name=%#&lastname=%#", number, firstName, lastName];
NSString *webStringURl = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(#"%#", strResult);
NSLog(#"%#", webStringURl);
NSLog even tells me that it does exactly what I tell him to:
ProjectX[4003:15203] http://www.bbc.com/phpFile.php?number=11166&name=Yong%20Wang%20Ding&lastname=Blamblam
It works everytime when the variables do not contain spaces. When I open the php file in my browser and enter the variables by hand (with spaces) it adds them to my database. I have no clue why it refuses to work when doing exactly(?) the same thing with my app.
It doesn't look like you're using webStringURl (sic) anywhere other than in the final NSLog statement. Are you sure you didn't mean to write:
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:webStringURl];
...for the third line?
At the moment, you're calling dataWithContentsOfURL on your unescaped version of the URL (which may still contain spaces).