how to get the external ip in objective c












6















i looked for some code that will help me to get the ip that the iphone connect with.



i find this one:



    - (NSString *)getIPAddress
{
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;

// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}

temp_addr = temp_addr->ifa_next;
}
}

// Free memory
freeifaddrs(interfaces);

return address;
}


but the problem is that he get me this ip
10.0.0.1



there is any articlecode about getting the external ip.



thank you










share|improve this question




















  • 4





    What sort of IP address are you expecting? The device is likely to be going through a WiFi or 3G network, etc. hence you're not likely to be able to retrieve any form of meaningful "external" IP address.

    – John Parker
    Mar 27 '11 at 16:42











  • Middaparka is right - from the device's viewpoint, 10.0.0.1 is its external IP address.

    – Sherm Pendley
    Mar 27 '11 at 17:43
















6















i looked for some code that will help me to get the ip that the iphone connect with.



i find this one:



    - (NSString *)getIPAddress
{
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;

// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}

temp_addr = temp_addr->ifa_next;
}
}

// Free memory
freeifaddrs(interfaces);

return address;
}


but the problem is that he get me this ip
10.0.0.1



there is any articlecode about getting the external ip.



thank you










share|improve this question




















  • 4





    What sort of IP address are you expecting? The device is likely to be going through a WiFi or 3G network, etc. hence you're not likely to be able to retrieve any form of meaningful "external" IP address.

    – John Parker
    Mar 27 '11 at 16:42











  • Middaparka is right - from the device's viewpoint, 10.0.0.1 is its external IP address.

    – Sherm Pendley
    Mar 27 '11 at 17:43














6












6








6


3






i looked for some code that will help me to get the ip that the iphone connect with.



i find this one:



    - (NSString *)getIPAddress
{
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;

// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}

temp_addr = temp_addr->ifa_next;
}
}

// Free memory
freeifaddrs(interfaces);

return address;
}


but the problem is that he get me this ip
10.0.0.1



there is any articlecode about getting the external ip.



thank you










share|improve this question
















i looked for some code that will help me to get the ip that the iphone connect with.



i find this one:



    - (NSString *)getIPAddress
{
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;

// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0)
{
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"])
{
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}

temp_addr = temp_addr->ifa_next;
}
}

// Free memory
freeifaddrs(interfaces);

return address;
}


but the problem is that he get me this ip
10.0.0.1



there is any articlecode about getting the external ip.



thank you







iphone objective-c cocoa-touch ios






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 '11 at 17:13









Black Frog

10.1k12256




10.1k12256










asked Mar 27 '11 at 16:39









MTAMTA

3,4421678168




3,4421678168








  • 4





    What sort of IP address are you expecting? The device is likely to be going through a WiFi or 3G network, etc. hence you're not likely to be able to retrieve any form of meaningful "external" IP address.

    – John Parker
    Mar 27 '11 at 16:42











  • Middaparka is right - from the device's viewpoint, 10.0.0.1 is its external IP address.

    – Sherm Pendley
    Mar 27 '11 at 17:43














  • 4





    What sort of IP address are you expecting? The device is likely to be going through a WiFi or 3G network, etc. hence you're not likely to be able to retrieve any form of meaningful "external" IP address.

    – John Parker
    Mar 27 '11 at 16:42











  • Middaparka is right - from the device's viewpoint, 10.0.0.1 is its external IP address.

    – Sherm Pendley
    Mar 27 '11 at 17:43








4




4





What sort of IP address are you expecting? The device is likely to be going through a WiFi or 3G network, etc. hence you're not likely to be able to retrieve any form of meaningful "external" IP address.

– John Parker
Mar 27 '11 at 16:42





What sort of IP address are you expecting? The device is likely to be going through a WiFi or 3G network, etc. hence you're not likely to be able to retrieve any form of meaningful "external" IP address.

– John Parker
Mar 27 '11 at 16:42













Middaparka is right - from the device's viewpoint, 10.0.0.1 is its external IP address.

– Sherm Pendley
Mar 27 '11 at 17:43





Middaparka is right - from the device's viewpoint, 10.0.0.1 is its external IP address.

– Sherm Pendley
Mar 27 '11 at 17:43












3 Answers
3






active

oldest

votes


















4














The easiest way to get your internet ip address from code is to use NSURLConnection.



For the URL you can use:
http://www.whatismyip.com/m/mobile.asp
or
http://checkip.dyndns.com/



Just parse the return data and you have your external ip address.






share|improve this answer



















  • 1





    I like it. Not so much for prod apps but GREAT for debugging. +1

    – Bastek
    Jun 25 '12 at 16:05











  • Unless you have like 10m DAU and call this every launch, then you really cost WhatIsMyIP.com A LOT of money in bandwidth for something that could be a local function. :o

    – Albert Renshaw
    Dec 18 '15 at 6:29



















2














Have a look at the example in my second Answer here.



In a nutshell it uses *http://www.dyndns.org/cgi-bin/check_ip.cg*i to get the extenal I.P






share|improve this answer

































    1














    Check Apple's PortMapper, does exactly what you want.



    As of iOS7 this is irrelevant.






    share|improve this answer


























    • The PortMapper is for "Runtime requirements: Mac OS X 10.5"

      – Black Frog
      Mar 27 '11 at 17:12











    • I am using it for my iPhone app.

      – ssteinberg
      Mar 27 '11 at 17:26











    • Did you have to make any code changes for the iOS device?

      – Black Frog
      Mar 27 '11 at 17:31






    • 1





      There is absolutely nothing to add. PortMapper does the job flawlessly. End of story. Down vote if you like.

      – ssteinberg
      Mar 27 '11 at 17:48






    • 2





      Give´s me null for the public ip Adress on iOS 7

      – davidOhara
      Jan 28 '14 at 14:12











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5450621%2fhow-to-get-the-external-ip-in-objective-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    The easiest way to get your internet ip address from code is to use NSURLConnection.



    For the URL you can use:
    http://www.whatismyip.com/m/mobile.asp
    or
    http://checkip.dyndns.com/



    Just parse the return data and you have your external ip address.






    share|improve this answer



















    • 1





      I like it. Not so much for prod apps but GREAT for debugging. +1

      – Bastek
      Jun 25 '12 at 16:05











    • Unless you have like 10m DAU and call this every launch, then you really cost WhatIsMyIP.com A LOT of money in bandwidth for something that could be a local function. :o

      – Albert Renshaw
      Dec 18 '15 at 6:29
















    4














    The easiest way to get your internet ip address from code is to use NSURLConnection.



    For the URL you can use:
    http://www.whatismyip.com/m/mobile.asp
    or
    http://checkip.dyndns.com/



    Just parse the return data and you have your external ip address.






    share|improve this answer



















    • 1





      I like it. Not so much for prod apps but GREAT for debugging. +1

      – Bastek
      Jun 25 '12 at 16:05











    • Unless you have like 10m DAU and call this every launch, then you really cost WhatIsMyIP.com A LOT of money in bandwidth for something that could be a local function. :o

      – Albert Renshaw
      Dec 18 '15 at 6:29














    4












    4








    4







    The easiest way to get your internet ip address from code is to use NSURLConnection.



    For the URL you can use:
    http://www.whatismyip.com/m/mobile.asp
    or
    http://checkip.dyndns.com/



    Just parse the return data and you have your external ip address.






    share|improve this answer













    The easiest way to get your internet ip address from code is to use NSURLConnection.



    For the URL you can use:
    http://www.whatismyip.com/m/mobile.asp
    or
    http://checkip.dyndns.com/



    Just parse the return data and you have your external ip address.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 27 '11 at 17:11









    Black FrogBlack Frog

    10.1k12256




    10.1k12256








    • 1





      I like it. Not so much for prod apps but GREAT for debugging. +1

      – Bastek
      Jun 25 '12 at 16:05











    • Unless you have like 10m DAU and call this every launch, then you really cost WhatIsMyIP.com A LOT of money in bandwidth for something that could be a local function. :o

      – Albert Renshaw
      Dec 18 '15 at 6:29














    • 1





      I like it. Not so much for prod apps but GREAT for debugging. +1

      – Bastek
      Jun 25 '12 at 16:05











    • Unless you have like 10m DAU and call this every launch, then you really cost WhatIsMyIP.com A LOT of money in bandwidth for something that could be a local function. :o

      – Albert Renshaw
      Dec 18 '15 at 6:29








    1




    1





    I like it. Not so much for prod apps but GREAT for debugging. +1

    – Bastek
    Jun 25 '12 at 16:05





    I like it. Not so much for prod apps but GREAT for debugging. +1

    – Bastek
    Jun 25 '12 at 16:05













    Unless you have like 10m DAU and call this every launch, then you really cost WhatIsMyIP.com A LOT of money in bandwidth for something that could be a local function. :o

    – Albert Renshaw
    Dec 18 '15 at 6:29





    Unless you have like 10m DAU and call this every launch, then you really cost WhatIsMyIP.com A LOT of money in bandwidth for something that could be a local function. :o

    – Albert Renshaw
    Dec 18 '15 at 6:29













    2














    Have a look at the example in my second Answer here.



    In a nutshell it uses *http://www.dyndns.org/cgi-bin/check_ip.cg*i to get the extenal I.P






    share|improve this answer






























      2














      Have a look at the example in my second Answer here.



      In a nutshell it uses *http://www.dyndns.org/cgi-bin/check_ip.cg*i to get the extenal I.P






      share|improve this answer




























        2












        2








        2







        Have a look at the example in my second Answer here.



        In a nutshell it uses *http://www.dyndns.org/cgi-bin/check_ip.cg*i to get the extenal I.P






        share|improve this answer















        Have a look at the example in my second Answer here.



        In a nutshell it uses *http://www.dyndns.org/cgi-bin/check_ip.cg*i to get the extenal I.P







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 10:27









        Community

        11




        11










        answered Mar 27 '11 at 16:48









        markhuntemarkhunte

        5,92621839




        5,92621839























            1














            Check Apple's PortMapper, does exactly what you want.



            As of iOS7 this is irrelevant.






            share|improve this answer


























            • The PortMapper is for "Runtime requirements: Mac OS X 10.5"

              – Black Frog
              Mar 27 '11 at 17:12











            • I am using it for my iPhone app.

              – ssteinberg
              Mar 27 '11 at 17:26











            • Did you have to make any code changes for the iOS device?

              – Black Frog
              Mar 27 '11 at 17:31






            • 1





              There is absolutely nothing to add. PortMapper does the job flawlessly. End of story. Down vote if you like.

              – ssteinberg
              Mar 27 '11 at 17:48






            • 2





              Give´s me null for the public ip Adress on iOS 7

              – davidOhara
              Jan 28 '14 at 14:12
















            1














            Check Apple's PortMapper, does exactly what you want.



            As of iOS7 this is irrelevant.






            share|improve this answer


























            • The PortMapper is for "Runtime requirements: Mac OS X 10.5"

              – Black Frog
              Mar 27 '11 at 17:12











            • I am using it for my iPhone app.

              – ssteinberg
              Mar 27 '11 at 17:26











            • Did you have to make any code changes for the iOS device?

              – Black Frog
              Mar 27 '11 at 17:31






            • 1





              There is absolutely nothing to add. PortMapper does the job flawlessly. End of story. Down vote if you like.

              – ssteinberg
              Mar 27 '11 at 17:48






            • 2





              Give´s me null for the public ip Adress on iOS 7

              – davidOhara
              Jan 28 '14 at 14:12














            1












            1








            1







            Check Apple's PortMapper, does exactly what you want.



            As of iOS7 this is irrelevant.






            share|improve this answer















            Check Apple's PortMapper, does exactly what you want.



            As of iOS7 this is irrelevant.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Dec 17 '14 at 12:57

























            answered Mar 27 '11 at 16:43









            ssteinbergssteinberg

            5,42532858




            5,42532858













            • The PortMapper is for "Runtime requirements: Mac OS X 10.5"

              – Black Frog
              Mar 27 '11 at 17:12











            • I am using it for my iPhone app.

              – ssteinberg
              Mar 27 '11 at 17:26











            • Did you have to make any code changes for the iOS device?

              – Black Frog
              Mar 27 '11 at 17:31






            • 1





              There is absolutely nothing to add. PortMapper does the job flawlessly. End of story. Down vote if you like.

              – ssteinberg
              Mar 27 '11 at 17:48






            • 2





              Give´s me null for the public ip Adress on iOS 7

              – davidOhara
              Jan 28 '14 at 14:12



















            • The PortMapper is for "Runtime requirements: Mac OS X 10.5"

              – Black Frog
              Mar 27 '11 at 17:12











            • I am using it for my iPhone app.

              – ssteinberg
              Mar 27 '11 at 17:26











            • Did you have to make any code changes for the iOS device?

              – Black Frog
              Mar 27 '11 at 17:31






            • 1





              There is absolutely nothing to add. PortMapper does the job flawlessly. End of story. Down vote if you like.

              – ssteinberg
              Mar 27 '11 at 17:48






            • 2





              Give´s me null for the public ip Adress on iOS 7

              – davidOhara
              Jan 28 '14 at 14:12

















            The PortMapper is for "Runtime requirements: Mac OS X 10.5"

            – Black Frog
            Mar 27 '11 at 17:12





            The PortMapper is for "Runtime requirements: Mac OS X 10.5"

            – Black Frog
            Mar 27 '11 at 17:12













            I am using it for my iPhone app.

            – ssteinberg
            Mar 27 '11 at 17:26





            I am using it for my iPhone app.

            – ssteinberg
            Mar 27 '11 at 17:26













            Did you have to make any code changes for the iOS device?

            – Black Frog
            Mar 27 '11 at 17:31





            Did you have to make any code changes for the iOS device?

            – Black Frog
            Mar 27 '11 at 17:31




            1




            1





            There is absolutely nothing to add. PortMapper does the job flawlessly. End of story. Down vote if you like.

            – ssteinberg
            Mar 27 '11 at 17:48





            There is absolutely nothing to add. PortMapper does the job flawlessly. End of story. Down vote if you like.

            – ssteinberg
            Mar 27 '11 at 17:48




            2




            2





            Give´s me null for the public ip Adress on iOS 7

            – davidOhara
            Jan 28 '14 at 14:12





            Give´s me null for the public ip Adress on iOS 7

            – davidOhara
            Jan 28 '14 at 14:12


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f5450621%2fhow-to-get-the-external-ip-in-objective-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks