Passing NSString in NSURL causes app crash [duplicate] - iphone

This question already has answers here:
NSURL returns Nil Value
(2 answers)
Closed 8 years ago.
I am passing NSString to NSURL and app crashes. I think this is due to 's used in NSString
NSLog of the NSString is
http://www.test.com/?AssessmentID=040714114412 &QuestionID=113&ResponseText=yes&AssessmentName=Housekeeping&AssessmentDate=11:44:21&AssessmentQuestion=Use the guest’s or employee’s name.&ResponseComment=No comment&DepartmentID=9&SectionName=Service Standards
here is the exception
[NSURL initWithString:relativeToURL:]: nil string parameter'
I think it is crashing due to guest's 's

The problem is your URL is not properly encoded. Take this & modify your code accordingly.
NSString *relativeURL = #"http://www.something with Space and parameters";
NSURL *url = [NSURL URLWithString:[relativeURL stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
This is the right approach rather than replacing white spaces with %20
Hope that helps.

Well i tried out the following piece of code and it is working for me... can you do this as per the following code!
NSString *relativeToURL = #"http://www.test.com/?AssessmentID=040714114412 &QuestionID=113&ResponseText=yes&AssessmentName=Housekeeping&AssessmentDate=11:44:21&AssessmentQuestion=Use the guest’s or employee’s name.&ResponseComment=No comment&DepartmentID=9&SectionName=Service Standards";
NSURL *url = [[NSURL alloc] initWithString:relativeToURL];
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];

Related

Pass special characters through NSURL and NSData [duplicate]

This question already has answers here:
Objective-C and Swift URL encoding
(13 answers)
Closed 9 years ago.
I just learned the hard way that you cannot pass special characters through NSURL. I am in need of a function that will help me pass characters like "&, %, ", ñ" and others via NSURL and NSData.
My code is below. You can see below that I replace the line breaks (\n) and spaces with %20. Is there a function or simple way I can have the types of characters listed above pass through NSURL? Any help would be great! Thank you!
NSString *var1_pre = [myTextView.text stringByReplacingOccurrencesOfString:#" "
withString:#"%20"];
NSString *var1 = [var1_pre stringByReplacingOccurrencesOfString:#"\n" withString:#"%20"];
NSString *strURL = [NSString stringWithFormat:#"http://www.website.com/page.php?
var1=%#",var1];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
Use
NSString *strUrl=[#"YOURURL" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strUrl];
NSString *urlString = [NSString stringWithFormat:#"URL_STRING"];
NSURL *MyUrl = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
stringByAddingPercentEscapesUsingEncoding. convert Legal URL String.

How to adapte a NSString with a NSUrl parameter? [duplicate]

This question already has answers here:
How do I URL encode a string
(24 answers)
Closed 9 years ago.
As you know no URL contain a space between the word of there parameters
and I want to pass MyString=#"hello every body" to my URL parameters, like this
[#"http://www.site.com/index.php?contenu=" stringByAppendingString:MyString];
And I don't know how I can convert MyString to a valid format for URL
Try with Following Code :
NSString *urlString = [NSString stringWithFormat:#"http://www.site.com/index.php?contenu=%#", MyString];
NSURL *myURL = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
AlsoRead This Official Documentation about String Format Specifiers.
Why don't you do the whole thing in this manner ?
First make a string with parameter in the following way :
NSString *stringURL = [NSString stringWithFormat:#"http://www.site.com/index.php?contenu=%#",MyString];
Convert the string to URL :
NSURL *url = [NSURL URLWithString:MyString];
Now you can use that url safely.
for space u should use %20, for that use url encode
Try This
Or use stringByReplacingPercentEscapesUsingEncoding
You can try this by replacing space with %20
Code ::
NSString *reqString;
reqString = [NSString stringWithFormat:#"%s%#", Server_URL, your_paramater];
reqString = [reqString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSLog(#"... URL :::: %#", reqString);
Try it once, It may be help you.
Thanks.
try like this ,
NSString *encodedUrlString = [urlString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];

How to load address in webview in iPhone? [duplicate]

This question already has an answer here:
Forward Geocode Example using CLGeocoder
(1 answer)
Closed 4 months ago.
Currently i am working in iPhone application, Using UIWebview to load address like "America", then run the application, the map not shown on the screen. I want to shown the location from User enter search bar field, How to fix this iuissue? please help me
Thanks in Advance
I tried this:
webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 40, 320,376)];
webView.delegate=self;
webView.scalesPageToFit=YES;
NSString *mapurl = #"http://maps.google.com/maps?q=";
NSString *urlString1 = [mapurl stringByAppendingFormat:#"America"];
NSString *OmitSpace = [urlString1 stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *url=[NSURL URLWithString:OmitSpace];
NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
Screen shot:
NSString *OmitSpace = [urlString1 stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
This will omit the whitespace only. It will not convert other escape characters. So replace above line with this.
NSString *OmitSpace = [urlString1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

Escaping special characters (ø, æ) for use inside a url

I try to display an image with a URL source in an iOS application, but it doesn't show up.
The url of the image is live example path.
When escaping this string using the following Objective-C code:
NSString *url= [(NSString *)CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)originalpath, NULL, CFSTR("øæ"), kCFStringEncodingUTF8) autorelease];
the result is (with encoding of øæ) : live xml path
All my files where the URLs are stored use text encoding (UTF-8).
How do I escape the URL in a right way, such that the image will be displayed?
don't go for ascii encoding try it
NSString *URLString = [yourImagepath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:URLString];
I hope it will help you out.
I believe you need to escape your url-string, before using it as an url...
NSString *URLString = [yourImagepath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [NSURL URLWithString:URLString];
Had the same problem on iOS with German umlauts. The NSURL category NSURL+IFUnicodeURL solved the problem for me, don't get scared by the amount of C code in there, it's well-hidden :)

NSString to NSURL?

Trying to convert a string to NSURL and this is not happening.
barcodeTextLabel.text = foundCode.barcodeString;
urlToGrab = [NSString stringWithFormat:#"%#", foundCode.barcodeString]; // foundCode.barcodeString is an NSString
urlToGrab shows the following "error invalid CFStringRef"
This is how you create an NSURL from an NSString:
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
You can use following for creating the file path to url.
NSURL *yourURL = [NSURL fileURLWithPath:#"/Users/xyz/Desktop/abc.sqlite"];
If foundCode.barcodeString is the string you want as your URL, then (like the above answer) use the NSURL class method URLWithString:(NSString *).
Your code should look like:
NSURL urlToGrab = [NSURL URLWithString:foundCode.barcodeString];
Where is your error coming into to play? The way your code is, urlToGrab is an instance of NSString. I would imagine you would get an error like you described if you tried to make an HTTP request on an NSString rather than NSURL.
Swapnali patil's answer works, but I will add an explanation.
You will get a nil if the format of NSString doesn't fit file criteria for NSURL (file:///xxx/file.ext).
My needs were with loading a JPG image via URL file path to nsdata; NSURL * u=[[NSURL alloc] initWithString:fpath] returned nil, but NSURL *yourURL = [NSURL fileURLWithPath:fpath] as in mentioned answer worked. A URL for files will be file:///users/xxx/pic.jpg format and give disk access. NSURL * u=[[NSURL alloc] initWithString:(NSString*) ] will also give nil object if nsstring is web URL but if missing http://