1

我在这方面还很陌生,并且尝试解决这个问题已经有一段时间了。我想从 url 下载文件并将其保存到设备上的文档文件夹中。之后,我想将其加载到 Webview。如果我手动复制文件,webview 部分似乎可以工作,但是当我保存 URL 时,它只显示使用正确文件名保存的 5529 个字节?

以后我需要从安全的网络服务器加载文件,因此需要连接身份验证方法。

@interface ViewController ()

@end

@implementation ViewController
@synthesize webView;
@synthesize webData;

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

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


-(void)handleDocumentOpenURL:(NSURL *)url
{
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    [webView setUserInteractionEnabled:YES];
    [webView loadRequest:req];
}
-(void)loadFileFromDocumentFolder:(NSString *) filename
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *file = [documentsDirectory stringByAppendingPathComponent:filename];
    NSURL *urlPdf = [NSURL fileURLWithPath: file];
    [self handleDocumentOpenURL:urlPdf];

}
- (IBAction)btnSave:(id)sender
{
    NSString *urlString = @"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf";

    NSURL *url = [NSURL URLWithString:urlString];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:YES];

    if (urlConnection)
    {
       webData = [NSMutableData data];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error !" message:@"Error has occured, please verify internet connection"  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];

        [alert show];

    }
}
- (IBAction)btnLoad:(UIButton *)sender
{
    [self loadFileFromDocumentFolder:@"test.pdf"];
}

#pragma mark - NSURLConnection Delegate Methods
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return YES;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{

        [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"did fail");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

    NSLog(@"did receive data");
    [webData appendData:data];
    }
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"did receive response");
    [webData setLength:0];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    BOOL isSuccessfull;
    NSLog(@"did finish loading. Bytes Received: %d", [webData length]);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"test.pdf"];

    isSuccessfull = [webData writeToFile:pdfPath atomically:YES];

}
@end
4

0 回答 0