NSURLREQUEST 时我收到 EXC_BAD_ACCESS

发布于 2024-12-11 02:12:53 字数 4496 浏览 0 评论 0原文

我在 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

I am getting EXC_BAD_ACCESS while NSURLREQUEST.
I am giving pdf url from server to to webview through AppDelegate_iPhone's currentBookPressed.
please can anyone tell what is the problem ...
Code:-

@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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨巷深深 2024-12-18 02:12:53

您应该将 line 替换

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

为 line

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

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

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

You should replace line

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

with line

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

It is because method requestWithURL:cachePolicy:timeoutInterval: in first parameter is waiting for object of NSURL class. In targerURL you have exactly that one.

Moreover in method [NSURL URLWithString:targetURL] (if you will need it) you should pass NSString as a first parameter, but you are passing NSURL.

牛↙奶布丁 2024-12-18 02:12:53

您在这一行中的问题

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

您的情况出现问题是因为 + (id)URLWithString:(NSString *)URLString 的参数是 NSString 并且您正在传递 NSURL 和方法尝试通过调用 -length 获取假定字符串的长度,它存在于 NSString 但不存在于 NSURL 中。

Your problem in this line

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

The problem in your case arises because parameter for + (id)URLWithString:(NSString *)URLString is NSString and you are passing NSURL and method trying to get the length of the supposed string by calling -length, which exists for NSString but not for NSURL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文