2012-05-02 19 views
0

我試圖做一個簡單的應用程序(使用我的設備上狄奧所以沒有Xcode中/ MAC,只是iFile的)顯示一個HTML文件,並在通過下載新的更新本地HTML文件中的導航欄按鈕版本從服務器並覆蓋現有的版本。 我的問題是 HTML文件的較新版本不會被下載和現有的一個保持不變。,我怎麼可以下載一個HTML文件,並覆蓋現有的一個(目標c)?

當按下按鈕,這是我必須嘗試更新本地的HTML文件中的代碼:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"blank" ofType:@"html"]isDirectory:NO]]]; 

NSData *theData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://mydomain.com/index.html"]]; 
[theData writeToFile:[NSBundle pathForResource:@"index" ofType:@"html" inDirectory:[[NSBundle mainBundle] bundlePath]] atomically:NO]; 

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]]; 

包含blank.html是一個空白的HTML文件。我把它放在那裏,所以我可以刷新index.html,也有index.html不使用白色它會被新版本覆蓋。我不確定這是否會發生甚至是必要的,但之前並不奏效,我認爲這可能會解決問題,但事實並非如此。

一些更多的信息:

<*RootViewController.h*> 

@interface RootViewController: UIViewController { 
    UINavigationBar*navBar; 
    UIWebView*webView; 
} 
@property (nonatomic, retain) UIWebView*webView; 
@end 

<*RootViewController.mm*> 

#import "RootViewController.h"; 
@implementation RootViewController 
- (void)loadView { 
    //this is where I set up the webview, navigation bar, and button 
} 

@synthesize webView; 

- (void)theButtonPressed { 
    //this is where I try to update the index.html file 
} 

請讓我知道我做錯了,如何解決它,或者如果你需要更多的信息。謝謝!

回答

0

不是隻讀的包,和你試圖寫它。

+0

所以,如果我把它放在文件目錄,它會工作嗎?我現在在Resources文件夾中,它是隻讀嗎? – mobabur94

+0

應該工作,試試看。如果您不希望用戶通過iTunes查看/修改文件(使用應用程序支持目錄),則可以使用NSFileManager獲取指向它的url鏈接(或類似鏈接):URLForDirectory:NSApplicationSupportDirectory ::: – Gruntcakes

+0

好吧,我嘗試了使用文檔目錄是這樣的: [theData writeToFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)lastObject] stringByAppendingPathComponent:@「index.html」] atomically:NO]; 但現在應用程序崩潰時,我按下更新按鈕..我嘗試使用NSError但他們不會編譯它。 – mobabur94

相關問題