Formatting NSURL - objective-c

In my app, I need to make a json request to google direction service through NSURL.
I'm able to get JSON response, when the URL is static.
NSURL *googleMapsURL=[NSURL URLWithString:#"http://maps.googleapis.com/maps/api/directions/json?origin=SanFrancisco,CA&destination=Dallas,TX&waypoints=Los+Angeles,CA%7CHouston,TX&mode=driving&sensor=false"];
NSData *data=[NSData dataWithContentsOfURL:googleMapsURL];
But, when I specify the source and destination stations dynamically, it's not working.
NSString *urlString=[NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/directions/json?origin=%#&destination=%#&waypoints=Los+Angeles,CA%7CHouston,TX&mode=driving&sensor=false",sourceStation, destinationStation];
NSURL *googleMapsURL=[NSURL URLWithString:urlString];
NSData *data=[NSData dataWithContentsOfURL:googleMapsURL];
It's generating the following error.
2012-05-08 03:30:15.960 SupplyTrackerApp2[3079:16403] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(0x19a7052 0x2448d0a 0x194fa78 0x194f9e9 0xecdfbd 0x2aae6 0x2a900 0x56ffbf 0x57021b 0x5704c3 0x580b71 0x5813df 0x581986 0x5815a4 0x34650 0x19a8ec9 0x4ab5c2 0x4ab55a 0x550b76 0x55103f 0x5502fe 0x4d0a30 0x4d0c56 0x4b7384 0x4aaaa9 0x1bbefa9 0x197b1c5 0x18e0022 0x18de90a 0x18dddb4 0x18ddccb 0x1bbd879 0x1bbd93e 0x4a8a9b 0x25cd 0x2535 0x1)
terminate called throwing an exception
Can any one help me?

To me it looks like either sourceStation or destinationStation is nil. For debugging you could try setting them explicitly in the lines before your NSString *urlString definition.
NSString *sourceStation = #"SanFrancisco,CA";
NSString *destinationStation = #"Dallas,TX";
If this works, then it's a problem with filling the respective NSStrings...

Related

+[NSData bookmarkDataWithContentsOfURL:]: unrecognized selector sent to class

I am trying to resolve an alias file's original path using Objective-C(or maybe C++; it's an .mm file). Not being very much familiar, I am somehow missing + and - methods' usage. I am aware of them being class and instance methods respectively, but in practice, the following the code, with the indicated lines give me following warning and error(at build):
Class method '+bookmarkDataWithContentsOfURL:' not found (return type defaults to 'id')
-
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSData bookmarkDataWithContentsOfURL:]: unrecognized selector sent to class 0x7fff88942cb8'
with 0x7fff88942cb8 being the NSData address as per lldb.
Which files should I make the changes in, to get bookmarkDataWithContentsOfURL:error: and URLByResolvingBookmarkData to work?
void *pathclass::resolveAliasFromURL(const char *filepath) const
{
NSError *error = nil;
NSString *filepathh = [[NSString alloc] initWithUTF8String:filepath];
NSData *bookmarkk = [NSData bookmarkDataWithContentsOfURL:filepathh]; /*problematic line*/
BOOL isstale = NO;
NSURL *actual = [NSURL URLByResolvingBookmarkData:bookmarkk bookmarkDataIsStale:isstale error:error];/*another problematic line, but build fails already*/
NSString *urlString = [actual absoluteString];
NSLog(#"%#",urlString);
}
If there are any other faults, please point out.
Your call to bookmarkDataWithContentsOfURL: is wrong in a few ways:
The signature looks like this:
+ (NSData *)bookmarkDataWithContentsOfURL:(NSURL *)bookmarkFileURL error:(NSError * _Nullable *)error;
First, the first parameter is of type NSURL*, not NSString*. Next, you miss off the error parameter completely (despite defining a variable for it). Lastly, the method is a class method on NSURL not NSData (NSData* is the return type).
So, first, make your file path into an NSURL*:
NSURL* bookmarkUrl = [NSURL URLWithString:filepathh];
Then, call the function using the proper arguments:
NSData *bookmarkk = [NSURL bookmarkDataWithContentsOfURL:bookmarkUrl error:&error];
You should check the returned value against nil - if it's nil, then an error occurred, and the error information will be contained inside error.
The documentation is quite helpful.
Your call to URLByResolvingBookmarkData:options:relativeToURL:bookmarkDataIsStale:error: has similar problems: you are missing several parameters, the first parameter should be NSURL, etc. Again, the documentation should help.

Facing issue while converting NSString to NSURL

I have an assets library url to a photo:
"assets-library://asset/asset.JPG?id=8D4D7820-EDD6-4CA4-A44A-2ACD53A112FC&ext=JPG"
I have this in a string property imgURLString.
I am trying to convert this to NSURL in the following way:
NSURL *imageURL = [NSURL URLWithString:self.imgURLString];
However, I am getting an exception:
[NSURL length]: unrecognized selector sent to instance 0xa8281f0
I am sure I have the above mentioned string in self.imgURLString and it is not trying to convert an empty string. So why am I getting this exception and how do I solve it?
I am basically trying to convert the string to an NSURL to fetch the photo using the assets library url.
Your self.imgURLString is actually already an NSURL, not an NSString. You are assigning the asset URL (an NSURL) to your NSString property. Change your property to be an NSURL.

Parse JSON with iOS 6 and xCode 4.5.2

I'm new in objective-c and and building a sign in page for a specific website. When the user provide information the following JSON return back from the server:
{
"object":{},
"resultCode":0,
"resultMessage":"You successfully signed in "
}
And this what I'm trying to do based on a tutorial I found:
-(IBAction)login{
// Download JSON
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString:[NSString stringWithFormat:#"https://www.example.com/login?username=%#&password=%#", nationalID.text,passowrd.text]]];
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray *response = [json objectForKey:#"resultMessage"];
NSLog(#"response %#",response);
}
I want to log out "resultMessage" but somehow it throws an exception:
2012-12-13 06:46:50.607 myProject[7178:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:
(0x1c8d012 0x10cae7e 0x1c8cdeb 0xbff817 0x2f1a 0x10de705 0x15920 0x158b8 0xd6671 0xd6bcf 0xd5d38 0x4533f 0x45552 0x233aa 0x14cf8 0x1be8df9 0x1be8ad0 0x1c02bf5 0x1c02962 0x1c33bb6 0x1c32f44 0x1c32e1b 0x1be77e3 0x1be7668 0x1265c 0x279d 0x26c5)
libc++abi.dylib: terminate called throwing an exception
Looks like the -dataWithContentsOfURL: message failed and returned nil, which is exactly what the error message says. You need to confirm that your -stringWithFormat: message is giving you the correct URL format, and check whether the server is returning what you expect.
if you could observe the issue description:
'NSInvalidArgumentException', reason: 'data parameter is nil'
means the response from your response from the web url is nil or NULL
this is not a issue related to JSON parser.
Looks like you have a simple typo, you are using:
nationalID.text,passowrd.text
You probably meant to not misspell 'password'.
First Try static url like:
https://www.example.com/login?username=admin&password=123
if this returns you correct response then try Property Variables like:
https://www.example.com/login?username=self.username.text&password=self.password.text
And one mistake you have made is that resultMessage is string not an Array.
Hope this helps you

Why does NSMutableURLRequest instantiation cause a crash?

I am trying to create a NSMutableURLRequest like this:
NSURL *URLWithString = [
NSString stringWithFormat:#"%#?%#",
urlString,
datas
];
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:URLWithString] autorelease];
When I run it on my iPhone 4S, the app crashes and I get the following exception:
2012-10-30 15:58:53.495 [429:907] -[__NSCFString absoluteURL]:
unrecognized selector sent to instance 0x1cd74a90
2012-10-30 15:58:53.497 [429:907] --- Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '-[__NSCFString
absoluteURL]: unrecognized selector sent to instance 0x1cd74a90'
--- First throw call stack:
(0x361b62a3 0x344c697f 0x361b9e07 0x361b8531 0x3610ff68 0x3611363f
0x320396e7 0x32039551 0x320394ed 0x33bde661 0x33bde597 0x387e1
0x376d9f1f 0x376da9a9 0x341c535d 0x3618b173 0x3618b117 0x36189f99
0x360fcebd 0x360fcd49 0x366392eb 0x374db301 0x37cc1 0x37c58)
libc++abi.dylib: terminate called throwing an exception
What's wrong?
Lots of issues:
For a start look into how to invoke NSString and NSURL methods. I have done it for the code you have pasted.
NSURL * myUrl = [NSURL URLWithString:[NSString stringWithFormat:#"%#?%#",urlString,datas]];
and
NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] initWithURL:myUrl] autorelease];
Your NSURL creation code is wrong.
NSURL *URLWithString = [NSString stringWithFormat: #"%#?%#", urlString, datas];
Here, you are trying to create an NSURL directly with an NSString class method (stringWithFormat). The result is that your variable URLWithString will be of the wrong type, and when you send it an NSURL message you'll get a crash like you are getting.
To fix this you need to create an NSString of the URL address first, and instantiate an NSURL using that, like so:
NSString *completeURLString = [NSString stringWithFormat:#"%#?%#", urlString, datas];
NSURL *completeURL = [NSURL URLWithString: completeURLString];
(Technically, these two lines can be combined; I've separated them to make it clear what's going on.)
Also, whilst probably not related to your crash, you shouldn't call your URL variable URLWithString, as that is the name for a class method of NSURL. Make sure to come up with a unique name for it, and to start it with a lower case character (at the very least, this will make your code easier to decipher for others).

how to solve run time error of [NSURL fileURLWithPath:error:]: unrecognized selector sent to class?

I am beginner in IPhone Developing. I want play sound. so, that's why I have apply this code
(void)viewDidLoad
{
NSError* err;
path=[[NSBundle mainBundle] pathForResource:#"Animalfile" ofType:#"plist"];
dict=[NSDictionary dictionaryWithContentsOfFile:path];
NSArray *animalaudio=[dict valueForKey:#"audio"];
NSString *audiolist=[animalaudio objectAtIndex:currentsound];
AVAudioPlayer *audio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:audiolist error:&err]];
audio.delegate=self;
[audio play];
}
and I have got the run time error of
[NSURL fileURLWithPath:error:]: unrecognized selector sent to class 0x182121c
2012-07-14 14:51:21.711 plistdemo[1236:10703] *** Terminating app due to uncaught
exception 'NSInvalidArgumentException', reason: '+[NSURL fileURLWithPath:error:]:
unrecognized selector sent to class 0x182121c'
terminate called throwing an exceptionsharedlibrary apply-load-rules all
so, give any suggestion and source code which apply in my code
current documentation for NSURL doesn't list any such method like 'fileURLWithPath:error:'... seems like its either deprecated or incorrect.
instead try using something like:
[NSURL fileURLWithPath:audioList]
hope it helps...