UIWebView отображает локально сохраненный документ на симуляторе, но не на устройстве - PullRequest
1 голос
/ 10 ноября 2011

У меня проблема с тем, что я пытаюсь просмотреть документ (документ Word, изображение или видео), который хранится локально.Я использую UIWebView и на симуляторе он работает отлично, но на устройстве это пустой экран (без ошибок в консоли).Вот мой код для просмотра документа:

    UIWebView *newWebView;
if (appDelegate.IS_IPAD)
{
    newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
}
else
{
    newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
}
newWebView.scalesPageToFit = YES;    
[self setWebView: newWebView];
[newWebView release], newWebView = nil;

[[self webView] setDelegate: self];
[[self view] addSubview:[self webView]];
NSURL *nsURL = [NSURL fileURLWithPath:filePath isDirectory:NO];
[[self webView] loadRequest:[NSURLRequest requestWithURL:nsURL]];

Вот как я сохраняю документ локально перед просмотром.Я думаю, что файл успешно сохраняется на устройстве, как и на симуляторе.

    // Save the file on the device
    NSURL *contentUrl = [NSURL URLWithString:selectedContent.contentUrl];
    NSString *fileName = [[contentUrl absoluteString] lastPathComponent];       
    NSString *homeDirectoryPath = NSHomeDirectory();  // Create the path
    NSString *unexpandedPath = [homeDirectoryPath stringByAppendingString:@"/MyApp/"];
    NSString *folderPath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedPath stringByExpandingTildeInPath]], nil]];
    NSString *unexpandedImagePath = [folderPath stringByAppendingFormat:@"/%@", fileName];
    NSString *filePath = [NSString pathWithComponents:[NSArray arrayWithObjects:[NSString stringWithString:[unexpandedImagePath stringByExpandingTildeInPath]], nil]];
    if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath isDirectory:NULL])
    {
        [[NSFileManager defaultManager] createDirectoryAtPath:nil withIntermediateDirectories:NO attributes:nil error:nil];        
    }
    // Do the actual write of the file to the device
    NSData *fileData = [NSData dataWithContentsOfURL:myUrl]; // Create the file data reference
    [fileData writeToFile:filePath atomically:YES]; //Save the document to the home directory

Ответы [ 2 ]

2 голосов
/ 10 ноября 2011

Следите за проблемами.Симулятор не чувствителен к регистру, в то время как iPhone!

0 голосов
/ 15 ноября 2011

Я решил эту проблему, сохранив свой документ в каталоге документов по умолчанию.Вот код, который я использовал для этого:

    NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDirectory = [arrayPaths objectAtIndex:0];
    NSString *filePath = [docDirectory stringByAppendingString:@"/"];
    filePath = [filePath stringByAppendingString:fileName];
    NSData *fileData = [NSData dataWithContentsOfURL:myUrl];
    [fileData writeToFile:filePath atomically:YES];
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...