【发布时间】:2012-03-22 15:37:52
【问题描述】:
我有这种方法可以将我的应用程序中的 html 文件加载到 UIWebView 中。 问题是这个文件名不时改变 - 我无法让它工作。 这是我的代码:
NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:[NSString stringWithFormat:@"%@_file",MyFileNameString] ofType:@"htm"];
// make a file: URL out of the path
NSURL *instructionsURL = [NSURL fileURLWithPath:path];
[ProfileHtml loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
我遇到了这个问题:[NSURL initFileURLWithPath:]: nil string parameter.
当我以这种方式使用它时,它的工作原理:
NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:[NSString stringWithFormat:@"%@_file",@"amir"] ofType:@"htm"];
// make a file: URL out of the path
NSURL *instructionsURL = [NSURL fileURLWithPath:path];
[ProfileHtml loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
我应该怎么做才能解决它? 谢谢!
//////////更新
这是整个方法: 希望你能帮助我。
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *zodiacProfile = [defaults valueForKey:@"zodiacProfile"];
NSLog(@"zodiacProfile: %@",zodiacProfile);
[zodiacWallpaper setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Wallpaper_%@",zodiacProfile]]];
zodiacTitle.text = [NSString stringWithFormat:@"About %@",zodiacProfile];
NSString *path;
NSBundle *thisBundle = [NSBundle mainBundle];
path = [thisBundle pathForResource:[NSString stringWithFormat:@"%@HTM",zodiacProfile] ofType:@"htm"];
NSURL *instructionsURL = [NSURL fileURLWithPath:path];
NSLog(@"zodiac: %@",zodiacProfile);
NSLog(@"path: %@",path);
NSLog(@"url: %@",instructionsURL);
[ProfileHtml loadRequest:[NSURLRequest requestWithURL:instructionsURL]];
【问题讨论】:
-
那么
MyFileNameString的内容是什么?如果这是工作和不工作之间的唯一区别,那就是问题所在。 -
在您的第一个示例中,您确定
MyFileNameString设置为您认为的值吗?你确定你的包中存在同名的文件吗? -
在构建应用程序之前,您确定所有 htm 文件都已添加到正确的目标(复制文件构建阶段)?
-
我想说你应该测试
pathForResource和fileURLWithPath的结果是否为零。 -
当我像第一个例子一样使用它时,路径变为 nil .. 这就是问题,但我应该如何解决它?
标签: iphone objective-c uiwebview