[英]Reachability crashes app
我剛剛實現了Apple的Reachability代碼,我的應用程序現在完全以隨機和不一致的方式崩潰:它可以連續成功加載20個網頁-但隨后在21日崩潰。 嘗試,否則可能會在第二次崩潰。 網頁加載嘗試
Instruments / NSZombies顯示出一些奇怪的東西:RefCt高達20(!),“負責呼叫者”是幾個不同的人:[UIRuntimeConnection initWithCoder],[UINib InstantiateWithOwner:options],[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:]等那正常嗎?
還是我應該只關注最后的責任呼叫者? 它們是[UIWindowController transitionViewDidComplete:fromView:toView:](將RefCt設為0)和[UIWebView webView:didFinishLoadForFrame:](將RefCt降至-1)?
我該如何進行調試和解決?
這是代碼:
#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:
{
//NSLog(@"The internet is down.");
//self.internetActive = NO; // (That's a BOOL variable)
[self displayMessageWithTitle:@"ERROR"
andMessage:@"Unable To Connect to Internet"
andOKButton:@"OK"];
[self dismissModalViewControllerAnimated:YES];
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; // (That's a BOOL variable)
break;
}
}
NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)
{
case NotReachable:
{
// NSLog(@"A gateway to the host server is down.");
// self.hostActive = NO; // (That's a BOOL variable)
[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.");
//self.hostActive = YES; // (That's a BOOL variable)
break;
}
case ReachableViaWWAN:
{
//NSLog(@"A gateway to the host server is working via WWAN.");
// self.hostActive = YES; // (That's a BOOL variable)
break;
}
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[activityIndicator stopAnimating];
}
// Since this ViewController is presented Modally, this method removes it:
-(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];
}
// Read somewhere that it might be better to put the removeObserver
// call here rather than viewWillDisappear...tried both - still get 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];
}
嘗試使用此代碼,以確保它可以正常工作。
將來自開發人員Apple的Reachability.h和Reachability.m文件包含到您的項目中。將SDK庫中的SystemConfiguration框架導入到您的項目中。然后將以下GlobalFunction.h和GlobalFunction.m文件添加到您的項目中
//GlobalFunction.h
#import <Foundation/Foundation.h>
@class Reachability;
@interface GlobalFunction : NSObject
{
Boolean internetActive;
Boolean hostActive;
Reachability * internetReachable;
Reachability * hostReachable;
Reachability * wifiReach;
}
@property (readwrite,assign) Boolean internetActive;
@property (readwrite,assign) Boolean hostActive;
- (Boolean) checkNetworkStatus;
- (BOOL)connectedToNetwork;
@end
//GlobalFunction.m
#import "GlobalFunction.h"
#import "Reachability.h"
#include <netinet/in.h>
#import <SystemConfiguration/SCNetworkReachability.h>
@implementation GlobalFunction
@synthesize internetActive,hostActive;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
// Checking Internet Connectivity
- (Boolean) checkNetworkStatus//:(NSNotification *)notice
{
// called after network status changes
internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
//NSLog(@"The internet is down.");
//[self ShowMsg:@"The internet connection appears to be offline."];
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;
}
default :
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;
}
}
[hostReachable release];
[internetReachable release];
return self.internetActive;
}
- (BOOL)connectedToNetwork {
// Create zero addy
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr*)&zeroAddress);
SCNetworkReachabilityFlags flags;
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags)
{
//NSLog(@"Error. Could not recover network reachability flags");
return 0;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
//below suggested by Ariel
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;
NSURL *testURL = [NSURL URLWithString:@"http://www.apple.com/"]; //comment by friendlydeveloper: maybe use www.google.com
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
//NSURLConnection *testConnection = [[NSURLConnection alloc] initWithRequest:testRequest delegate:nil]; //suggested by Ariel
NSURLConnection *testConnection = [[[NSURLConnection alloc] initWithRequest:testRequest delegate:nil] autorelease]; //modified by friendlydeveloper
return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
}
-(void)dealloc {
internetReachable=nil;
hostReachable=nil;
wifiReach=nil;
[super dealloc];
}
@end
------>Just write the code for checking internet connection
#import<GlobalFunction.m>
-(void)viewDidLoad
{
if([globalFunc checkNetworkStatus])
{
[self ShowAlert:@"Internet Connection appears"];
}
else
{
[self ShowAlert:@"The Internet connection appears to be offline.."];
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.