簡體   English   中英

Internet連接(可恢復性)不起作用

[英]Internet connection (Rechability) not work

我將以下代碼用於互聯網連接,但無法正常工作(wifi或互聯網更改時,reachabilityChanged不會調用)

//Internet.m : 
...
@interface Internet ()
@property (nonatomic) Reachability *Wifi;
@end

@implementation Internet

-(void)checkNetwork
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
    self.Wifi = [Reachability reachabilityForLocalWiFi];
    [self.Wifi startNotifier];
}

- (void) reachabilityChanged:(NSNotification *)note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
    if (curReach == self.Wifi)
    {
        NetworkStatus netStatus = [curReach currentReachabilityStatus];
        if(netStatus == NotReachable)
        {
            NSString *status=@"NO";
        }//if
        else if(netStatus == ReachableViaWiFi)
        {
            NSString *status=@"Yes";
        }//else
    }//if
}//reachabilityChanged

//ViewController.m
- (void)viewDidLoad
{
    [super viewDidLoad];

    [self performSelectorInBackground:@selector(callInternet) withObject:nil];
}//load

-(void)callInternet
{
    inter=[[Internet alloc]init];//inter is instance variable
    [inter checkNetwork];
}//callInternet

@sajidZare,

Appdelegate.h文件中

//Check Internet Connection
.//Define method for that
-(BOOL)checkInternetConnection;

Appdelegate.m文件中

 //Check Internet Plugin
    #include<unistd.h>
    #include<netdb.h>

    #pragma mark Check Internet connection
    -(BOOL)checkInternetConnection {

        char *hostname;
        struct hostent *hostinfo;
        hostname = "google.com";
        hostinfo = gethostbyname (hostname);
        if (hostinfo == NULL)
        {
            NSLog(@"-> no connection!\n");
            return NO;
        }
        else{
            NSLog(@"-> connection established!\n");
            return YES;
        }

    }

//這是最好的解決方案,請嘗試一下,可能會對您有所幫助。

your.h文件中的 另一個解決方案

    #import "Reachability.h"            // internet Rechability
    //---rechability methods---//
    @property (nonatomic) Reachability *internetReachability;
-(void)checkInternet;

在your.m文件中

-(void)checkInternet
{
    self.internetReachability = [Reachability reachabilityForInternetConnection];
    if (self.internetReachability.currentReachabilityStatus==NotReachable)
    {
        NSLog(@"internet is not available..");
        [self internetAlertView];
    }


    else
        {
            NSLog(@"internet Available..");
*//Here you can do whatever you want to do//*
            [self BTN_Spark:self];
        }
        [self.internetReachability startNotifier];
    }


    -(void)internetAlertView
    {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Please check the internet connection!!!" delegate:self cancelButtonTitle:@"OK"  otherButtonTitles:nil];
        [alertView show];
    }

暫無
暫無

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

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