Page 1 of 1
Forum

Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!

Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.

Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!



Share:
Notifications
Clear all

[Solved] Lazarus - MacOS X - How to detect if network is available?

1 Posts
1 Users
0 Reactions
1,587 Views
 Hans
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter  

I have written a tiny function which detects on a Mac if any network connection is available.
This function relies on the availability of a nameserver (DNS) which under normal circumstances should be available.

Alternatives I have seen so far, either ping Google.com (irrelevant for a network without Internet access) or use specific Reachability functions (Cocoa) functions, which are pretty tricky to implement [in Lazarus Pascal].

In this case we will find out if the Mac has any kind of network connection (WiFi, Ethernet, Bluetooth, 3G, etc).
But it will NOT tell you if there is an Internet connection, it will just detect if there is a network connection.

This function returns TRUE (there is a network available) of FALSE (there is NO network available).

Please add "Process" to your Uses clause.

function FoundNetwork:boolean;
var s:ansistring;
begin
  RunCommand('/usr/sbin/scutil',['--dns'],s);
  FoundNetwork:= pos('nameserver',s)>0;
end;     

   
ReplyQuote
Share: