Как получить уведомления о том, что тип соединения изменился (3G, Edge, Wifi, GPRS) - PullRequest
1 голос
/ 07 октября 2011

Есть ли способ узнать, является ли текущее соединение 3G, Edge или WiFi, и получить уведомление об этом?Или просто узнать, является ли соединение WiFi?

Ответы [ 2 ]

3 голосов
/ 07 октября 2011

Вы должны использовать класс доступности Apple

http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_Reachability_h.html

после реализации вы можете использовать это в делегате:

- (void) configureTextField:  (Reachability*) curReach
{
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    BOOL connectionRequired= [curReach connectionRequired];
    NSString* statusString= @"non";
    switch (netStatus)
    {
        case NotReachable:
        {
            statusString = @"Access Not Available";

            //Minor interface detail- connectionRequired may return yes, even when the host is unreachable.  We cover that up here...
            connectionRequired= NO;  
            break;
        }

        case ReachableViaWWAN:
        {
            statusString = @"Reachable WWAN";


            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cellular Data Detected" message:@"You are using Cellular data such as 3G or Edge. Downloading large amount of data may effect your cellular internet package costs. To avoid such extra cost kindly use Wifi." 
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];


            break;
        }
        case ReachableViaWiFi:
        {
            statusString= @"Reachable WiFi";

            break;
        }
    }
    if(connectionRequired)
    {
        statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];
    }
    NSLog(@"Network= %@",statusString);
    if ([statusString isEqualToString:@"Access Not Available"]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"It seems you are not connected to the internet, the app will try to load from the last cached data - assuming this data exist." 
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    //textField.text= statusString;
}


- (void) updateInterfaceWithReachability: (Reachability*) curReach
{
    if(curReach == hostReach)
    {
        //[self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];
       // NetworkStatus netStatus = [curReach currentReachabilityStatus];
       // BOOL connectionRequired= [curReach connectionRequired];

        [self configureTextField:curReach];

    }   
}
1 голос
/ 07 октября 2011

Да, у Apple есть пример проекта, который показывает это. Достижимость

И вот пример.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...