如何在iPhone中处理网络切换?

时间:2009-04-29 05:25:07

标签: iphone

我刚刚开发了一个iPhone音频流应用程序......现在它可以播放音频文件而没有任何问题。当iPhone网络从3G切换到WiFi时出现问题而反之亦然..我需要在网络切换时暂停流,但是当网络到来时我无法恢复播放。任何人都可以指出如何用代码处理这种情况?

此致 Syam S

1 个答案:

答案 0 :(得分:3)

您可以将应用设置为check Reachability status,然后在网络状态发生变化且网络无法访问时发送“暂停”NSNotification(同样,“播放”NSNotification当网络状态发生变化,现在网络再次可以访问时):

[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:@"kNetworkReachabilityChangedNotification" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseStreaming:) name:@"kPauseAudioStreamingIfPlaying" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pauseStreaming:) name:@"kPlayAudioStreamingIfPaused" object:nil];

- (void) reachabilityChanged:(id)sender {
  NetworkStatus reachabilityStatus = [[Reachability sharedReachability] internetConnectionStatus];
  if (reachabilityStatus == NotReachable)
     [[NSNotificationCenter defaultCenter] postNotificationName:@"kPauseAudioStreamingIfPlaying" object:nil];
  else 
     [[NSNotificationCenter defaultCenter] postNotificationName:@"kPlayAudioStreamingIfPaused" object:nil];
}