NSHost “currentHost” not recognized

孤者浪人 提交于 2020-01-15 10:49:46

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/6535354/nshost-currenthost-not-recognized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!