gpt4 book ai didi

iphone - 有任何可达性专家吗?

转载 作者:行者123 更新时间:2023-12-03 16:35:18 25 4
gpt4 key购买 nike

在将Reachability .h和.m文件合并到其中以检查Internet连接之前,我的应用程序运行良好。尽管大部分情况下一切仍然运行正常(所有网页均正确加载,并且当我有意关闭机场进行测试时,可达性很好地捕获了它)-我现在确实在奇怪的时候遇到了莫名其妙的崩溃,令人恐惧的“Exc-Bad-Access”错误...
我运行仪器并找到了僵尸-参见屏幕抓图:

查看“RefCt”列,您可以看到它达到15-我已经看到它超过20!
在“负责 call 者”列(最右侧)中,我看到了各种我不认识的方法-以及我当然没有启动的 call 者-我假设它们在运行期间由系统内部调用-时间?

无论哪种方式,我都非常仔细地遵循了Apple的可达性代码和说明,以及来自这个排名很高的stackoverflow线程的提示:
How to check for an active Internet connection on iOS or OSX?

但是我仍然遇到这些莫名其妙的崩溃。

谁能提供任何建议?

这是代码:

#import "Reachability.h"


-(void) viewWillAppear:(BOOL)animated
{
// check for internet connection
[[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
}



-(void) viewDidLoad {

url = [NSURL URLWithString: @"http://www.google.com"];
NSURLRequest *req = [NSURLRequest requestWithURL: url];
[webPageView loadRequest:req];
[super viewDidLoad];
}



-(void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

switch (internetStatus)
{
case NotReachable:
{
[self displayMessageWithTitle:@"ERROR"
andMessage:@"Unable To Connect to Internet"
andOKButton:@"OK"];
[self dismissModalViewControllerAnimated:YES];
break;

}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");

break;

}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");

break;

}
}

NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
// NSLog(@"A gateway to the host server is down.");

[self displayMessageWithTitle:@"ERROR"
andMessage:@"Host Not Reachable"
andOKButton:@"OK"];
[self dismissModalViewControllerAnimated:YES];
break;

}

case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");

break;

}

case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");

break;

}
}
}



- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicator stopAnimating];
}


// Since this ViewController is presented Modally, this method removes it
// via a button click:
-(IBAction) dismissWebTixView:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}




-(void) viewWillDisappear:(BOOL)animated {
NSLog(@"In webForTix's 'viewWillDisappear' method....");
[[NSNotificationCenter defaultCenter] removeObserver:self
name:kReachabilityChangedNotification
object:nil];
}


// Was told it might be good to put "removeObserver" here and not in viewWillDisappear
// well neither worked - the App still crashes....
- (void)dealloc
{
//NSLog(@"In webForTix's dealloc method....");
// [[NSNotificationCenter defaultCenter] removeObserver:self
// name:kReachabilityChangedNotification
// object:nil];
NSLog(@"In webForTix's dealloc method - removedObserver...");
[super dealloc];

}

最佳答案

您必须仔细平衡对addObserver和removeObserver的调用。

如果在vieWillAppear中添加通知的观察者,则需要在viewWillDisappear中将其删除。

如果您两次添加观察者,则对于同一通知您将被调用两次,并且必须调用两次removeObserver才能摆脱这两个通知。

我没有发现任何明显的内存管理问题。您发布的所有代码似乎都在创建自动发布的对象,应该没问题。

关于iphone - 有任何可达性专家吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8362996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com