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 01-28-2012, 01:54 AM   #1 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default http://google.com NOT working for Internet Connection Detection

My new app just released today and it appears that google.com is no longer working for my Internet Connection detection:

Code:
            //check for Internet Connection
            NSURL *url = [[NSURL alloc] initWithString:@"http://google.com"];
            NSError *error = nil;
            NSStringEncoding encoding;
            
            NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
                                                             usedEncoding:&encoding 
                                   
                                                                    error:&error];
            
            //if no connection
            
            if ([my_string length] == 0) {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"A connection to the Internet is required to access this page." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                
                [alertView show];
                
            }
Is anyone else experiencing this issue? All other URLs still work.
__________________
Please visit - MushroomPudding.com
marshusensei is offline   Reply With Quote
Old 01-28-2012, 01:57 AM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

That isn't a good way to check for an internet connection. I think one reason is if the server hangs, your app freezes. Here is the advised way: objective c - How to check for an active Internet Connection on iPhone SDK? - Stack Overflow
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 01-28-2012, 02:03 AM   #3 (permalink)
Registered Member
 
marshusensei's Avatar
 
Join Date: Mar 2011
Location: Australia
Age: 28
Posts: 105
marshusensei is on a distinguished road
Default :)

Thanks Domele. I was ignorant to this. I'll switch to reachability in the next update.
__________________
Please visit - MushroomPudding.com
marshusensei is offline   Reply With Quote
Old 01-28-2012, 04:34 AM   #4 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default

Reachability is not fool proof either btw.
This may have changed with late revisions of reachability but last time I used it it was only able to detect if you had a network connection, not if you had internet.

Say you're at a hotel, you connect to the hotel wifi - you are properly connected to the network but whatever url you type in you are directed to a landing page for you to type in your room number.

This type of connection will show up as perfectly OK in reachability and your app goes off to do whatever but will fail (depending on how you handle it...)

Same thing if you are on a network with 'pre-pay' - the user could be out of credit. The phone is perfectly connected to 3g but can't get any data through. Reachability will however report that the connection is OK.


What you probably want to do is first check reachability - if you have a OK there you can try to download a very small file from your own server. If that works then you can assume connectivity for a small bit of time.

Testing google.com probably doesn't make any sense if you want to connect to a service you have running on your own server. You should test the actual server you are trying to use.

The other thing is that connectivity can come and go. The user could be in a car and have very good service at one point but a few minutes later have no service. So make sure you properly implement the reachability notifications.
sneaky is offline   Reply With Quote
Old 01-28-2012, 07:06 AM   #5 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 75
ckgni is on a distinguished road
Default

Reachability had lots of bugs for me.

The most trustworthy method I use in my apps and being accepted by Apple, is adding in my viewcontroler .m files this:

Code:
-(void)webView:(UIWebView *)webview didFailLoadWithError:(NSError *)error {
    UIAlertView *erroralert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                         message:@"No Internet connection. This application requires an Internet connection to function properly."
                                                        delegate:nil 
                                               cancelButtonTitle:@"Close" 
                                               otherButtonTitles: nil];
    [erroralert show];
    
}
This displays a popup alert with a message that there is no active Internet connection.

You can add this code in your viewcontroller m files, just after all the #import and @synthesize commands.

Replace "webview" with the name of your UIWebView.

Last edited by ckgni; 01-28-2012 at 07:12 AM.
ckgni is offline   Reply With Quote
Old 01-28-2012, 12:25 PM   #6 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Quote:
Originally Posted by sneaky View Post
Reachability is not fool proof either btw.
This may have changed with late revisions of reachability but last time I used it it was only able to detect if you had a network connection, not if you had internet.
Yes but Reachability has methods for checking connection to a particular host name or IP address, not just network connection in general.
baja_yu is offline   Reply With Quote
Old 01-28-2012, 02:56 PM   #7 (permalink)
Token farm animal
 
sneaky's Avatar
 
Join Date: Apr 2011
Posts: 321
sneaky is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Yes but Reachability has methods for checking connection to a particular host name or IP address, not just network connection in general.
I stand corrected:
Code:
//reachabilityWithHostName- Use to check the reachability of a particular host name. 

+ (Reachability*) reachabilityWithHostName: (NSString*) hostName;

 

//reachabilityWithAddress- Use to check the reachability of a particular IP address. 

+ (Reachability*) reachabilityWithAddress: (const struct sockaddr_in*) hostAddress;

 

//reachabilityForInternetConnection- checks whether the default route is available.  

//  Should be used by applications that do not connect to a particular host

+ (Reachability*) reachabilityForInternetConnection;
Loading…
sneaky 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: 407
11 members and 396 guests
Atatator, condor304, FrankWeller, imac74, MAMN84, mraalex, n00b, PowerGoofy, QuantumDoja, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,123
Posts: 402,908
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

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