2011-10-18 2 views
0

NSURLREQUEST中にEXC_BAD_ACCESSを取得しています。 私は、AppDelegate_iPhoneのcurrentBookPressedを通じてサーバーからwebviewにpdf urlを渡しています。 誰もが問題が何であるかを伝えることができますしてください... コード: -NSURLREQUEST中にEXC_BAD_ACCESSを取得しています

@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 
+0

例外が発生する場所はどこですか? – Eimantas

+0

私はどこにコードが書かれているのですか...私はNSURLREQUESTにアクセスしています...これはpdfリンクです... http://litofinter.es.milfoil.arvixe.com/PDF/1211025183_testです。 pdf – mAc

+0

受付:--- ***キャッチされていない例外 'NSInvalidArgumentException'のためアプリを終了します、理由: ' - [NSURLの長さ]:インスタンスに送信された認識できないセレクタ0x4c776e0' – mAc

答えて

2

あなたがラインにライン

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

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

を交換する必要がありますこれは、最初にための方法requestWithURL:cachePolicy:timeoutInterval:ですパラメータはNSURLクラスのオブジェクトを待機しています。 targerURLには、まさにそれがあります。

また、メソッド[NSURL URLWithString:targetURL](必要な場合)の最初のパラメーターとしてNSStringを渡す必要がありますが、NSURLを渡しています。

+0

もう一度ありがとう猫...しかし、 EXC_BAD_ACCESS、しかし異なったposiで。:)。それはライン[pdfWebview loadRequest:DownloadRequest]に来ています。 – mAc

+0

しかし、もう1つ質問があります。ポートレートモードで私のwebviewをロードすると、風景と肖像画の表示がうまくいきます...しかし、いつでも私のwebviewを風景モードでロードすると、Webビューは半画面のみ(圧縮)オリエンテーションをポートレートモードに変更すると、より多くのデータが圧縮されます...どうすればいいですか? – mAc

+0

1. 'viewDidLoad'の最後に' [DownloadRequest retain]; '行を追加してください。 2番目の質問は別のスレッド(別の質問)でお願いします – Nekto

1
この行の

あなたの問題

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

+ (id)URLWithString:(NSString *)URLStringためのパラメータがNSStringあるので、あなたのケースで問題が発生すると、あなたは-lengthを呼び出すことにより、想定される文字列の長さを取得しようとNSURLと方法を渡している、存在していますNSStringの場合はNSURLの場合は使用できません。

+0

あなたの説明に感謝します...それは私のために良かった、私はその部分を解決しましたが、今私はこの行の例外を得ています.......... ..しかし、それでも私はEXC_BAD_ACCESSを与えていますが、今は別のposi .. :)に来ています[pdfWebview loadRequest:DownloadRequest]; – mAc

+1

- (void)connectionDidFinishLoading :(NSURLConnection *)接続とチェック値 –

+0

Hmmm ..私はロードの要求を終わらせただけであることが分かった... – mAc

関連する問題