I have a very strange phenomenon going on with my script. When they are manually made in the script, everything works. When I upload it by iOS, however, it appears that a few of my strings prevent it from sending (when I remove those variables, it sends and executes the insert commands fine again with only the values sent). What I mean by prevent is it fails to send the request at all and I get no insert (the script is designed to insert whatever it gets). Even crazier, is that I nsslog is showing the proper values in those positions just before executing the nsurl. Therefore, I was curious if it is possible for nsurl to be blocked by some of its own values? The values that block it are two of my six text fields and one of two nsstrings passed from another view controller. I have given an example of my format here.
NSString *urlString = [NSString stringWithFormat:#"http://(mywebsite)?string=%#&text=%#", string, textfield.text];
If you're text contains unescaped characters like #, ?, ' or " for example, then yes, the content of your url will cause issues. For example, if i enter "'abcd'&x=50" into your textfield, then your web string will suddenly have an x parameter. Or, you can add in stuff like colons, backslashes etc.
If you correctly escape your string, you should be able to build URL's that way.
Related
I would like to validate whether an NSString can be converted to a valid NSURL. I know that using URLWithString will make a URL, but it is not always valid. Additionally, I don't want to make a web call every time my user enters a string to verify the URL as that is not battery/data efficient, and it relies on having an active web connection which is not always the case. I came across this site various URLs, and I am now attempting to copy the regex they used and convert it to NSRegular Expression. I've been using this helpful cheatsheet to try and convert it, but to no avail. I stored the regex as a const like so:
static NSString * const urlPattern = #"_^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:/[^\s]*)?$_iuS";
I have been attempting to convert this by adding escape characters in front of all special characters listed in the cheatsheet, but it doesn't seem to be working properly. Has anyone here has any luck getting #diegoperini's URL regex to work in Objective-C or Swift using NSRegularExpressions?
I am currently working on an app that connects to custom server using NSStream. Once connected the app allows for user input via a textfield. In order for the server to recognize an incoming command a \t needs to be first and then the string from something like self.inputField.text.
I have everything working up to this point as far as opening/closing of sockets, sending/receiving etc.
The problem is my sent string looks like so: \tSOMECOMMAND but the \t is not being interpreted as a tab but as string '\tSOMECOMMAND'. How can I prepend a \t (tab) to the text in my input field?
As always thanks in advance!
You shouldn't require the user to add the tab character as it's counter-intuitive, easy to forget and an implementation detail they don't need to know about.
In your controller object:
Get the string from the text field.
Prepend the tab character.
Send the string to the server.
A user typing a backslash and a 't' is not necessarily the same as an escape sequence for a tab.
Those will be sent as two literal characters.
Your code needs to identify that escape sequence string and replace the string with a tab character. That's what the compiler does to an escape sequence when it is found within delimiters within which it expects there might be an escape sequence.
I'm feeding some NSString data (forum posts queried from my website) into a UILabel as part of my first app. Thing is, sometimes, depending on the content of the post, the Label goes entirely blank. I've tinkered enough to discern that there are certain characters that cause the problem, but I can't quite pin down the full set.
Is there a collected list of character types to watch out for with this kind of thing? And even better, is there a method for escaping them, or automatically converting them into something more acceptable?
Thank you for helping out a n00b!
Looks like you have whitespace or new line in your string:
Try this:
NSString* labelText = [stringFromWebsite stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]
I am trying to give you some clue even you haven't put piece of code here in your question.
You should firstly try to encode that coming String by using appropriate method like encoding:NSUTF8StringEncoding and still and try to set decoded string to your UIlabel.
Another alternative is that you can create some regex for filtering purpose of that coming string. you can find many of similar thread over the google.
I am working on an email validation link for a website. When a user registers and finishes filling in their personal data (and it passes all the checks), they are sent to a jsp page saying that an email has been sent to the address they entered as the username, with a link to click to validate the email address. So that part is all well and good, I generate the link (for now just using my localhost) and it looks like this as an example http://localhost:9999/javawork/msc/validate/?6FRQ8RAT&u=1s3w1Iih64egX01188HT. When they click the link it goes to the jsp page index.jsp in the validation folder. At this point I need to grab the entire URL and send it to a function to make sure the URL is formatted properly (for security purposes). If it passes and the format is fine, I need to grab the 8 digit code immediately after the '?' and also the value of 'u'. I then send those values to a function that checks that they match what we have in our DB, and if they do, I update the DB record with a validation date so we know they have validated their email address.
So my question is first, how do I grab the entire URL to check the format, and second, how do I grab the 8 digit code, and the value of 'u'? I have been looking online and all examples require creating multiple functions or classes, and using the URL class. And they all want me to make an instance of a URL object and initialize it using the entire URL. But it is not a static URL, it will be different for every user that registers, as it generates a random 8 digit code to check against, and the value of 'u' is the masked user id from the DB. I don't understand how it can require you to initialize the entire URL in order to get the values, when you don't know what the values are until you get them from the URL.
Is there a simple way to grab the values, and the entire URL? Even if I can just get everything after the '?', I know the base URL and can build a new String to check the formatting if I can get from the '?' and after. Please help with that part. Thanks.
The Interface HTTPServletRequest contains a method getRequestURL which returns a StringBuffer which you may use to check the format of the entire URL.
You can get it, in a jsp page with :
<%=request.getRequestURL()%>
If you are using the format of request that you specified above, then your second question :
how do I grab the 8 digit code, and the value of 'u'?
May be answered by manipulating that StringBuffer to split at the ? and & for the 8 digit code.
Or use another request method,
ServletRequest.getParameter(java.lang.String name)
To grab each parameters, though, i'm not certain how it will end up handling the unnamed parameter of the 8 digit code. Let me know how that goes.
Don't think of the 8-digit code as an unnamed parameter. Think of it as a parameter without a value.
request.getParameterNames() will give you the 8-digit code as well as "u". So you can loop through like so:
String code = "";
for(String paramName : request.getParameterNames()) {
if(!paramName.equalsIgnoreCase("u"))
code = paramName;
}
I am just starting a very basic program in Grails (never used it before, but it seems to be very useful).
What I have so far is:
in X.groovy,
a String named parameters, with constraint of maximum length 50000 and a couple other strings and dates, etc.
in XController.groovy,
static scaffold = X;
It displays the scaffold UI (very handy!), and I can add parameter strings and the other objects associated with it.
My problem is that the parameters string is a long string with formatting that is pasted in by the user. When it is displayed on the browser, however, it does not retain any carriage returns.
What is the best way to go about this? I'm a very beginner at Grails and still have lots and lots of learning to do on this account. Thanks.
The problem is that the string is being displayed using HTML which doesn't parse \n into a new line by default. You need to wrap the text in <pre> (see: http://www.w3schools.com/tags/tag_pre.asp) or replace the \n with <br/> tags to display it correctly to the user.