Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 12-31-2009, 07:50 PM   #1 (permalink)
Anyone for iPad Dev?
 
Join Date: Sep 2009
Location: Take a Guess
Posts: 74
fondu-Dev is on a distinguished road
Default Detect WiFi Connection

I try the following code to detect wifi. I Also imported the Reachabillity.h file but I gives me two errors and one warning.

Code:
[[Reachability sharedReachability] setHostName:@"www.drobnik.com"]; //Warning: 'Reachabillity' may not respond to '+sharedReachabillity'
	
	NetworkStatus internetStatus = [[Reachability sharedReachability] remoteHostStatus]; //Incompatible Types
	
	if ((internetStatus != ReachableViaWiFiNetwork)) //ReachableViaWifiNetwork undeclared
	{
		UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"You require an internet connection via WiFi" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
		[myAlert show];
		[myAlert release];
	}
errors are in comments
fondu-Dev is offline   Reply With Quote
Old 01-01-2010, 04:35 AM   #2 (permalink)
Persian Developer
 
Join Date: Mar 2009
Posts: 274
Momeks is on a distinguished road
Default

here is correct code and works fine

YourAppDelegate.h
Code:
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <netdb.h>
#include <SystemConfiguration/SCNetworkReachability.h>


-(BOOL)connectedToNetwork;

YourAppDelegate.m

Code:
- (BOOL) connectedToNetwork
{
	// Create zero addy
	struct sockaddr_in zeroAddress;
	bzero(&zeroAddress, sizeof(zeroAddress));
	zeroAddress.sin_len = sizeof(zeroAddress);
	zeroAddress.sin_family = AF_INET;
	
	// Recover reachability flags
	SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
	SCNetworkReachabilityFlags flags;
	
	BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
	CFRelease(defaultRouteReachability);
	
	if (!didRetrieveFlags)
	{
		printf("Error. Could not recover network reachability flags\n");
		return NO;
	}
	
	BOOL isReachable = ((flags & kSCNetworkFlagsReachable) != 0);
	BOOL needsConnection = ((flags & kSCNetworkFlagsConnectionRequired) != 0);
	return (isReachable && !needsConnection) ? YES : NO;
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {	
	
	// Override point for customization after app launch	
    [window addSubview:viewController.view];
	[window makeKeyAndVisible];
	[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
	if (![self connectedToNetwork]) {
		UIAlertView *alert = [[UIAlertView alloc]
							  initWithTitle:@"Network Connection Error"
							  message:@"You need to be connected to the internet."
							  delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
		[alert show];
		[alert release];
	}

}

import SystemConfiguration.framework on your project
__________________
My Applications On the App Store



Last edited by Momeks; 01-01-2010 at 04:41 AM.
Momeks is offline   Reply With Quote
Old 09-14-2010, 11:28 AM   #3 (permalink)
www.Alexj17.com
 
Alexj17's Avatar
 
Join Date: May 2009
Location: Liverpool, England
Posts: 181
Alexj17 is on a distinguished road
Send a message via Skype™ to Alexj17
Default

hey, sorry to bring up an old thread,

did this help you? i have the same errors, i have added the code in the second post but i have got the exact same errors still.

Alex
Alexj17 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 341
16 members and 325 guests
akacaj, alexP, ClerurcifeDer, Domele, Duncan C, givensur, GraffitiCircus, guusleijsten, JmayLive, michelle, NetGuru, NSString, Paul Slocum, Punkjumper, Sloshmonster, soohyun
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,114
Posts: 402,883
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:50 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0