With the help of Shardik, I think, I could solve this riddle.
If you run this example on Mac OS X, please swith off WiFi or remove the network-cable.
The debugger should show you the current state.
As it seems, Apple defines being online as NOT BEING OFFLINE. If I got it right
Please have a test. Please credit Shardik and me, if you use the snippet in your Apps.
EDIT: Added CFRelease(target) - otherwise, there would be some memory leak. Thanks again, Shardik!
Code: Select all
ImportC "/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration"
SCNetworkCheckReachabilityByName(target.i,status.i)
SCNetworkReachabilitySetCallback(target.i,callback.i,context.i)
SCNetworkReachabilityCreateWithName(nix.i,target.i)
SCNetworkReachabilityGetFlags(target.i,flags.i)
SCNetworkReachabilityScheduleWithRunLoop(target.i,runLoop.i,runLoopMode.i)
CFRunLoopGetCurrent()
CFRunLoopStop(loopid.i)
CFRelease(target.i)
EndImport
Structure SCNetworkReachabilityContext
version.i
info.i
retain.i
release.i
copyDescription.i
EndStructure
#NetWork_No_Server = 0
#NetWork_Is_Online = 2
#NetWork_Is_Offline = 7
Procedure Check_Connectivity(URL.s)
Protected status.i = 0
Protected result.i = 0
result = SCNetworkCheckReachabilityByName(@url,@status)
If result
ProcedureReturn status
EndIf
EndProcedure
ProcedureCDLL Check_Connectivity_Callback(target.i,flags.i,info.i)
; you probably will compare the target here with the Target-ID you got earlier
; This way, you can determine, if a certain server is online
If Flags = #NetWork_No_Server
Debug "No Network-Connection at all."
ElseIf Flags = #NetWork_Is_Online
Debug "We are online."
ElseIf Flags = #Network_Is_Offline
Debug "We are offline"
EndIf
EndProcedure
context.SCNetworkReachabilityContext
With context
\version = 0
\info = @"www.apple.com"
\retain = #Null
\release = #Null
\copyDescription = #Null
EndWith
; First get the Target-ID
target.i = 0
target = SCNetworkReachabilityCreateWithName(#Null,@"www.apple.com")
If target = 0
Debug "Could not reach www.apple.com"
EndIf
; Now se the callback
If SCNetworkReachabilitySetCallback(target,@Check_Connectivity_Callback(),context) = 0
Debug "Could not set Callback"
EndIf
; Find the Loop from Mac OS X
temp.i = CFRunLoopGetCurrent()
; Add outself to the Loop
If SCNetworkReachabilityScheduleWithRunLoop(target,temp,CFStringCreateWithCString_(0, "kCFRunLoopDefaultMode", 0)) = 0
Debug "Scheduling failed!"
EndIf
OpenWindow(0,200,200,200,200,"Test")
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
temp = CFRunLoopGetCurrent()
CFRunLoopStop(temp)
CFRelease(target)