2011年4月25日

UIWebView local file注意事項

不是什麼大問題,卡了關留個紀錄
1.加入檔案時,選Create folder reference for any added folders才會建立相對資料夾
否則雖然看到有個資料夾,build到機器上檔案還是全解在根目錄



2.load網頁的方法
下面混用了不同兩種的載入方式,嘗試把可能的寫法都包上去:
UIWebView *displayWebView=[[UIWebView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

//用loadData:MIMEType:textEncodingName:baseURL:
NSString *resPath=[[NSBundle mainBundle]pathForResource:@"demo_1" ofType:Nil];
[displayWebView loadData:[NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@/index.html",resPath]] MIMEType:@"text/html" textEncodingName:@"utf-8" baseURL:[NSURL fileURLWithPath:resPath]];

//用loadHTMLString:baseURL:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Safari Technology Demo - 360°.html" ofType:nil];
NSString *resPath=[[NSBundle mainBundle] resourcePath];
//如果URLWithString讀入的網頁字串有空白或斜線,要預處理
resPath =[resPath stringByReplacingOccurrencesOfString:@"/" withString:@"//"];
resPath =[resPath stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
[displayWebView loadHTMLString: [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil] baseURL:[NSURL URLWithString:[NSString stringWithFormat:@"file:/%@//",resPath]]];

3.NSURL兩個函式
Local file還是應該用fileURLWithPath比較恰當,也可省去不必要的字元處理
連網路上的網頁或是一些自己寫成NSString的html就用URLWithString了
然後path盡量都給給完整路徑

Apple HTML 5的demo source還真不好下載...

沒有留言: