0

我是 iPhone 新手,

我正在按照苹果的建议创建 NSURLConnection但是 当我关闭我的视图时我的应用程序崩溃了,我尝试了NSZombieEnabled向我展示的概念-[CALayer release]: message sent to deallocated instance 0x68b8f40

我正在显示一个网页Webview,当用户单击 webview 中的下载链接时,shouldStartLoadWithRequest将在我正在创建的此方法中调用方法NSURLConnection

这是我的代码片段,

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1
{
    [receivedData appendData:data1];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);

    DirPath=[self applicationDocumentsDirectory];

     NSLog(@"DirPath=%@",DirPath);
    [receivedData writeToFile:DirPath atomically:YES];

    UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:@"Download Complete !"
                                                         message:nil delegate:nil 
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
    [Alert show];
    [Alert release];


    // release the connection, and the data object
    [connection release];
    [receivedData release];
}


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error1
{
    [connection release];
    [receivedData release];

    // inform the user
    NSLog(@"Connection failed! Error - %@ %@",
          [error1 localizedDescription],
          [[error1 userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {


    //CAPTURE USER LINK-CLICK.

            Durl=[[url absoluteString]copy];

            //Checking for Duplicate .FILE at downloaded path....

            BOOL success =[[NSFileManager defaultManager] fileExistsAtPath:path];
            lastPath=[[url lastPathComponent] copy];

            if (success) //if duplicate file found...
            {
                UIAlertView* Alert = [[UIAlertView alloc] initWithTitle:@"This FILE is already present in Library."
                                                                     message:@"Do you want to Downlaod again ?" delegate:self 
                                                           cancelButtonTitle:nil
                                                           otherButtonTitles:@"Yes",@"No",nil];
                [Alert show];
                [Alert release];

            }
            else  //if duplicate file not found directly start download...
            {
                // Create the request.
                NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:Durl]
                                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                      timeoutInterval:60.0];

                // create the connection with the request and start loading the data
                NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
                if (theConnection) {
                    // Create the NSMutableData to hold the received data.
                    receivedData = [[NSMutableData data] retain];
                } else {
                    NSLog(@"Inform the user that the connection failed."); 
                }

    return YES;   
}

任何帮助将不胜感激。

4

2 回答 2

0

我认为这个问题 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

当您关闭视图时,您正在创建一个NSURLConnection具有委托自我的实例,该视图deallocated上的所有内容。但是当NSURLConnection尝试调用它的委托方法时会发生崩溃。

因此,您需要将委托设置NSURLConnection为 nil 以viewWillDisappear执行此操作,您需要NSURLConnection在接口中创建对象。

@interface yourClass
{
   NSURLConnection *theConnection;
}
@end

theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

- (void)viewWillDisappaer:(BOOL)animated
{
    [super viewWillDisappaer:animated];
    theConnection.delegate = nil;
}
于 2012-08-23T16:19:11.657 回答
0

确保在您的 dealloc 实现中您正在重置delegateto nil。这样做的另一个地方是viewWillDisappear:.

您的应用程序崩溃/访问僵尸的原因是该UIWebView实例可能会尝试回调 viewController,即使它已经被释放。为了防止这种情况,您必须在 viewController 超出范围之前将delegate其设置UIWebView回。nil在 iOS5 的 ARC 实现之前使用委托时,这是一个普遍的问题。iOS5 最终提供了弱引用,这些实际上是nil它们自己的实例被释放后。

示例 A:

- (void)dealloc
{
    [...]
    _webView.delegate = nil;
}

示例 B:

- (void)viewWillDisappaer:(BOOL)animated
{
    [super viewWillDisappaer:animated];
    _webView.delegate = nil;
}

编辑:再次阅读您的问题后,我意识到僵尸是 UIView 或 UIControl,因为消息已发送到 CALayer。通过暂时删除所有与 webView 相关的代码,确保您的问题实际上与 web 视图有关。

于 2012-08-23T06:30:35.987 回答