Howto detect if user has access to Internet in ASOC?

Dear friends,

My project has routines that ask my server for info, and so, if the user does not have Internet, it can not proceed.

So, I need to detect if user has Internet.

I have learned that pinging is restricted in some local networks, so I wrote this which seems to work, but I wonder if Apple tells us the correct way to do it in Cocoa, and if we can access it from ASOC…

Thanks for your comments,


    on test_internet_()
        try
            (**** trying to load my page: ****)
            set test_internet to "curl -A 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (FM Scene 4.6.1)' 'http://www.idanfe.com/you_have_internet.html'"
            
            set has_internet to do shell script test_internet 
            log "has_internet is: " & has_internet
            
            if has_internet contains "you have internet" then
                log "You have Internet connection..."
                return true
                else
                return false
            end if
            on error
            log  "You are not connected to the internet"
            return false
        end try
    end test_internet_

Like you have it working now is the best way. You need only internet when you need something from a server, check the ‘cable’ between your machine and the server only. The rest of the world isn’t important. You could use traceroute as an alternative to check if the traced server is actually your server.

Hi,

this is a solution using SCNetworkReachability API of SystemConfiguration.framework

  1. define an Objective-C class with name Reachability (subclass of NSObject)

.h file


#import <Foundation/Foundation.h>

@interface Reachability : NSObject {}

- (BOOL)hostIsReachable:(NSString *)host;

@end

.m file


#import "Reachability.h"
#import <SystemConfiguration/SystemConfiguration.h>

@implementation Reachability

- (BOOL)hostIsReachable:(NSString *)host
{
	NSParameterAssert(nil != host);

	SCNetworkReachabilityRef	target;
	BOOL                        result;
	SCNetworkConnectionFlags    flags = 0;
	target = SCNetworkReachabilityCreateWithName(NULL, [host UTF8String]);
	result = SCNetworkReachabilityGetFlags(target, &flags);
	CFRelease(target);
	return (result && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired));
}

@end

  1. add SystemConfiguration.framework in Build Phases > Link Binary with Libraries (XCode 4)

  2. in your AppDelegate script add a outlet property


property Reachability : missing value

  1. in MainMenu.xib add an object (blue cube) and name it Reachability (Inspector > Custom Class)

  2. in MainMenu.xib connect the Reachability object with the Reachability property in AppDelegate

then you can use this AppleScriptObjC code to check a host


set isReachable to Reachability's hostIsReachable_("www.google.com") as boolean

I tried to translate Stefan’s code into ASOC as follows:

script InternetTesterAppDelegate
	property parent : class "NSObject"
	property testAddress : "http://www.idanfe.com/you_have_internet.html"
	
	on applicationWillFinishLaunching_(aNotification)
		set theHost to current application's NSString's stringWithString_(testAddress)
		set theTarget to current application's SCNetworkReachabilityCreateWithName(missing value, theHost's UTF8String())
		set {theBool, theResult} to SCNetworkReachabilityGetFlags(theTarget, reference)
               -- do some more stuff with theResult
	end applicationWillFinishLaunching_
	
end script

This didn’t work – I got an error saying that SCNetworkReachabilityCreateWithName was an unknown function. Am I doing something wrong here, or are only foundation functions available from ASOC? I think I’ve tried other non-foundation C-functions that didn’t work either, whereas foundation functions like NSMakeRect do work. I added the SystemConfiguration framework to my project and I’m using Xcode 3 under Lion.

Ric

As far as I can see, only AppKit and Foundation functions are available. It looks like an oversight to me – I’d suggest reporting it to bugreporter.

The functions you’re trying are listed in the framework’s .bridgesupport file, so they should be accessible.

Stefan has given good step-by-step instructions, but there’s a variation to consider. When you make your Objective-C class as per his instructions, instead of making a new instance in MainMenu.xib you can just change the parent of your AS class from “NSObject” to that of the Objective-C class. Then you can call the -hostIsReachable: method as if it is part of your class.

Hi Stefan and all other friends,

Thanks for the kind response.

I followed Stefans instructions except for step 2:

I never did this before and the option “link Binary with Libraries” is greyed out, I am using xCode 4.

If someone can hint me why it is greyed out please tell me.

I have set up the object (blue cube) as usual, and the class and call from my applescript script seems to work fine.

Thanks!

Have a nice weekend!

Have you clicked on the disclosure triangle to its left?

I think I have my friend Shane, check screenshot:

https://files.me.com/fundidor/8prdcp

You have the Project selected, not the Target. When you have the Target selected, click on the Build Phases tab at the top of the editing area.

Still greyed out, check:

https://files.me.com/fundidor/w1oanx

Not the menu item Add Build Phase, click on tab Build Phases

Thanks StefanK and Shane, it seems I got it now:

https://files.me.com/fundidor/z0cxvf

It is confusing, isn’t it?