在iPhone中单击后退按钮时应用程序崩溃
我不熟悉iPhone 我目前正在开发一个iPhone应用程序,并在其中实现了从url下载文件的功能。我已经创建了UIWebView,当用户单击在iPhone中单击后退按钮时应用程序崩溃,iphone,ipad,uiwebview,nsurlconnection,back-button,Iphone,Ipad,Uiwebview,Nsurlconnection,Back Button,我不熟悉iPhone 我目前正在开发一个iPhone应用程序,并在其中实现了从url下载文件的功能。我已经创建了UIWebView,当用户单击webview中的下载链接时,下载将开始,我正在将该文件保存到documents目录中的指定文件夹中,这一切都在我的第二个视图中正常工作 但在这之后,当我按下后退按钮导航到my第一视图时,我的应用程序崩溃了。。。显示EXC\u坏访问 -(void)viewWillAppear:(BOOL)animated{ //Doing some ope
webview
中的下载链接时,下载将开始,我正在将该文件保存到documents目录中的指定文件夹中,这一切都在我的第二个视图中正常工作
但在这之后,当我按下后退按钮导航到my第一视图时,我的应用程序崩溃了。。。显示EXC\u坏访问
-(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秒或半秒后崩溃
这是我在第二视图中的代码
- (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"));
}
我的日志显示:didfailloadwitheror:Error Domain=webkiterrodomain code=102“帧加载中断”UserInfo=0x6b34910{NSErrorFailingURLKey=My\u URL,NSErrorFailingURLStringKey=My\u URL,NSLocalizedDescription=帧加载中断};静载:是
DirPath=/Users/krunal/Library/Application Support/iPhone Simulator/5.0/Applications/FCDDDE83-A9B3-4C14-A56C-E8C5FCE7F5C4/Documents/DownloadedFile.epub
任何帮助都将受到感谢。首先,你在这里要做的事情,不应该从一个角度来看。与网络服务通信的代码最好在模型类中,而不是在视图中:
现在,您可以在连接调用您在视图中实现的委托方法的情况下结束,但是您的视图已经被释放,正如您单击“上一步”按钮一样
此外,您不维护指向在webView:shouldStartLoadWithRequest:method中创建的NSURLConnection实例的指针。相反,您依赖于调用connectionDidFinishLoading:方法来再次释放连接对象。这样,如果多次调用该方法,您永远无法确定是否正在释放它,或者是否过度释放了它
在视图类中使用实例变量来保存指向连接对象的指针,以便在必要时(当不再使用它时,或当视图消失时)释放它。请确保在删除视图之前取消查询([connection cancel])。首先,您在此处尝试执行的操作不应该从视图中执行。与网络服务通信的代码最好在模型类中,而不是在视图中:
现在,您可以在连接调用您在视图中实现的委托方法的情况下结束,但是您的视图已经被释放,正如您单击“上一步”按钮一样
此外,您不维护指向在webView:shouldStartLoadWithRequest:method中创建的NSURLConnection实例的指针。相反,您依赖于调用connectionDidFinishLoading:方法来再次释放连接对象。这样,如果多次调用该方法,您永远无法确定是否正在释放它,或者是否过度释放了它
在视图类中使用实例变量来保存指向连接对象的指针,以便在必要时(当不再使用它时,或当视图消失时)释放它。请确保在删除视图([连接取消])之前也取消查询。在添加WebView的第一个视图的dealoc方法中,设置WebView的委托=nil。
有时,这可能是崩溃的原因。在添加WebView的第一个视图的dealloc方法中,设置WebView的委托=nil。
有时这可能是崩溃的原因。我认为你发布了错误的东西。。。考虑这个,
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error1
无需释放连接,即可接收数据
在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之前,这是一种阻止内存泄漏的措施。最好在活动结束前设置活动微调器。我认为您发布的内容是错误的。。。考虑这个,
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error1
无需释放连接,即可接收数据
在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之前,这是一种阻止内存泄漏的措施。最好在事件完成之前使用活动微调器。做两件事:
无论何时释放连接和数据(receivedData),请将其设置为nil李>
在您的dealloc方法中,在发布webView之前,[webView停止加载]
做两件事:
无论何时释放连接和数据(receivedData),请将其设置为nil李>
在您的dealloc方法中,在发布webView之前,[webView停止加载]
尝试将其添加到secondView控制器类中
- (void)viewWillDisappear
{
if ([webView isLoading])
[webView stopLoading];
[webView setDelegate:nil];
}
或者将其添加到secondView的后退按钮操作中尝试将其添加到secondView控制器类中
- (void)viewWillDisappear
{
if ([webView isLoading])
[webView stopLoading];
[webView setDelegate:nil];
}
或者将此添加到secondView的后退按钮操作中我不确定,但请尝试注释“[connection release];”代码释放实例变量的一个好做法是在释放它们后将它们设置为nil
,并计划不再使用它们。这样可以防止在发布后意外访问ivar而导致的许多EXC\u BAD\u ACCESS
错误。@Ramshad:我尝试过,但仍然崩溃。单击“上一步”按钮时,您应该尝试将web视图委托为零。Yourwebview.delegate=nil;在dealoc方法中,我在dealoc
方法中编写了webview.delegate=nil
,但仍然崩溃。我不确定,但请尝试注释“[connection release];”code释放实例变量的一个好做法是在释放它们之后设置它们,并计划不再使用它们。这样,在发布后意外访问ivar会导致许多EXC\u BAD\u ACCESS
错误