Я получаю EXC_BAD_ACCESS, пока NSURLREQUEST - PullRequest
0 голосов
/ 18 октября 2011

Я получаю EXC_BAD_ACCESS, пока NSURLREQUEST.Я даю URL pdf с сервера на веб-просмотр через текущую книгу AppDelegate_iPhone.кто-нибудь может подсказать в чем проблема ... Код: -

@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 ]

2 голосов
/ 18 октября 2011

Вы должны заменить строку

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 голос
/ 18 октября 2011

Ваша проблема в этой строке

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

Проблема в вашем случае возникает из-за того, что параметр для + (id)URLWithString:(NSString *)URLString равен NSString, и вы передаете NSURL, а метод пытается получить длину предполагаемой строки с помощьювызов -length, который существует для NSString, но не для NSURL.

...