NSURL is nil when using NSDate - iphone

I have an NSURL which contains a URL and a variable that is an NSDate:
NSURL *url = [[[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://www.googlebio.com/xml_test.aspx?date=%#",self.storeDate]] autorelease];
I also tried it this way but to no avail:
NSString*string = [NSString stringWithFormat:#"http://www.googlebio.com/xml_test.aspx?date=%#",self.storeDate];
NSURL*url=[NSURL URLWithString:string];
[url autorelease];
The application does not crash but when I debug it, url is nil.
Any ideas as to why this is?
Thanks.
Stefan.

If your date is in a format that NSURL will be confused by, then it will be nil. Make sure you urlencode your date, or represent it in a way that is a legitimate URL. Look into this:
[string stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
Perhaps if you put a debug statement of your url string, I can help further

Related

iphone url escaping problems

Hi I have to send data to server via JSON and usually I do it like that:
NSMutableString * temp=[[NSMutableString alloc] initWithString:service_registra_inc];
//here I add more staff to temp
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:temp]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
but I get some error message that the url is wrong: something like url length but I have searched around and it means I have to escape my url si I have found this fonction that doesn't work for me:
NSString *temp2=(__bridge_transfer NSString*)CFURLCreateStringByAddingPercentEscapes(NULL,((CFStringRef)temp.UTF8String),NULL,NULL,kCFStringEncodingUTF8);
and there the program just stops and it says signal EXC_BAD_ACCess.
Well I don't really know how to transform mutable strings into CFStringRef so xcode just suggested the corrections for me but I don't really understand what is happening. Please help.... I have read the doc but it doesn't say how to cast NSSMutableString to CFStringRef and back or how to use the whole thing to create an NSURL object directly. Thks
Why are you using CFURL & CFStringRef functions here?
You could do what you are trying to do via NSString's stringByAddingPercentEscapesUsingEncoding: method. I've linked the documentation for you.
Something like:
NSMutableString * temp=[[NSMutableString alloc] initWithString:service_registra_inc];
// append your staff... errr, stuff here.
NSString * temp2 = [temp stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:temp2]];
[NSURLConnection alloc] initWithRequest:request delegate:self];
(don't forget to release things if you're not using ARC)
An NSString * is also a CFStringRef through a mechanism known as toll-free bridging and an NSMutableString * is also an NSString * through inheritance. So your second line of code should be:
NSString *temp2 = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
NULL,
(CFStringRef)temp,
NULL,
NULL,
kCFStringEncodingUTF8);
Though in practice you might prefer:
NSString *temp2 =
[temp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Which has the secondary advantage of returning an object with a non-owning reference, so you don't need to worry about releasing it even if you're not using ARC.

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://

NSURL with string

I have problem with NSURL. I am trying to create NSURL with string
code
NSString *prefix = (#"tel://1234567890 ext. 101");
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [[NSURL alloc] initWithString:dialThis];
NSLog(#"%#",url);
also tried
NSURL *url = [NSURL URLWithString:dialThis];
but it gives null . what is wrong ?
Thanks..
Your problem is the unescaped spaces in the URL. This, for instance, works:
NSURL *url = [NSURL URLWithString:#"tel://1234567890x101"];
Edit: As does this..
NSURL *url2 = [NSURL URLWithString:[#"tel://1234567890 ext. 101"
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
Before passing any string as URL you don't control, you have to encode the whitespace:
NSString *dialThis = [prefix stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// tel://1234567890%20ext.%20101
As a side note, iOS is not going to dial any extension. The user will have to do that manually.
From Apple URL Scheme Reference: Phone Links:
To prevent users from maliciously redirecting phone calls or changing the behavior of a phone or account, the Phone application supports most, but not all, of the special characters in the tel scheme. Specifically, if a URL contains the * or # characters, the Phone application does not attempt to dial the corresponding phone number.
Im not sure the "ext." in phone number can be replce by what value? but you can try like this,
NSString *prefix = [NSString stringWithString: #"tel://1234567890 ext. 101"];
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [NSURL URLWithString:[dialThis stringByReplacingOccurrencesOfString:#" ext. " withString:#"#"]];
// it might also represent by the pause symbol ','.
you can go to find the ext. is equivalent to what symbol in the phone, then replace it.
but dunno it can be work in actual situation or not....
As with iOS 9.0,
stringByAddingPercentEscapesUsingEncoding:
has been deprecated.
Use the following method for converting String to NSURL.
let URL = "URL GOES HERE"
let urlString = URL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())
If you've got something you think should be a URL string but know nothing about how URL strings are supposed to be constructed, you can use NSURL's URLWithDataRepresentation:relativeToURL: method. It parses the URL string (as bytes in an NSData) and percent-encodes characters as needed. Use the NSUTF8StringEncoding for best results when converting your NSString to NSData.
NSURL *url = [NSURL URLWithDataRepresentation:[#"tel:1234567890 ext. 101" dataUsingEncoding:NSUTF8StringEncoding] relativeToURL:nil];
NSLog(#"%#",url);
creates a URL with the string 1234567890%20ext.%20101
It attempts to do the right thing. However, for best results you should find the specification for the URL scheme you using and follow it's syntax to create your URL string. For the tel scheme, that is https://www.rfc-editor.org/rfc/rfc3966.
P.S. You had "tel://" instead of "tel:" which is incorrect for a tel URL.
Try this one, It works for me....
NSString *prefix = (#"tel://1234567890 ext. 101");
NSString *dialThis = [NSString stringWithFormat:#"%#", prefix];
NSURL *url = [NSURL URLWithString:[queryString stringByReplacingOccurrencesOfString:#" " withString:#"%20"]];
NSLog(#"%#",url);
Make an extension for use in any part of the project as well:
extension String {
var asNSURL: NSURL! {
return NSURL(string: self)
}
}
From now you can use
let myString = "http://www.example.com".asNSURL
or
myString.asNSURL

Not able to create valid nsurl

I am trying to parse data from a xml web service but I am not able to get my url. Please check my code:
NSLog(#"log url: %#",url);
[url stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSLog(#"%#",url);
[url stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSLog(#"%#",url);
NSURL *URL = [NSURL URLWithString:url];
NSXMLParser *home_Parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[home_Parser setDelegate:self];
[home_Parser parse];
[home_Parser release];
I am always getting URL as nil. The reason is my string url has space included. I have tried to replace it and escape it but it is not working. Following is the url that I am using
http://www.mydomain.com/radioListGenre.php?genre=Radiostations playing Rock
how can I remove this space. Is there any other technique the those I used above.
Thanks
Pankaj
stringByAddingPercentEscapesUsingEncoding and stringByReplacingOccurrencesOfString operate on the string provided and return a string. You're not assigning the return value to anything so url isn't changing, the return value is just vanishing into space. You want something more like:
url = [url stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
assuming url is a mutable string. Now url contains the modified version of url. Without the assignment it's effective the same as doing something like this:
a + 1;
instead of:
a = a + 1

NSURL declaration /allocation showing error

I Have tried
NSString *alertTxt =textfieldName.text;
NSLog(#"Name: %#",alertTxt);
NSURL *urlAddress = [[NSURL alloc ] initWithString: #"%#/login/index.php",alertTxt];
error : too many argument to function initstring ..
the user will giv the url address to the alertText field and i hav to embed in to the webkit may i know the process how to make it !!!
Where im making mistake kindly help me out in this
Thanks
-initWithString: can only take a complete string, not a format string. To use -initWithString: you need to complete the string first.
NSString* urlString = [NSString stringWithFormat:#"%#/login/index.php", alertTxt];
NSURL* urlAddress = [[NSURL alloc] initWithString:urlString];
BTW, it may be more efficient to use -stringByAppendingString:.
NSString* urlString = [alertTxt stringByAppendingString:#"/login/index.php"];