0

我是 iPhone 新手,

我目前正在开发一个 iPhone 应用程序,并且我已经实现了从 url 下载文件的能力。我已经创建了 UIWebView,当用户点击下载链接时webview,下载将开始,我将该文件保存到文档目录中的指定文件夹中,这一切都在我的Second View..

但是在此之后,当我按下后退按钮导航到 myFirst view时,我的应用程序崩溃了......显示EXC_BAD_ACCESS

-(void)viewWillAppear:(BOOL)animated{
        //Doing some operation and it works fine...
           NSLog(@"viewWillAppear in First View.......");
    }

-(void)viewDidAppear:(BOOL)animated{
    NSLog(@"viewDidAppear in First View.......");
}

Log当我点击返回按钮但我的应用程序在 1 秒或半秒后崩溃时,我可以看到上面。

这是我的代码Second View

- (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 {

    url = [request URL];

    //CAPTURE USER LINK-CLICK.

            DirPath=[self applicationDocumentsDirectory];

            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;   
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{ 
    if (buttonIndex == 0) 
    {        
        // Create the request.
        NSURLRequest *theRequest1=[NSURLRequest requestWithURL:[NSURL URLWithString:Durl]
                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                              timeoutInterval:60.0];

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

    }
    else
    {[alertView dismissWithClickedButtonIndex:1 animated:TRUE];}
}

- (void)webView:(UIWebView *)webview didFailLoadWithError:(NSError *)error1 {

    NSLog(@"didFailLoadWithError: %@; stillLoading:%@", error1,(webview.loading?@"NO":@"YES"));
}

我的日志显示:didFailLoadWithError: Error Domain=WebKitErrorDomain Code=102 "Frame load interrupted" UserInfo=0x6b34910 {NSErrorFailingURLKey=MY_URL, NSErrorFailingURLStringKey=MY_URL, NSLocalizedDescription=Frame load interrupted}; stillLoading:YES

DirPath=/Users/krunal/Library/Application Support/iPhone Simulator/5.0/Applications/FCDDDE83-A9B3-4C14-A56C-E8C5FCE7F5C4/Documents/DownloadedFile.epub

任何帮助都会得到帮助。

4

5 回答 5

1

首先,你在这里试图做的事情,你不应该从一个角度去做。与网络服务通信的代码最好在模型类中,而不是在视图中:

http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html

现在,您可能会遇到连接正在调用您在视图中实现的委托方法的情况,但是当您单击后退按钮时,您的视图已经被释放。

此外,您无需维护指向您在 webView:shouldStartLoadWithRequest: 方法中创建的 NSURLConnection 实例的指针。相反,您依赖于调用 connectionDidFinishLoading: 方法来再次释放连接对象。这样,您永远无法确定您是否正在释放它,或者您是否可能过度释放它,如果该方法被多次调用。

在你的视图类中使用一个实例变量来保存一个指向连接对象的指针,这样你就可以在必要时释放它(当你不再使用它时,或者当视图消失时)。确保在删除视图之前取消查询([连接取消])。

于 2012-08-21T12:52:04.397 回答
1

我认为你发布了错误的东西......考虑一下,

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error1

无需释放连接,receivedData

在 dealloc 块,添加此代码

- (void) dealloc
  {
      if (theConnection)
      { 
           [theConnection release], theConnection = nil;
      }

      if (receivedData)
      { 
           [receivedData release], receivedData = nil;
      }
  }

如果您使用相同的 webView 创建多个连接,请添加,然后添加

if (theConnection)
{ 
      [theConnection release], theConnection = nil;
}

if (receivedData)
{ 
     receivedData release], receivedData = nil;
}

在分配 URLConnection 和 NsMutableData 之前,这是阻止内存泄漏的措施。最好有一个活动微调器,直到事件完成。

于 2012-08-23T16:15:22.300 回答
1

尝试在 secondView Controller 类中添加它

- (void)viewWillDisappear
{
    if ([webView isLoading])
        [webView stopLoading];

    [webView setDelegate:nil];
}

或者在 secondView 的后退按钮操作中添加这个

于 2012-08-24T18:34:35.177 回答
0

做两件事:

  1. 无论您在哪里释放连接和数据(receivedData),都将其设置为 nil。
  2. 在您的 dealloc 方法中,在释放 webView 之前 [webView stopLoading]。
于 2012-08-24T13:05:34.967 回答
0

在添加 WebView 的第一个视图的 dealloc 方法中,设置 webView = nil 的委托。有时这可能是崩溃的原因。

于 2012-08-21T17:36:34.217 回答