UPDATE (April 13th, 2015): I’ve added a separate download for a 64 bit version of libReachability.a. This file is untested as I do not have regular access to a 64 bit iOS device.
UPDATE: The project has been updated to include retrieving the current SSID (that the device is connected to). For Android, this means adding the Access Wifi State permission. Refer to the updated text in the article.
It’s a question that has been asked a few times in the Embarcadero forums: how to check if a mobile device (either Android or iOS) has an internet connection.
I already knew of a solution for iOS, and recently I came across one for Android, so I figured I would combine the two and create a platform-independent unit, called NetworkState. In that unit, I created an abstract class that could be derived from in other units that would provide the implementation for each platform, i.e. Android and iOS. The declaration for the abstract class TCustomNetworkState looks like this:
function GetSSID: String; virtual; abstract;
function IsConnected: Boolean; virtual; abstract;
function IsWifiConnected: Boolean; virtual; abstract;
function IsMobileConnected: Boolean; virtual; abstract;
end;[/sourcecode]
As can be seen, the intention is to simply provide functions for checking if either there is a connection at all, and specifically whether there is either a Wifi or Mobile (Cellular data) connection, and what the SSID is. It was also the intention that the implementation use the supported methods for checking for connections, rather than say, using the Indy components.
The units NetworkState.iOS and NetworkState.Android each declare TPlatformNetworkState that provide the actual implementation, and the implementation uses clause of NetworkState is appropriately IFDEF’d for each platform so that the actual instance that is created is the one for the specific platform being compiled for.
I’ve included a demo project below that includes all the necessary files. If you’re going to use TNetworkState in your own project, make sure the files:
libReachability.a
NetworkState.pas
NetworkState.iOS.pas
NetworkState.Android.pas
SCNetworkReachability.pas
CaptiveNetwork.pas
are either in your project directory or in the compiler search path. Bear in mind that for the Android platform you’ll need to include the Access Network State, and Access Wifi State permissions in the project options:
For iOS on the device, you’ll need to add the SystemConfiguration framework in the SDK manager. Please refer to this article, and instead of CoreBluetooth (as per the 2nd image), use SystemConfiguration.
Excellent, I was looking for it.
Thank you.
I have downloaded your example. When I try to compile it for IOS Device, I get this error
[DCC Error] E2597 ld: file not found: /System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration
Thank you.
My apologies; I had meant to include the instructions for adding SystemConfiguration to the SDK manager. I’ve updated the article with additional instructions at the end.
Did exactly what is in that article, but still have the same error. I can see the SystemConfiguration framework in the list along with other frameworks. I was also trying to play with libSystemConfiguration path in SCNetworkReachability.pas, but it did not help.
Your help is very appreciated. You contact me by email, I can provide a screenshot.
I realized, that I missed the last step in adding the framework. I forgot to click Update Local File Cache button. It works now. Thank you for your help.
LINK ERROR when compiling for iOS
======================
I also already has a solution for iOS in XCode (reachability) but I just bought XE5 and thought let’s try it cross-platform 🙂
I dowloaded your code and ran it on an android device – works like a charm!
I selected my iPhone and ran again – link error
[DCC Error] E2597 ld: file not found: /System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration
Maybe there is something I’m not quite getting, but this file does exist on my iMac and is an alias for:
/System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration
Delphi is running on another iMac running Windows 7 which connects to the other iMac running paserver
(I am able to compile and run other programs on the iPhone with this setup)
Any idea how to fix this?
Never mind, I just refreshed the page I left open last night 🙂 and found the answer!
It works now, thank you very much for such a fast response!
Hi, thanks you very much !
But I have a problem to compil it 🙁
[DCC Fatal Error] CaptiveNetwork.pas(6): F1026 Fichier introuvable : ‘C:\Users\Retwas\Documents\RAD Studio\Projets\whisky\Macapi.CoreFoundation.dcu’ (la portée d’unité “Macapi” indique seulement OSX32)
What’s the problem ? Thanks
Nice code nut it helps only to know you are connected to a net. it can be also a intranet without internet connection or what ever. so i prefer this simple code.
function TdmMain.CheckInternet: boolean;
begin
result:=false;
try
IdTCPClient1.ReadTimeout:=2000;
IdTCPClient1.ConnectTimeout:=2000;
IdTCPClient1.Port:=80;
IdTCPClient1.Host:=’google.de’;
IdTCPClient1.Connect;
IdTCPClient1.Disconnect;
result:=true;
except
result:=false;
end;
end;
here you will see a comment why you don’t want use that code:
http://www.fmxexpress.com/must-have-check-network-connectivity-state-library-for-delphi-xe8-firemonkey-on-android-and-ios/
so be aware and use asych connection in separate thread. An example of would be great! any?
Daniel is referring to his example; not mine. Calling the network state check in iOS returns immediately, so shouldn’t cause any issues.
Daniel’s method actually attempts to make a network connection, so it should be contained in a thread to prevent the iOS app from closing if the call takes too long. I’ll write an article on how to achieve this and post it soon.
Does this code not work for the iOS simulator ? Error reads : ‘ Objective C class Reachability can not be found’ . This is straight from the demo project
I have the exact same issue (“Objective C class Reachability can not be found”) when running on iOS simulator AND on iPhone . Could it be a problem with the iOS version? I am using iOS7, XE5 Update 2.
Thanks so much that was really usefull for us. I would love to discuss with you some business opportunity.
I’ve sent you an email.
With XE5 Update 2 and iPhoneOS 7.0.3 SDK I get this error (worked fine with XE5 Update 1):
[DCC Hint] H2596 ld: warning: unknown dwarf DW_FORM_strp (offset=0xFFFFCF2C) is too big in libReachability.a(Reachability.o)
Any ideas? I might need to update my local file cache for the iOS SDK.
That’s a hint; not an error. Have you tried running it? It works fine for me.
If you’re unable to run it due to a compiler error, try right-clicking the project in the project manager, click “Clean”, then right-click and “Build”. You may still receive the hint, but it should run OK.
with Delphi 10.2.2 Tokyo I get same error, but now is a error, not a hint, Build project is interrupted.
Any ideas?
Hi.
I get same trouble with Delphi 10.2.3 Tokyo.
Any idea how to fix this? Thanks.
Can you provide the complete output of the error, please?
iOS 32:
[DCC Error] E2597 Id: warning: unknow dwarf DW_FORM_strp (offset=0xFFFFCF2C) is too big in .\units\iOS\iOSDevice32\libReachability.a(Reachability.o)
[DCC Fatal Error] F2588 Linker error code: 1 ($00000001)
Failed
iOS 64:
[DCC Error] E2597 Id: warning: unknow dwarf DW_FORM_strp (offset=0xFFFFC7E7) is too big in .\units\iOS\iOSDevice64\libReachability.a(Reachability.o)
[DCC Fatal Error] F2588 Linker error code: 1 ($00000001)
Failed
I got same error here. Did you find a solution?
I’m not receiving the error, using Delphi Tokyo 10.2.3 and Xcode 9.2, so I’m unsure as to what the problem is
I did get it running thanks. It was some other linker problem with the project file after installing Update 2 that was keeping it from running. A new project file took care of the issue.
[…] Head over and download the class and demo for NetworkState. […]
Is there any way to implement NFC?
Might be better off using BLE:
http://blogs.computerworld.com/wireless-networking/22961/apple-ios-7-ibeacon-wearables-and-ble-leave-little-future-nfc
If I have time, I could look into both
Hm, I do not agree with Jonny Evans that nfc is dead. I tried to do nfc detection and if is it enabled. I would send you my code for you to view it. I do not know is code ok. Do you have e-mail address?
You may contact me using the form on my “About” page
[…] http://delphi.radsoft.com.au/2013/11/checking-for-an-internet-connection-on-mobile-devices-with-delp… […]
[…] http://delphi.radsoft.com.au/2013/11/checking-for-an-internet-connection-on-mobile-devices-with-delp… […]
I’m using Delphi XE5 and the network API works fine when running on android, but just crashes on iOS 5.1.1. The application just shuts down when trying to retrieve info. Any idea why or how to troubleshoot?
Thanks!
Have you run it via the debugger to see where the crash occurs?
I’m yet to test it with iOS 5.1.1. I may have time to do so sometime this week.
I wonder if it is to enable or disable WIFI and not only check the status.
Thank you.
As it stands, just to check the status. I can look into enabling/disabling the wi-fi
I test your demo, andriod is OK, but ios is not OK. the errmessage is [DCC Error] E2597 ld: file not found: /System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration
I have do step by step as http://delphi.radsoft.com.au/2013/10/adding-other-ios-frameworks-to-the-sdk-manager/
when i update local file Cache, the error message is Directory does not exist: /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.1.sdk/usr/include/
I use delphi XE7 and Xcode 6 for IOS 7.1
I do’t know how to do now ? thank you ! I am from china,
I am poor in english. I hope your Email reply.
I’m about to start checking out XE7, so I’ll revisit this article, thanks!
[…] Heute, 17:18 Sowas wie das hier? http://delphi.radsoft.com.au/2013/11…th-delphi-xe5/ Kaum macht man's richtig – schon funktioniert's Nachdenkliche Menschen denken oft warum sie […]
While running this code in XE7 it is giving access violation error. Can you please look into this ?
It would help to have more information, such as which platform you are targeting (i.e. iOS or Android), and exactly where the error occurs.
Some devices (as LG G Pad 7, for example) show the Access Viotation Exception.
I don’t know why, but if you use IsWifiConnected or IsMobileConnected in those devices, the exception is raised (IsConnected don’t raise the exception).
If you could explain why is happing this, and the solution, i would thank you very much.
Cheers 🙂
I don’t have an LG G Pad 7, so I’m unable to check; sorry.
Hi
I have XE7 and also have the same issue as noted about the missing directory when trying to get the bluetooth api
I seem to recall something along the lines that bluetooth support is better in xe7 anyway though so maybe does not sepecificly need the api?
(but I may be talking through a hole in my head too here)
Thanks!
If you’re referring to the reference to adding a new framework in the SDK manager, note that I specifically mention to replace “CoreBluetooth” with “SystemFramework”
Hi
I still get error about missing folder with updating local file cache in SDK manager if I set to use SystemFramework instead of CoreBluetooth
(and so means get compile error with XE7 with the networkstate.pas about file not found systemconfiguration etc
You have XE7 now, have got it to work?
thanks!
What’s the missing folder error message? Please provide the *exact* message.
Yes, I have it compiling in XE7, however my iPhone isn’t working so I’m unable to test it. As far as I can recall, it doesn’t work in the simulator because the Reachability class is not included in the framework in the simulator (though as I mentioned, it will still compile)
copy /paste does not seem to work
but its the same missing folder (on the Mac side) error for when doing the local file update
that others have already mentioned
what xcode version and iOS version have got it to work with?
I have Xcode 6.0.1. This release has iOS 7.1 SDK for the simulator only, which is what I was targetting when I compiled. It has iOS 8.0 SDK for the simulator and device, however there’s changes that need to be made in order for it to compile. I’m starting a new article to cover this, so keep any eye out for it.
Hi
Ok.thanks for help so far 🙂
Just to note, I am targetting Apple store (i.e device), as using Macincloud
Hello, I can compile in Delphi XE7, but when the app runs on iOS Simulator with 8.1 I get an error ObjectiveC reachability class could not be found
It doesn’t work with the simulator; only with the device
Thanks
Bom dia, onde posso fazer o download do exemplo?
thank you!
Hello, where can I download the example?
thank you!
My download manager was broken. It’s back again now 🙂
Mi gestor de descargas se rompió. Es volver de nuevo ahora 🙂
I’d like to see your demo. Where is its link to download?
My download manager was broken. It’s back again now 🙂
Where can I get the demo program(source code)? I can’t find the link to it.
My download manager was broken. It’s back again now 🙂
How I can download this sample? Can’t found any download link 🙁
My download manager was broken. It’s back again now 🙂
Thanks for the link,
About Delphi XE7 support: should be added to the uses list this unit – Androidapi.Helpers, SharedActivityContext is moved from FMX.Helpers.Android to this unit for XE7
getIpAddress Why Int?
cast 127.0.0.1 string?
GetIPAddress is declared as an integer in the Java interface. There’s some information here, about how to format it as a string in Java:
http://stackoverflow.com/questions/16730711/get-my-wifi-ip-address-android
(The answer by Digital Rounin)
It would be possible to do something similar in Delphi.
hey I am using XE7.
and I use your code to check network state,WiFi state in android.
It work Nice.
But How I check Whether Internet is available or Not while connected to WiFi in Android.
Hi, i tested the code XE8 IOS32 bit and is working, but on IOS64 bit i get an error on libReachability.a, i guess that we need the 64 bit version of this library, do you have any idea of how to get it?
Thank you
You’ll need to build a new libReachability.a in Xcode. I’m yet to do this myself, however this page may help for now:
http://code.tutsplus.com/tutorials/ios-sdk-detecting-network-changes-with-reachability–mobile-18299
I may be able to work it out over the next couple of days or so.
I’ve now added a link to download a 64 bit version of libReachability.a (see the article contents). I’m yet to be able to test it though, as I don’t have regular access to a 64 bit iOS device.
You’re great! Was just looking for a 64-bit solution for iOS 32/64bit deployment
Thanks, Peter. I assume it works, then? I’m yet to test it myself.
I tested this on an Android – compiled with XE7 once Androidapi.Helpers had been added to the Networkstate.Android.pas to obviate the SharedActivityContext error), and it works beautifully!
[…] Head over and download the network state component library for checking the internet connection in D… […]
Hi
I was able to add the framework using the new way in XE8 now
great
and it compiled OK
re the 64 bit libReachability.a file
one problem is, there is no option to add 64bit iOS in the compiler for some reason with the demo test project?
Make sure you add the iOS Device – 64 bit SDK in the SDK Manager (Tools|Options|Environment Options\SDK Manager)
Hi
thats already there
you have to create a new project (then copy across the source code/objects) in XE8 so as to be able to have 64 bit iOS as a target listed
(unless there is some other work around)
Weird thing is: I didn’t have to create a new project. I just re-opened in XE8. Mind you, I may have opened it in XE6 and XE7 previously 🙂
I am thinking that I would need to start a new project so as XE8 can then add in 64 bit option , come to think of it
[…] NetworkState(一个检查网络连接状态的组件) […]
Hello,
How I know wheather connection is intranet or internet in wifi state..?
Please Help..
Works like a charm. The tip on adding SystemConfiguration Framework was great too! Thanks.
64 bit version of libReachability is causing a compile time error on XE8 Update 1. The 32 bit version works OK. The error message is:
[DCC Error] E2597 NYI lto::isObjectFile
NYI lto::archName
ld: file was built for archive which is not the architecture being linked (arm64): libReachability.a for architecture arm64
It did a lipo -info on the 64 bit libReachability.a, and the result (“Architectures in the fat file: libReachability.a are: armv7 arm64”) doesn’t indicate anything abnormal.
It should be noted that it works OK for another team member who is using XE8 without the Update 1. I wonder if the error I am getting is somehow related to the Update 1. Any thoughts on how to fix it?
Please disregard my last comment which was about getting a compile time error E2597 in XE8 update 1 when choosing iOS 64 as target. It was traced back to having a 32 bit file lying around in the project directory. Thx.
[…] Checking for an internet connection on mobile devices with Delphi XE5 […]
On Delphi XE8 upd1, I get this error when compiling for Android:
[DCC Error] NetworkState.Android.pas(115): E2003 Undeclared identifier: ‘SharedActivityContext’
and then:
[DCC Error] NetworkState.Android.pas(148): E2003 Undeclared identifier: ‘JStringToString’
Using: Android SDK 24.0.2 32-bit
Is there something that has changed in the Android SDK and must be changed in this source code?
Sorry, should have searched around first. With XE7 and later change FMX.Helpers.Android to Androidapi.Helpers;
See: http://stackoverflow.com/questions/25981458/in-which-uses-can-i-find-sharedactivitycontext
Now it run successfully on my Android.
Do you already implemented or know who did the MacOS Reachability wrapper to test Internet Connection on Mac OS-X?
Cheers,
KM
Is it possible to get event when wifi interface is enabled or disabled.
Good night, Dave.
These classes work for detection but have a fault.
It happened here with me now that I have the local network running but my internet is not reaching the router.
It detects that I am connected to the Router but can not see that I am out of the internet.
Take a test there to stay without IP WAN (no internet coming but normal working network) and see what I’m saying …
Good job in the classes.
Best regards,
Guilherme Augusto
I suspected that might be an issue, given what else I have read, and from other comments.
I’m planning to revisit this issue, and others, very soon.
It’s all right.
The intention is precisely to try to improve the great solution that has created.
Thank you for your attention and response.
Have great parties and the end of the year.
Best Regards,
Guilherme Augusto
How can I enable javascript in TWebView (WebView) for Delphi Berlin?
So I am trying this solution with Tokyo.1 and keep getting the same error. It compiles just fine, but when I try to run it on my iPad (5th Gen), or when I try to deploy it I keep getting “Required local file ‘Pingtest’ (I renamed the project, no other alterations) not found. Deployment failed.” Other projects of mine work just fine.
There’s at least a couple of reasons for that error – the first and most obvious is that you may be attempting to deploy without actually compiling. Another reason is that apparently using a network mapping on some VM’s causes this problem (does not for me using VirtualBox). Sometimes it’s caused by a problem with the project file. You might like to recreate the project, add your units back in, and see how that goes.
My development environment: Delphi Tokyo 10.2.3, running on Windows 10 (x64). XCode 8.2.1 running on Mac El Capitan 10.11.5.
When building an iOS app in Delphi, I get following compile time error:
[DCC Error] E2597 ld: warning: unknown dwarf DW_FORM_strp (offset=0xFFFFCA7A) is too big in libReachability.a(Reachability.o) ld: warning: unknown dwarf DW_FORM_strp (offset=0xFFFFCAC6) is too big in libReachability.a(Reachability.o)
Any thoughts on how to fix this error?
Thanks!
Looks like a few are having this problem. I’ll look into it within the next day
I also have this problem Delphi Tokyo 10.2.3, Xcode 9.4, Mac OS High Sierra 10.13.4
DCC Error] E2597 ld: warning: unknown dwarf DW_FORM_strp (offset=0xFFFFC7E7) is too big in libReachability.a(Reachability.o)
ld: warning: unknown dwarf DW_FORM_strp (offset=0xFFFFC833) is too big in libReachability.a(Reachability.o)
Undefined symbols for architecture arm64:
Error: “_OBJC_CLASS_$_NSObject”, referenced from: _OBJC_CLASS_$_Reachability in libReachability.a(Reachability.o);
Error: “_OBJC_CLASS_$_NSBundle”, referenced from: objc-class-ref in libReachability.a(Reachability.o);
Error: “_OBJC_METACLASS_$_NSObject”, referenced from: _OBJC_METACLASS_$_Reachability in libReachability.a(Reachability.o);
Error: “_OBJC_CLASS_$_NSNotificationCenter”, referenced from: objc-class-ref in libReachability.a(Reachability.o);
Error: “_OBJC_CLASS_$_NSString”, referenced from: objc-class-ref in libReachability.a(Reachability.o);
ld: symbol(s) not found for architecture arm64
I’m working on an alternative to this solution; one that does not require the libReachability library. I need to resolve an issue with the API calls first
That sounds great thank you
Thanks for library supporting!
Hello, you still can not find a solution to the bug in iOS? Your code is great for Android but I would love to make it work on iOS. Thanks for your support.
I’ve now posted an article which provides an alternative solution; one that does not require the libReachability library:
http://delphiworlds.com/2018/07/connectivity-checker-for-mobile-revisited/
Excellent, thank you, added in and working
Hello, That link gives 404 error. Google says it should be http://delphiworlds.com/2018/07/connectivity-checker-for-mobile-revisited/
Fixed the link; thanks!
Well in delphi 10.4 you getting access violation after button1 click.
There’s an updated demo here, that should work with Delphi 10.4
In Delphi 11.x
Download do Kastri > https://github.com/DelphiWorlds/Kastri
Open the delphi > tools > options > Language > Delphi > library > library path:
– add path Kastri-master\API
– add path Kastri-master\Controls
– add path Kastri-master\Core
– add path Kastri-master\Features\Connectivity
– add path Kastri-master\Include
Create new project
Add uses DW.Connectivity;
if TConnectivity.IsConnectedToInternet then
begin
Showmessage(‘Device is connected to the internet’);
if TConnectivity.IsWifiInternetConnection then
Showmessage(‘via Wifi’);
end
else
Showmessage(‘Device is NOT connected to the internet’);
Is there an issue? If so, please report to: https://github.com/DelphiWorlds/Kastri/issues
Hi,
I tried to follow these clear instructions, but I kept having a linker error. The solution was to add SystemConfiguration framework to Delphi SDK Manager.
Go to Tools, Options, Deployment, SDK Manager, scroll to Frameworks group, click on any of them (example AddressBook) then click the ‘Add a new path item’ – top button on the right. Use ‘$(SDKROOT)/System/Library/Frameworks’ as path on remote machine and ‘SystemConfiguration’ as framework name. Make sure Path Type is Framework (should be, by default, because AddressBook was selected). Save everything. Now it builds.
You might be interested to know that there is a much more up-to-date implementation in this demo:
https://github.com/DelphiWorlds/Kastri/tree/master/Demos/Connectivity
I’m sorry, seems I didn’t read the full instructions – they were there already. And I forgot to mention that clicking ‘Update local file cache’ is mandatory. Apologies.