iOS涔婫BK缂栫爜_鐭ラ亾鐨勮交杞︾啛璺笉鐭ラ亾鐨勭炕灞辫秺宀�

1. 鏈嶅姟鍣ㄨ繑鍥炵殑鏄疓BK缂栫爜鐨勫瓧绗︿覆 iOS绔互UTF-8鐨勫瓧绗︿覆灞曠ず鍒癓abel涓�

NSStringEncoding enc =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSData * data = [@"GBK缂栫爜鐨勫瓧绗︿覆" dataUsingEncoding:enc];
    NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    label.text = str;

2.iOS绔� 灏咷BK缂栫爜鐨勫瓧绗︿覆浼犵粰鏈嶅姟鍣�

  • 涓�寮�濮嬪湪杩欎竴鐐逛笂 鏄柟鍚戞悶閿欎簡 瀵艰嚧鍋氫簡寰堝鏃犵敤鍔�
    鏂瑰悜閿欏湪:
鍥犱负iOS绔殑鍙傛暟 姣斿濮撳悕 瀛︽牎鍚嶇О 閮芥槸涓枃 鎶婅繖浜涘�� 浠ュ弬鏁扮殑褰㈠紡浼犵粰鍚庡彴 鎯崇潃鎶婅繖浜涘��
杞负GBK缂栫爜鐨勫瓧绗︿覆鍙戠粰鏈嶅姟鍣ㄥ氨濂戒簡
浜庢槸寮�濮嬩簡涓嬮潰鐨勬悳绱煍嶈胺姝屸灂鐧惧害
涓�鎼� 缁撴灉杩樼湡涓嶅皯
  • 绗竴 .
    iOS涔婫BK缂栫爜_鐭ラ亾鐨勮交杞︾啛璺笉鐭ラ亾鐨勭炕灞辫秺宀�_第1张图片
    Snip20180513_1.png

    涓嶅緱涓嶈寰堝鍦版柟鍗冪瘒涓�寰�

  • 绗簩.寰堝鍗氬璇寸殑閫昏緫鎰熻娌¢棶棰� 姣斿

NSString*aString =@"杩欐槸涓�涓祴璇晄tring杞珿BK鍙戦�佺殑瀛楃涓�";

NSStringEncoding   gbkEncoding=CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

NSData*aData = [aString dataUsingEncoding: gbkEncoding];

NSString*base64String = [aData base64EncodedStringWithOptions:0];

parameters[@"printcontent"] = [NSString stringWithFormat:@"T:%@",base64String];

[self.mgrPOST:followURL parameters:parameters progress:nil success:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

NSLog(@"%@",responseObject);

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

NSLog(@"err : %@",error);

}];


杩欐牱寰�鏈嶅姟鍣ㄧ鍙戦�佺殑鏁版嵁灏辨槸gbk缂栫爜鐨勪簡锛屾暟鎹簱涓啓鍏ョ殑涔熸槸涓枃浜嗭紝涔熻兘姝g‘鍖归厤涓枃浜嗐��

浣嗘槸瀵规垜鏉ヨ 鍗磋涓嶉��

  • 绗笁.鏍规嵁浠ヤ笂涓ゆ潯 鎴戠粰NSString娣诲姞浜嗕竴涓被鍒� 鐢ㄤ簬灏哫code榛樿鐨剈tf-8鐨勫瓧绗︿覆杞崲鎴怗BK缂栫爜鐨勫瓧绗︿覆
    iOS涔婫BK缂栫爜_鐭ラ亾鐨勮交杞︾啛璺笉鐭ラ亾鐨勭炕灞辫秺宀�_第2张图片
    Snip20180513_2.png

    鍒扮洰鍓� 涔熸病鏁存槑鐧� 涓婇潰閭d箞杞崲 涓轰粈涔堜笉鎴愬姛 姣斿浼犲叆@"1鐝�"鎴栬�匑"寮�鍙戝尯"绛夎浆瀹岀粨鏋滀负nil 褰撲紶鍏ユ煇浜涗腑鏂囧�兼椂 鍙堣兘鏈夎浆瀹岀殑缁撴灉

  • 绗洓.鍦℅ithub涓婃壘鎵惧惂
    iOS涔婫BK缂栫爜_鐭ラ亾鐨勮交杞︾啛璺笉鐭ラ亾鐨勭炕灞辫秺宀�_第3张图片
    Snip20180513_3.png

    鏃犲 涔熶笉琛�

涓�.鏈�缁� 杞崲浜嗘�濊矾 鎯冲埌AFN瀵硅繖鍧楁湁鏃犺缃� 涓�鎼滅储 杩樼湡鏈�

  • 绗竴. 鐩存帴浜嗗綋鎯冲埌鐨勫氨鏄竴涓嬭繖绉嶈缃� 浣嗘槸 骞朵笉琛�
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];  
NSStringEncoding gbkEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);  
manager.responseSerializer.stringEncoding = gbkEncoding;  
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/json"];  
[manager POST:TTBaseURL parameters:dictionary  
      success:^(AFHTTPRequestOperation *operation,id responseObject) {  
          if (succes) {  
              succes(responseObject);  
          }  
            
      }failure:^(AFHTTPRequestOperation *operation,NSError *error) {  
          if (fail) {  
              fail(error);  
          }  
      }];  

鍥�.缁忚繃浠ヤ笂鐨勮繖浜涙帰绱� 鏃犳灉鍚� 閫氳繃濡備笅鏂瑰紡缁堜簬OK浜�<鍟板棪杩欎箞澶� 骞朵笉鏄噾瀛楁暟 鑰屾槸鎶婃暣涓棶棰樼殑鎵�鏈夋帰绱㈣繃绋嬭褰曚竴涓� 鏂瑰悜寰堥噸瑕�!!!>

NSString * urlStr = @"https://xxxxxxxxxxxxxx";
NSString * body = [NSString stringWithFormat:@"number=%@&selectcity=%@",assistNum,city];
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [urlRequest setHTTPMethod: @"POST"];
    [urlRequest setHTTPBody:[body dataUsingEncoding:enc]];
    [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
 AFHTTPSessionManager *mgr8 = [AFHTTPSessionManager manager];
    mgr8.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/html",@"text/json",@"text/plain",@"text/text",@"text/javascript",nil];
    mgr8.requestSerializer = [AFHTTPRequestSerializer serializer];
    mgr8.responseSerializer = [AFHTTPResponseSerializer serializer];

NSURLSessionDataTask *dataTask = [mgr8 dataTaskWithRequest:urlRequest completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        //澶勭悊鎴愬姛涔嬪悗鐨勯�昏緫
    }];
    [dataTask resume];

閴翠簬鑳藉姏鏈夐檺 姘村钩涓�鑸� 鐞嗚В鏈夎涔嬪 鏈涗笉鍚濇寚鍑� 娣辫〃鎰熻阿.

你可能感兴趣的:(iOS涔婫BK缂栫爜_鐭ラ亾鐨勮交杞︾啛璺笉鐭ラ亾鐨勭炕灞辫秺宀�)