我在NSURLREQUEST时获得EXC_BAD_ACCESS

时间:2011-10-18 07:57:23

标签: iphone objective-c nsurlconnection nsurlrequest

我在NSURLREQUEST时获得EXC_BAD_ACCESS。 我通过AppDelegate_iPhone的currentBookPressed将pdf url从服务器发送到webview。 请任何人都能说出问题是什么...... 代码: -

@class AppDelegate_iPhone;
@interface PdfShowViewController : UIViewController<UIWebViewDelegate> {

    UIWebView *pdfWebview;
    AppDelegate_iPhone *appDelegate;
    NSMutableData *receivedData;
    UIActivityIndicatorView *myIndicator;
    IBOutlet UIProgressView *progress;

    NSURLRequest* DownloadRequest;
    NSURLConnection* DownloadConnection;

    long long bytesReceived;
    long long expectedBytes;

}


@property (nonatomic,retain) UIWebView *pdfWebview;
@property (nonatomic,retain) UIActivityIndicatorView *myIndicator;
@property (nonatomic,retain) IBOutlet UIProgressView *progress;
@property (nonatomic,retain) NSMutableData *receivedData;
@property (nonatomic, readonly, retain) NSURLRequest* DownloadRequest;
@property (nonatomic, readonly, retain) NSURLConnection* DownloadConnection;

-(IBAction)onTapBack;

@end


#import "PdfShowViewController.h"
#import "AppDelegate_iPhone.h"

@implementation PdfShowViewController

@synthesize pdfWebview,myIndicator,progress,receivedData,DownloadRequest,DownloadConnection;

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

    unsigned char byteBuffer[[receivedData length]];
    [receivedData getBytes:byteBuffer];
    NSLog(@"Data === %ld",receivedData);

    NSInteger receivedLen = [data length];
    bytesReceived = (bytesReceived + receivedLen);
    NSLog(@"received Bytes ==  %f",bytesReceived);

    if(expectedBytes != NSURLResponseUnknownLength) 
    {
        NSLog(@"Expected Bytes in if ==  %f",expectedBytes);
        NSLog(@"received Bytes in if ==  %f",bytesReceived);

        float value = ((float) (bytesReceived *100/expectedBytes))/100;
        NSLog(@"Value ==  %f",value);
        progress.progress=value;
    }

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    //[connection release];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    expectedBytes = [response expectedContentLength];
    NSLog(@"%f",expectedBytes);

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

    [myIndicator stopAnimating];
    [myIndicator removeFromSuperview];

    pdfWebview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 40, 320, 420)];
    [pdfWebview setAutoresizingMask:UIViewAutoresizingFlexibleWidth];   
    [pdfWebview setScalesPageToFit:YES];
    [pdfWebview setAutoresizesSubviews:YES];

    [pdfWebview loadRequest:DownloadRequest];

    [self.view addSubview:pdfWebview];

    //[connection release];


}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

    appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate];

    myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    myIndicator.center = self.view.center;  
    myIndicator.hidesWhenStopped = NO;
    [self.view addSubview:myIndicator];
    [myIndicator startAnimating];

    //receivedData = [[NSMutableData alloc] initWithLength:0];
    NSLog(@"%@",appDelegate.currentBookPressed);
    NSString * urlString = [appDelegate.currentBookPressed stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSLog(@"%@",urlString);

    NSURL *targetURL = [NSURL URLWithString:urlString];
    NSLog(@"%@",targetURL);

// Here comes Acception

    DownloadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:targetURL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:120.0];
    DownloadConnection = [[NSURLConnection alloc] initWithRequest:DownloadRequest delegate:self];

    if (DownloadConnection) {
        receivedData = [[[NSMutableData data]initWithLength:0]retain];
    }

}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}
-(IBAction)onTapBack
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
    [super dealloc];
    [pdfWebview release];
    [receivedData release];
}


@end

2 个答案:

答案 0 :(得分:2)

你应该替换行

DownloadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:targetURL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:120.0];

带行

DownloadRequest = [NSURLRequest requestWithURL:targetURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:120.0];

这是因为第一个参数中的方法requestWithURL:cachePolicy:timeoutInterval:正在等待NSURL类的对象。在targerURL中你只有那个。

此外,在方法[NSURL URLWithString:targetURL]中(如果您需要),您应该将NSString作为第一个参数传递,但是您正在传递NSURL

答案 1 :(得分:1)

你的问题在这一行

DownloadRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:targetURL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:120.0];  

因为+ (id)URLWithString:(NSString *)URLString的参数是NSString并且您正在通过调用-length来调用{{1}}来尝试获取所谓字符串的长度,因此会出现问题。 NSString但不适用于NSURL。