私はこれにかなり慣れていないので、かなり長い間この問題を解決しようとしました。URL からファイルをダウンロードして、デバイスのドキュメント フォルダに保存したいと考えています。その後、Webviewにロードしたいと思います。ファイルを手動でコピーすると WebView 部分が機能するように見えますが、URL を保存すると、正しいファイル名で保存された 5529 バイトしか表示されませんか?
将来的には、安全な Web サーバーからのファイルのロードに取り組む必要があるため、接続認証メソッドを使用します。
@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