2017-11-15 3 views
2

私は現在、editor.htmlという名前のファイルを持っています。静的で、呼び出されるたびにHTMLコンテンツをWebビューに読み込みます。Xcodeを使用してHTMLファイルにファイルを書き込んだ後、Webビューに読み込むことはできますか?

私のHTMLコード(バグの修正/新機能の追加)は常に変化し続けているので、このHTMLとCSSコードをJsonレスポンスを使って動的に追加できるようにAPIがあります。

Xcodeでは、既存のeditor.htmlファイルを作成/編集して、このAPIから受け取った文字列を入力できますか?

私はテスト目的のためにこのコードを試しましたが、何も書いていませんが、ユーザーはそれがうまくいっているというコメントにかなり積極的に反応しました。

NSError *error; 
NSString *stringToWrite = @"/Users/name/Desktop/testHTML"; 
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"myfile.txt"]; 
NSString *contents = [NSString stringWithFormat:@"This is test for html"]; 

[stringToWrite WRITETOFILE:filePathにアトミック:YESエンコーディング:NSUTF8StringEncodingエラー:&エラー]。

答えて

1

ファイルがAppバンドルに含まれている場合は、まずファイルを実行時にアプリケーションのドキュメントディレクトリにコピーしてから、ファイル内のAPIからダウンロードしたコンテンツを置き換えることができます。

ファイルをアプリケーションバンドルからドキュメントディレクトリにコピーし、ファイル名とファイル拡張子をファイルタイプに置き換えます。

NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSError *error; 

NSString *dataPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"tessdata"]; 
NSLog(@"Datapath is %@", dataPath); 

// If the expected store doesn't exist, copy the default store.  
    if ([fileManager fileExistsAtPath:dataPath] == NO) 
    { 
     NSString *tessdataPath = [[NSBundle mainBundle] pathForResource:@"eng" ofType:@"traineddata"]; 
     [fileManager copyItemAtPath:tessdataPath toPath:dataPath error:&error]; 

    } 


-(NSString*) applicationDocumentsDirectory{ 
    // Get the documents directory 
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *docsDir = [dirPaths objectAtIndex:0]; 
    return docsDir; 
} 

書き込みデータファイル

NSError *error; 
NSString *stringToWrite = @"1\n2\n3\n4"; 
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"myfile.txt"]; 
[stringToWrite writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; 
+0

に、あなたは受け入れることができます&それはあなたを助けている場合答えupvote。 –

+0

ありがとうございます、可能であればObjective C相当物を提供できますか?私のプロジェクトはObj Cです。 –

+0

私は相当するobjcコードを変換していないので、変換する必要があります。 –

関連する問題