iOS涓В鐮�.txt鏂囦欢鍦║IWebView鍑虹幇涓枃涔辩爜鐨勯棶棰�

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鐨勬蹇�


iOS涓В鐮�.txt鏂囦欢鍦║IWebView鍑虹幇涓枃涔辩爜鐨勯棶棰�_第1张图片
uti.png

鍚屼竴绫诲瀷鏍囪瘑绗�(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鍏ㄧ悆閫氱敤鏈夋椂浼氬嚭鐜版湰鍦版敮鎸佷笉濂芥儏鍐碉紱

鍑虹幇涔辩爜鐨勫師鍥犲湪浜庝娇鐢ㄧ紪鐮佺殑涓嶅悓鐜妭涔嬮棿鏀寔鐨勭紪鐮佷笉涓�鏍枫��

你可能感兴趣的:(iOS涓В鐮�.txt鏂囦欢鍦║IWebView鍑虹幇涓枃涔辩爜鐨勯棶棰�)