【问题标题】:iPhone application freeze when out of range from wifi connection?iPhone应用程序在超出wifi连接范围时冻结?
【发布时间】:2012-05-18 14:43:57
【问题描述】:

我开发了一个无线电应用程序,它使用网络连接进行在线流媒体,并且我正在检查网络是否可用。如果没有网络连接,它会显示一个警报“他们没有可用的网络”。我的代码在这里

 - (void)viewDidLoad
  {
  [super viewDidLoad];


  //checking network reachability statys, this will show one alert view if no network available
    Reachability* reachabile = [Reachability reachabilityWithHostName:@"www.apple.com"];
    NetworkStatus remoteHostStatus = [reachabile currentReachabilityStatus];

   if(remoteHostStatus == NotReachable) 
    {

    NSLog(@"not reachable");
       UIAlertView *notReachableAlert1=[[UIAlertView alloc]initWithTitle:@"NO INTERNET  CONNECTION" message:@"This Application Need Internet To Run" delegate:self cancelButtonTitle:@"Okay Buddy" otherButtonTitles:nil];
    notReachableAlert1.delegate=self;
     [notReachableAlert1 show];
     [notReachableAlert1 release];


    }




    [[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayerPlaybackStateDidChange:) 
                                             name:MPMoviePlayerPlaybackStateDidChangeNotification 
                                           object:nil];




    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerLoadStateChanged:) name:MPMoviePlayerLoadStateDidChangeNotification object:player];



    // Do any additional setup after loading the view from its nib.
  }

我还检查了通知的条件

  -(void) moviePlayerPlaybackStateDidChange:(NSNotification*)notification 
  {
  Reachability* reachabile = [Reachability reachabilityWithHostName:@"www.apple.com"];
  NetworkStatus remoteHostStatus = [reachabile currentReachabilityStatus];

  NSLog(@"playbackDidChanged");

  MPMoviePlayerController *moviePlayer = notification.object;
  player=notification.object;
  MPMoviePlaybackState playbackState = moviePlayer.playbackState;
   if(playbackState == MPMoviePlaybackStateStopped) 
     {
      NSLog(@"MPMoviePlaybackStateStopped");


   }
   else if(playbackState == MPMoviePlaybackStatePlaying) {
    NSLog(@"MPMoviePlaybackStatePlaying");


} else if(playbackState == MPMoviePlaybackStatePaused) {
    NSLog(@"MPMoviePlaybackStatePaused");

    if(remoteHostStatus == NotReachable) 
    {

          NSLog(@"not reachable");
          UIAlertView *notReachableAlert1=[[UIAlertView alloc]initWithTitle:@"NO INTERNET CONNECTION" message:@"This Application Need Internet To Run" delegate:self cancelButtonTitle:@"Okay Buddy" otherButtonTitles:nil];
        notReachableAlert1.delegate=self;
        [notReachableAlert1 show];
        [notReachableAlert1 release];


      }
 } else if(playbackState == MPMoviePlaybackStateInterrupted) 
    {
    NSLog(@"MPMoviePlaybackStateInterrupted");

    if((remoteHostStatus == NotReachable)&&(remoteHostStatus != ReachableViaWiFi)) 
    {

          NSLog(@"not reachable");
          UIAlertView *notReachableAlert1=[[UIAlertView alloc]initWithTitle:@"NO INTERNET CONNECTION" message:@"This Application Need Internet To Run" delegate:self cancelButtonTitle:@"Okay Buddy" otherButtonTitles:nil];
        notReachableAlert1.delegate=self;
        [notReachableAlert1 show];
        [notReachableAlert1 release];


     }

我的问题是,当应用程序在没有 3g 和正常数据连接的情况下超出 wifi 连接范围时,它会冻结一段时间。当我回到范围时,它会进入活动状态并显示警报。

我在网络可用性检查方面做错了什么吗?

【问题讨论】:

    标签: wifi freeze


    【解决方案1】:

    这是您可以根据您的应用程序编辑的示例

    @class Reachability;
    
    @interface urAppDelegate : NSObject <UIApplicationDelegate> 
    {
    
            Reachability* internetReachable;
    
            Reachability* hostReachable;
    
            BOOL   hostActive;
    
            BOOL   internetActive; 
    
    }
    
    @property (nonatomic, assign) BOOL hostActive;
    
    @property (nonatomic, assign) BOOL internetActive;
    @end
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    
        self.internetActive=NO;
        self.hostActive=NO;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
    
        internetReachable = [[Reachability reachabilityForInternetConnection] retain];
        [internetReachable startNotifier];
    
        // check if a pathway to a random host exists
        hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
        [hostReachable startNotifier];
    
        // now patiently wait for the notification
    
     [self.window makeKeyAndVisible];
        return YES;
    }
    
    -(void) checkNetworkStatus:(NSNotification *)notice{
        NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    
        switch (internetStatus)
        {
            case NotReachable:
            {
                NSLog(@"The internet is down.");
                self.internetActive = NO;            
                break;
            }
            case ReachableViaWiFi:
            {
                NSLog(@"The internet is working via WIFI.");
                self.internetActive = YES;
    
                break;
            }
            case ReachableViaWWAN:
            {
                NSLog(@"The internet is working via WWAN.");
                self.internetActive = YES;            
                break;
            }
        }
    
        NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
        switch (hostStatus)
        {
            case NotReachable:
            {
                NSLog(@"A gateway to the host server is down.");
                self.hostActive = NO;            
                break;
            }
            case ReachableViaWiFi:
            {
                NSLog(@"A gateway to the host server is working via WIFI.");
                self.hostActive = YES;
    
                break;
            }
            case ReachableViaWWAN:
            {
                NSLog(@"A gateway to the host server is working via WWAN.");
                self.hostActive = YES;
                break;
            }
        }
    
    
        if (internetActive && hostActive)
        {       
            // Net work Available.......
    
        }
        else
        {
            UIAlertView *netWorkAlert=[[UIAlertView alloc]initWithTitle:@"Network Connection Error" message:@"Please Check Connection" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles:nil, nil];
            [netWorkAlert show];
            [netWorkAlert release];
        }
    
    }
    

    【讨论】:

    • 好的,谢谢您的反馈,请您指定哪些对象是这些 internetReachable、hostReachable。 ?这些是可达性类的对象吗?
    • internetReachable 和 hostReachable 都是 Reachability 类的实例
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    相关资源
    最近更新 更多