2012-03-22 73 views
0

我有這種方法,從我的應用程序加載HTML文件到UIWebView。 問題是這個文件名不時變化 - 我無法讓它工作。 這是我的代碼:無法加載「路徑」與「stringWithFormat」objective-c

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]]; 
+0

那麼'MyFileNameString'的內容呢?如果這是工作和不工作之間的唯一區別,那就是問題所在。 – trojanfoe 2012-03-22 15:43:15

+0

在你的第一個例子中,你確定'MyFileNameString'被設置爲你認爲的值嗎?你確定你的捆綁包中有這個名字的文件嗎? – jonkroll 2012-03-22 15:43:49

+0

在構建應用程序(複製文件構建階段)之前,您確定所有htm文件都已添加到正確的目標嗎? – Alladinian 2012-03-22 15:51:19

回答

0

好的,問題是,我想要的焦點和忘記將html文件添加到我的項目。 對於將來需要此代碼的所有人來說 - 它的工作非常完美。

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]]; 
相關問題