Obtain the IP address of an Apple device

Source: Internet
Author: User

Original address: get the IP address of the Apple device

Mac

Local

-(Nsstring *) getipwithnshost {// obtain all local addresses nsarray * addresses = [[nshost currenthost] addresses]; nsstring * stringaddress = nil; For (nsstring * anaddress in addresses) {// find the correct IP address if (! [Anaddress hasprefix: @ "127"] & [[anaddress componentsseparatedbystring: @ "."] Count] = 4) {stringaddress = anaddress; break ;}} return stringaddress ;}

Remote

-(Nsstring *) getip {nsuinteger an_integer; nsarray * ipitemsarray; nsstring * externalip; // This is the request for a domain name nsurl * ipurl = [nsurl urlwithstring: @ "http://www.dyndns.org/cgi-bin/check_ip.cgi"]; If (ipurl) {nserror * error = nil; nsstring * theiphtml = [nsstring stringwithcontentsofurl: ipurl encoding: nsutf8stringencoding error: & error]; If (! Error) {nsscanner * thescanner; nsstring * text = nil; Theil = [nsscanner scannerwithstring: theiphtml]; while ([thepolicisatend] = No) {// find start of tag [theincluscanuptostring: @ "<" inclustring: NULL]; // find end of tag [theincluscanuptostring: @ ">" inclustring: & text]; // Replace the found tag with a space // (you can filter multi-spaces out later if you wish) theiphtml = [theiphtml stringbyreplacingoccurrencesofstring: [nsstring stringwithformat: @ "% @>", text] withstring: @ ""]; ipitemsarray = [theiphtml componentsseparatedbystring: @ ""]; an_integer = [ipitemsarray indexofobject: @ "Address:"]; externalip = [ipitemsarray objectatindex: ++ an_integer];} nslog (@ "% @", externalip);} else {nslog (@ "Oops... g % lD, % @ ", [error code], [error localizeddescription]) ;}return externalip ;}

Mobile devices (iPhone, iPod ...)

-(Nsstring *) getaddress {char iphone_ip [255]; // default value: "127.0.0.1" strcpy (iphone_ip, "127.0.0.1"); nshost * myhost = [nshost currenthost]; if (myhost) {nsstring * Ad = [myhost address]; If (AD) {// copy to the character array strcpy (iphone_ip, [ad cstringusingencoding: nsisolatin1stringencoding]);} return [nsstring stringwithformat: @ "% s", iphone_ip];}

MAC address

# Include <stdio. h> # include <corefoundation/corefoundation. h> # include <iokit/iokitlib. h> # include <iokit/Network/ioethernetinterface. h> # include <iokit/Network/ionetworkinterface. h> # include <iokit/Network/ioethernetcontroller. h> static kern_return_t findethernetinterfaces (io_iterator_t * matchingservices); static kern_return_t getmacaddress (io_iterator_t intfiterator, uint8 * macaddress, uint8 buffersize); // Returns an iterator containing the primary (built-in) Ethernet interface. the caller is responsible for // releasing the iterator after the caller is done with it. static kern_return_t findethernetinterfaces (io_iterator_t * matchingservices) {kern_return_t kernresult; outcome; // Ethernet interfaces are instances of class kioethe Rnetinterfaceclass. // ioservicematching is a convenience function to create a dictionary with the key kioproviderclasskey and // the specified value. matchingdict = ioservicematching (kioethernetinterfaceclass); // note that another option here wocould be: // matchingdict = iobsdmatching ("en0"); // But en0: isn' t necessarily the primary interface, especially on systems with multiple Ethernet ports. If (null = matchingdict) {printf ("ioservicematching returned a null dictionary. \ n ");} else {// each ionetworkinterface object has a boolean property with the key kioprimaryinterface. only the // primary (built-in) interface has this property set to true. // ioservicegetmatchingservices uses the default matching criteria defined by ioservice. this considers // only the following properties plus Any family-specific matching in this order of precedence // (see ioservice: passivematch): // kioproviderclasskey (ioservicematching) // kionamematchkey (ioservicenamematching) // kiopropertymatchkey // attributes // family-specific matching // kiobsdnamekey (iobsdnamematching) // kiolocationmatchkey // The ionetworkingfamily does not define any family-specifi C matching. this means that in // order to have ioservicegetmatchingservices consider the kioprimaryinterface property, we must // Add that property to a separate dictionary and then add that to our matching dictionary // specifying kiopropermattychkey. propertymatchdict = cfdictionarycreatemutable (kcfallocatordefault, 0, & kcftypedictionarykeycallbacks, & kcftypedictionaryvaluecallbacks); If (null = Propertymatchdict) {printf ("cfdictionarycreatemutable returned a null dictionary. \ n ");} else {// set the value in the dictionary of the property with the given key, or add the key // to the dictionary if it doesn't exist. this call retains the value object passed in. cfdictionarysetvalue (propertymatchdict, cfstr (kioprimaryinterface), kcfbooleantrue); // now add the dictionary containing Matching value for kioprimaryinterface to our main // matching dictionary. this call will retain propertymatchdict, so we can release our reference // on propertymatchdict after adding it to matchingdict. cfdictionarysetvalue (matchingdict, cfstr (kiopropertymatchkey), propertymatchdict); cfrelease (propertymatchdict); }}// specify retains the returned iterator, so release Iterator when we're done with it. // ioservicegetmatchingservices also consumes a reference on the matching dictionary so we don't need to release // The dictionary explicitly. kernresult = ioservicegetmatchingservices (kiomasterportdefault, matchingdict, matchingservices); If (kern_success! = Kernresult) {printf ("ioservicegetmatchingservices returned 0x % 08x \ n", kernresult);} return kernresult;} // given an iterator implements ss a set of Ethernet interfaces, return the MAC address of the last one. // if no interfaces are found the MAC address is set to an empty string. // In this sample the iterator shoshould contain just the primary interface. static kern_return_t getmacaddress (io_iterator_t in Tfiterator, uint8 * macaddress, uint8 buffersize) {io_object_tintfservice; io_object_tcontrollerservice; kern_return_tkernresult = kern_failure; // make sure the caller provided enough buffer space. protect Against buffer overflow problems. if (buffersize <kioethernetaddresssize) {return kernresult;} // initialize the returned address bzero (macaddress, buffersize); // ioiteratornext retains the retu Rned object, so release it when we're re done with it. while (intfservice = ioiteratornext (intfiterator) {cftyperefmacaddressascfdata; // ionetworkcontrollers can't be found directly by the ioservicegetmatchingservices call, // since they are hardware nubs and do not participant in driver matching. in other words, // registerservice () is never called on them. so we 've found the ionetworkinterface And Will // get its parent controller by asking for it specifically. // ioregistryentrygetparententry retains the returned object, so release it when we're re done with it. kernresult = ioregistryentrygetparententry (intfservice, kioserviceplane, & controllerservice); If (kern_success! = Kernresult) {printf ("ioregistryentrygetparententry returned 0x % 08x \ n", kernresult );} else {// retrieve the MAC address property from the I/O registry in the form of a cfdata macaddressascfdata = ioregistryentrycreatecfproperty (controllerservice, cfstr (kiomacaddress), kcfallocatordefault, 0 ); if (macaddressascfdata) {cfshow (macaddressascfdata); // For display purposes only; output goes to stde Rr // get the raw bytes of the MAC address from the cfdata cfdatagetbytes (macaddressascfdata, cfrangemake (0, kioethernetaddresssize), macaddress); cfrelease (macaddressascfdata );} // done with the parent Ethernet controller object so we release it. (void) ioobjectrelease (controllerservice);} // done with the Ethernet interface object so we release it. (void) ioobjectrelease (intfservice);} return Kernresult;} int main (INT argc, char * argv []) {Signature = kern_success; signature; uint8macaddress [Signature]; kernresult = findethernetinterfaces (& intfiterator); If (kern_success! = Kernresult) {printf ("findethernetinterfaces returned 0x % 08x \ n", kernresult);} else {kernresult = getmacaddress (intfiterator, macaddress, sizeof (macaddress); If (kern_success! = Kernresult) {printf ("getmacaddress returned 0x % 08x \ n", kernresult );} else {// output the current MAC address printf ("this system's built-in MAC address is % 02x: % 02x: % 02x: % 02x: % 02x: % 02x. \ n ", macaddress [0], macaddress [1], macaddress [2], macaddress [3], macaddress [4], macaddress [5]);} (void) ioobjectrelease (intfiterator); // release the iterator. return kernresult ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.