簡體   English   中英

如何接收連接已更改類型的通知(3G,Edge,Wifi,GPRS)

[英]How do I receive notifications that the connection has changed type (3G, Edge, Wifi, GPRS)

有沒有辦法發現當前連接是3G,Edge還是WiFi,並收到通知? 或者只是一種方法來發現連接是否是WiFi?

您應該使用Apple的Reachability類

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];

    }   
}

是的,Apple有一個示例項目可以證明這一點。 可達性

這是一個例子。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM