简体   繁体   English

可达性崩溃应用

[英]Reachability crashes app

I just implemented Apple's Reachability code and my App is now crashing in a completely inconsistent and random fashion: it could successfully load 20 web-pages in a row - but then crash on the 21st. 我刚刚实现了Apple的Reachability代码,我的应用程序现在完全以随机和不一致的方式崩溃:它可以连续成功加载20个网页-但随后在21日崩溃。 attempt, or it could crash after just the 2nd. 尝试,否则可能会在第二次崩溃。 web-page load attempt 网页加载尝试

Instruments/NSZombies shows something strange: the RefCt gets to be as high as 20 (!), with the "Responsible Callers" being several different ones: [UIRuntimeConnection initWithCoder], [UINib instantiateWithOwner: options], [NSKeyedUnarchiver _decodeArrayOfObjectsForKey:], etc. Is that normal? Instruments / NSZombies显示出一些奇怪的东西:RefCt高达20(!),“负责呼叫者”是几个不同的人:[UIRuntimeConnection initWithCoder],[UINib InstantiateWithOwner:options],[NSKeyedUnarchiver _decodeArrayOfObjectsForKey:]等那正常吗?

Or should I just focus on the last Responsible Callers? 还是我应该只关注最后的责任呼叫者? Those are [UIWindowController transitionViewDidComplete:fromView:toView:] (which brings the RefCt to 0), and [UIWebView webView:didFinishLoadForFrame:] (which takes the RefCt down to -1)? 它们是[UIWindowController transitionViewDidComplete:fromView:toView:](将RefCt设为0)和[UIWebView webView:didFinishLoadForFrame:](将RefCt降至-1)?

How do I go about debugging and solving this? 我该如何进行调试和解决?

Here is the Code: 这是代码:

#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];

} }

Try this code sure it will work. 尝试使用此代码,以确保它可以正常工作。

Include Reachability.h and Reachability.m files from developer apple to your project.Import SystemConfiguration framework from SDK libraries to your project.Then add the following GlobalFunction.h and GlobalFunction.m files to your project 将来自开发人员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.

相关问题 在检查iphone中的网络可达性后app崩溃了? - app crashes after checking network reachability in iphone? 应用程序在后台时的iOS可达性 - iOS Reachability when app is in Background 什么时候*必须*应用程序包含Reachability类来测试网络可达性? - When exactly *must* an app include the Reachability class to test for network reachability? 可达性示例应用程序为何在这里停滞? - Why does Reachability sample app stall here? 我可以使用Apple Reachability类制作“付费应用”吗? - Can I make a 'paid app' with apple Reachability classes? 同一应用程序中的ASIHTTPRequest和ShareKit具有两个可达性文件 - ASIHTTPRequest and ShareKit in the same app has two reachability files 我是否需要使用可达性来发送应用内 Email? - Do I need to use Reachability for sending in-app Email? iPhone可达性-如何在整个应用程序中使用它? - iPhone Reachability - How can I use it across my entire app? 使用可访问性类检查Internet连接时应用程序崩溃 - App crashing when using Reachability Classes to check for internet connection 在iPhone应用程序上具有可否肯定性的可到达性-它会超越苹果吗? - Reachability on iPhone app with a false positive - will it get past apple?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM