1锛屼粈涔堟槸gbk鍜寀tf-8缂栫爜锛�
2锛孏BK涓嶶TF-8鐨勫尯鍒紵
1锛孏BK鐨勬枃瀛楃紪鐮佹槸鍙屽瓧鑺傛潵琛ㄧず鐨勶紝鍗充笉璁轰腑銆佽嫳鏂囧瓧绗﹀潎浣跨敤鍙屽瓧鑺傛潵琛ㄧず锛屽彧涓嶈繃涓哄尯鍒嗕腑鏂囷紝灏嗗叾鏈�楂樹綅閮藉畾鎴�1銆�
鑷充簬UTF锛�8缂栫爜鍒欐槸鐢ㄤ互瑙e喅鍥介檯涓婂瓧绗︾殑涓�绉嶅瀛楄妭缂栫爜锛屽畠瀵硅嫳鏂囦娇鐢�8浣嶏紙鍗充竴涓瓧鑺傦級锛屼腑鏂囦娇鐢�24浣嶏紙涓変釜瀛楄妭锛夋潵缂栫爜銆傚浜庤嫳鏂囧瓧绗﹁緝澶氱殑璁哄潧鍒欑敤UTF锛�8鑺傜渷绌洪棿銆�
2锛孏BK鍖呭惈鍏ㄩ儴涓枃瀛楃锛�
UTF8鏄浗闄呯紪鐮侊紝瀹冪殑閫氱敤鎬ф瘮杈冨ソ锛孶TF-8鍒欏寘鍚叏涓栫晫鎵�鏈夊浗瀹堕渶瑕佺敤鍒扮殑瀛楃銆�
GBK鏄浗瀹剁紪鐮侊紝閫氱敤鎬ф瘮UTF8宸紝涓嶈繃UTF8鍗犵敤鐨勬暟鎹簱姣擥BK澶
GB2312鏄疓BK鐨勫瓙闆嗭紝GBK鏄疓B18030鐨勫瓙闆�
GBK鏄寘鎷腑鏃ラ煩瀛楃鐨勫ぇ瀛楃闆嗗悎
鏀寔灏辫兘姝e父瑙傜湅浣犵殑鏂囧瓧鑰屼笉浼氬嚭鐜颁贡鐮併��
鐢║IWebview鎵撳紑txt鏂囦欢鏈夋椂鍊欎細鍑虹幇涔辩爜鐨勬儏鍐碉紝杩欑鎯呭喌搴旇鏄痶xt鐨勭紪鐮侀棶棰橈紝瑙e喅鏂规濡備笅锛�
webview鎵撳紑缃戦〉鏈夎繖涓変釜鏂规硶锛�
- (void)loadRequest:(NSURLRequest *)request;
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
- (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
baseUrl鏄粈涔堬紵
涔熷氨鏄痙ata涓湁涓�浜涢摼鎺ユ槸鍥剧墖锛宑ss閮芥槸澶栭儴鏂囦欢锛岀劧鍚庤繖浜涙枃浠堕渶瑕佸埌涓�涓洰褰曚笂鍘绘壘銆俠aseUrl灏辨槸杩欎釜鐩綍銆�
1锛岃缃産aseUrl
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0] ;
//鏍规嵁鑷繁鐨勫叿浣撴儏鍐佃缃紝鎴戠殑html鏂囦欢鍦╠ocument鐩綍锛岄摼鎺ヤ篃鏄湪杩欎釜鐩綍涓婂紑濮�
NSURL *baseUrl = [NSURL fileURLWithPath:documentsDir];
2锛孧IMEType鐨勯粦榄旀硶锛�
鏃㈡枃浠剁殑鎸囧畾绫诲瀷銆�
UTI鍜孧IME鐨勬蹇�
鍚屼竴绫诲瀷鏍囪瘑绗�(Uniform Type Identifier,UTI)浠h〃IOS淇℃伅鍏变韩鐨勪腑蹇冪粍浠躲�傚彲浠ユ妸瀹冪湅鎴愪笅涓�浠g殑MIME绫诲瀷銆俇TI鏄爣璇嗚祫婧愮被鍨�(姣斿鍥惧儚鍜屾枃鏈�)鐨勫瓧绗︿覆锛屼粬浠埗瀹氬摢浜涚被鍨嬬殑淇℃伅灏嗙敤浜庡叕鍏辨暟鎹璞★紝浠栦滑涓嶉渶瑕佷緷璧栦簬鑰佸紡鐨勬寚绀虹锛屾瘮濡傛枃浠舵墿灞曞悕锛孧IME绫诲瀷锛屾垨鑰呮枃浠剁被鍨嬬殑鍏冩暟鎹��
MIME(Multipurpose Internet Mail Extensions)澶氱敤閫斾簰鑱旂綉閭欢鎵╁睍绫诲瀷銆傛槸璁惧畾鏌愮鎵╁睍鍚嶇殑鏂囦欢鐢ㄤ竴绉嶅簲鐢ㄧ▼搴忔潵鎵撳紑鐨勬柟寮忕被鍨嬶紝褰撹鎵╁睍鍚嶆枃浠惰璁块棶鐨勬椂鍊欙紝娴忚鍣ㄤ細鑷姩浣跨敤鎸囧畾搴旂敤绋嬪簭鏉ユ墦寮�銆傚鐢ㄤ簬鎸囧畾涓�浜涘鎴风鑷畾涔夌殑鏂囦欢鍚嶏紝浠ュ強涓�浜涘獟浣撴枃浠舵墦寮�鏂瑰紡銆�
//鍚庣紑鍚嶅瓧绗︿覆杞寲涓篣TI瀛楃涓�
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
//浣跨敤UITypeCopyPreferredTagWithClass(),鏄疷TI瀛楃涓茶浆鍖栦负鍚庣紑鎵╁睍鍚�,杩欓噷鏄浆鎹TI瀛楃涓茶浆鍖栦负MIME绫诲瀷
CFStringRefmimeType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
//閲婃斁
CFRelease(UTI);
CFBridgingRelease(mimeType);
3锛岀紪鐮佺被鍨嬶紙textEncodingName锛�
@"GBK" 鏃bk缂栫爜鏂瑰紡
@"utf-8" 鏃tf-8缂栫爜鏂瑰紡
鍓嶄袱绉嶆柟娉曞疄鐜颁腑鏂囪В鐮佸涓嬶細
- (void)loadRequest:(NSURLRequest *)request;
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
txt鍒嗗甫缂栫爜鍜屼笉甯︾紪鐮佷袱绉嶏紝甯︾紪鐮佺殑濡俇TF-8鏍煎紡txt锛屼笉甯︾紪鐮佺殑濡侫NSI鏍煎紡txt銆傚甫缂栫爜鐨勭敤NSString鐨剆tringWithContentsOfFile鍙鍑猴紝涓嶅甫鐨勶紝鎴戜滑渚濇灏濊瘯GBK鍜孏B18030缂栫爜銆�
NSStringEncoding *useEncoding = nil;
NSString *urlString = [NSString stringWithContentsOfFile:[self.file fileDataLocalPath] encoding:useEncoding error:nil];
//鎸塯bk鐨勬柟寮忚В鐮侊紱
if (urlString) {
urlString = [NSString stringWithContentsOfFile:[self.file fileDataLocalPath] encoding:0x80000632 error:nil];
}
if (urlString) {
//灏嗚В鐮佺殑璐村埌webview涓�
[self.previewWebView loadHTMLString:urlString baseURL:nil];
} else {
//濡傛灉gbk瑙g爜涓嶆垚鍔燂紝灏濊瘯utf-8瑙g爜锛孨SUTF8StringEncoding
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:0x80000632];
NSURL *requesurl = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:requesurl];
//鐢ㄧ涓�绉嶆柟寮忓姞杞�
[self.previewWebView loadRequest:request];
}
绗笁绉嶆柟寮忥細
//鍚屼竴绫诲瀷鏍囪瘑绗�
CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, fileExtension, NULL);
CFStringRef mimeType = UTTypeCopyPreferredTagWithClass(UTI, kUTTagClassMIMEType);
CFRelease(UTI);
CFBridgingRelease(mimeType);
//GBK瑙g爜
[self.previewWebView loadData:previewData MIMEType:(__bridge NSString *)(mimeType) textEncodingName:@"GBK" baseURL:nil];
iOS涓璾tf-8鍥介檯閫氱敤缂栫爜鏈夋椂浼氬嚭鐜颁贡鐮佹儏鍐碉紝鐢╣bk鍥藉缂栫爜鍙互寰堝ソ鏀寔涓枃缂栫爜銆�
濡傛灉搴旂敤鍦ㄥ浗鍐単bk缂栫爜鐨勬柟寮忓熀鏈�氱敤锛屼絾鏄浗澶栧彈鐜闄愬埗鏄撻�犳垚涔辩爜锛寀tf鍏ㄧ悆閫氱敤鏈夋椂浼氬嚭鐜版湰鍦版敮鎸佷笉濂芥儏鍐碉紱
鍑虹幇涔辩爜鐨勫師鍥犲湪浜庝娇鐢ㄧ紪鐮佺殑涓嶅悓鐜妭涔嬮棿鏀寔鐨勭紪鐮佷笉涓�鏍枫��