i'd like to use this code to know my ip, but i got 2 warnings that i can't fix for now. I also found this post : Accessing IP Address with NSHost
but i just wanted to understand why this code does not work, if anyone has an answer?
here's my code :
-(NSString*)getAddress {
NSString *iphone_ip = [NSString initWithString:#"127.0.0.1"];
NSHost* myhost =[NSHost currentHost];
if (myhost)
{
NSString *ad = [myhost address];
if (ad)
strcpy(iphone_ip,[ad cStringUsingEncoding: NSISOLatin1StringEncoding]);
}
return [NSString stringWithFormat:#"%s",iphone_ip];
}
the first warning is on :
NSHost* myhost =[NSHost currentHost]
, saying that currentHost is not recognised.
The second one is on
NSString *ad = [myhost address];
" incompatible obj-c types initializing 'struct NSData ", expected 'struct NString" "
I can imagine the second warning might disappear when the first warning is resolved...
Thanks for your help
Paul
As you can see from the documentation here, it's documented under the OS X library.
NSHost
It's actually a private API on the iPhone. You should still be able to use it, but you'll get compiler warnings.
If you need to find out your IPAddress, you can use a NSURLRequest and NSURLConnection using this URL: WhatIsMyIP API
That page is there specifically for programmers to use. They ask that you ping it no more than once every 300 seconds. You can find a FAQ here: FAQ
Related
i am trying to accomplish country specific Cuisine places, means like mexican food, spanish food, Thai food, indian food, via google places API on an iPhone app.
How can i do that?? As i see supported types, there is only food, restaurant, etc in it. besides whenever i use this query..
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%#&type=restaurant&query=%#&sensor=true&key=%#", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:#"%i", currenDist], googleType, kGOOGLE_API_KEY];
i am getting few results only, whenever i try to add more types like food|restaurant|establishment like below query my app crashes.
NSString *url = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%f,%f&radius=%#&type=food|restaurant|establishment&query=%#&sensor=true&key=%#", currentCentre.latitude, currentCentre.longitude, [NSString stringWithFormat:#"%i", currenDist], googleType, kGOOGLE_API_KEY];
As i told my app crashes with this error may be i am doing something wrong with url.
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
* First throw call stack: (0x13be022 0x200fcd6 0x1366a48 0x13669b9 0xbadfad 0x2dd2 0x13bfe42 0xaa09df 0x139294f 0x12f5b43 0x12f5424
0x12f4d84 0x12f4c9b 0x15d47d8 0x15d488a 0x17a626 0x236d 0x22d5)
terminate called throwing an exception(lldb)
Please can someone guide me with this?? i hope i cleared all my points.
Thanks & regards,
Malhaar
It appears you are trying to use the Places API Textsearch as you are using the query parameter. If this is correct make sure you are hitting the correct backend:
maps.googleapis.com/maps/api/place/textsearch/json
Also make sure you are using the correct parameter to specify types:
types=food|restaurant|establishment
I would recommend reading the documentation thoroughly and and performing a few test html requests through your browser to make sure you have the right request before plugging it into your application.
Although this is almost 7 months old, and I'm sure you've either solved it or given up on it by now, I have just had the same issue and it took some resolving. I thought I'd put the answer here to help anyone else with a similar error.
The problem I found was to do with the URL encoding not liking the | character.
The way around it I found was to:
// Create an NSString with the URL which includes the '|' characters.
NSString * urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.189781,-114.488907&radius=6918&types=food|bar&keyword=&sensor=true&key=YOUR KEY HERE";
// Encode the URL
NSString* encodedURLString = [urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
// Covert to a NSURL
NSURL *googleRequestURL=[NSURL URLWithString:encodedURLString];
And then use the googleRequestURL as the URL.
Hopefully that will solve it for you!
Okay, all I am doing is setting an NSString to a value with this code:
NSString *stringURL = [NSString stringWithFormat:#"http://api.themoviedb.org/3/movie/%#/trailers?api_key=1523229ded5824dab8bb7840782db266",searchID];
This is a string that I then turning into a URL for querying the TMDB database. This line of code gives me a BAD_EXC_ACCESS and it is blowing my mind because using this sort of NSString construction is something I have done thousands of times without a problem.
The one other thing to note is that this line is being executed right after another query call is made. The weird thing is that call makes the stringURL the same way, yet it works fine.
Any help would be appreciated...
You need to use %i to log an NSInteger, not %#
You need to use the following
NSString *stringURL = [NSString stringWithFormat:#"http://api.themoviedb.org/3/movie/%d/trailers?api_key=1523229ded5824dab8bb7840782db266",searchID];
Because searchID has NSInteger type and you are using "%#"
If it's an NSInteger you need to use %ld or you will got a warning, you can also use %d and explicitly cast to int via (int)searchID
Im getting this annoying error and tried every i know but in this case it hasnt helped.
I have in my delegate....
vid_name = [push objectForKey:#"vid"];
(in the console .... vid = "video" )
now in my normal page i have
NSString *videoName = [(MissileAppDelegate *)[[UIApplication sharedApplication] delegate] vid_name];
NSString *path = [[NSBundle mainBundle] pathForResource:videoName ofType:#"mp4"];
when i run this and it comes to play the video, i get an error that doesnt even relate to the video, it happens with all variables i try and pass over using the [push objectForKey:#"vid"];. if i just vid_name =#"video" it works fine.
Any Ideas?
Alex
First of all do not name your variables like vid_name! You should always use vidName in Objective-C.
You should share more information, I'm not really getting what you're doing and the error message you get would be niche.
But It sounds like a memory management problem. Because when you use the #"video" this is a static string that is always there. But when you use objectForKey: you get an object that is autoreleased and get's eventually deallocated at some point. So make sure your vid_name still exists when accessing it from your 'normal page'.
Cocoa Memory Management Programming Guide
I have a strange behave:
After making a few changes in my code, I got an unasked URL in the NSURL. Actually, this URL was valid in a test I made yesterday, but the system remembers this URL and I cannot changed it even in another app.
When I strted a new app with the followung code:
int main(int argc, char *argv[]) {
NSURL *URLurl = [NSURL URLWithString:#"http://www.google.com"];
NSLog([URLurl absoluteString]);
}
the baseUrl is something like: http:inl.co.il, even I asked for google.com,
the NSLog is correct and prints: http://www.google.com
But it does not help me since the base is what the entire code works with.
And strangly, where from is that NSURL taking this old unused string?
Any help?
Thank you
Never log variables directly inside of NSLog like this, as any percent symbols will easily cause mayhem. Instead do:
NSLog(#"%#", [URLurl absoluteString]);
Lacking further information, this is probably the root of the problem. Also, the compiler should be warning you your original statement is dodgy, anyway.
I'm converting data (from a web page) to a string). The basic code works (but there's been some subtle change somewhere - maybe on server).
NSLog shows the expected string (maybe 1000 chars long). However, when I float over responseString, it shows "Invalid". Worse, parsing with componentsSeparatedByCharactersInSet does not work.
Ideas?
NSString *responseString;
responseString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSLog([NSString stringWithFormat:#"responsestring ='%#'",responseString]);
if ([responseString compare:#""] != NSOrderedSame) {
lines = [responseString componentsSeparatedByCharactersInSet: [NSCharacterSet characterSetWithCharactersInString:#";"]];
This may happen when the configuration is set to "Release" rather than "Debug" I think.
Do not trust what the debugger says, it is not accurate, this has happened to me and took me a while to realise that the xcode debugger is not always right and should not be trusted, so i no longer trust the debugger and i use nslog statements whenever it tries to tell me something is invalid. So dont worry about it it happens, and when it happened to me I was also parsing responses from some webservice.
Just to be clear -- my experience with seeing "Invalid" in the debugger means that the reference is to an already-released object.
Your question and the comments below seem to suggest that you are thinking "Invalid" is an actual string value -- but are you sure you don't just have a memory management probably?