2

I am developing a MAC OSX Desktop application , in that for checking reachability of particular ip i always use

system("ping -c 3 192.168.10.1")

Is this a good way or any other way is there. I tried apple reachability class but result is not as expected. I also tried using AFNetworking but no good result. With this system call i am getting good result, but i wanted to know the disadvantages in using it or any other best way that works.

Avi
  • 7,469
  • 2
  • 21
  • 22
MacDeveloper
  • 1,334
  • 3
  • 16
  • 49
  • Your question hasn't much to do with **Xcode**. In Xcode IDE you can develop literally anything. It has more to do with what you want from your program, its degree of compatibility, portability, etc. In case you develop in Obj-C, Cocoa, mac-only, then `NSTask` is probably the most elegant and most reliable API. – user3078414 Jul 14 '16 at 10:43

2 Answers2

2

No.

You should look at NSTask if you want to shell out, this provides a full fledge API to allow you to interact with the system.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSTask_Class/

Using the NSTask class, your program can run another program as a subprocess and can monitor that program’s execution. An NSTask object creates a separate executable entity; it differs from NSThread in that it does not share memory space with the process that creates it.

Oliver Atkinson
  • 7,970
  • 32
  • 43
1

Is this a good way

No, it's terrible.

Using system() is expensive, as it results in forking and execing another process, and it provides a very awkward interface as you have to find a way of parsing its output (popen() is better, but still expensive).

Also if you intend to submit your app to the Mac App Store then you will need to sandbox it and running ping might not be possible (not sure).

See this Apple Guide for the right way to do it. If you have issues with using the Reachability API then you should start a new question to address those issues.

Droppy
  • 9,691
  • 1
  • 20
  • 27
  • http://stackoverflow.com/questions/38306947/managerfordomain-is-always-returning-not-reachable. http://stackoverflow.com/questions/37742367/reachabilitywithaddress-is-not-working-in-objective-c-programming. None of these helped me to resolve my reachability issue. I am working on this since long time. Atlast i added ping in my code. – MacDeveloper Jul 14 '16 at 10:34
  • @syammala And what does `ping` tell you, given you aren't even parsing what it outputs? – Droppy Jul 14 '16 at 10:37
  • ret = system(command); ret = WEXITSTATUS(ret); From this ret value i am checking whether i am able to ping to that ip or not – MacDeveloper Jul 14 '16 at 10:46
  • But my question is that is there any better way or is this ok.. As you said its expensive should i look for more better way?? – MacDeveloper Jul 14 '16 at 10:48
  • 1
    I stand by my answer. – Droppy Jul 14 '16 at 10:48