IOS开发应用中NSDATA转换为NSSTRING时乱码问题解决

移动开发 iOS
IOS开发应用中NSDATA转换为NSSTRING时乱码问题解决是本文要介绍的内容,主要是来学习乱码的解决问题,本文通过NSDATA和NSSTRING来实现乱码的解决。

IOS开发应用中NSDATA转换为NSSTRING乱码问题解决是本文要介绍的内容,主要是来学习乱码的解决问题,本文通过NSDATANSSTRING来实现乱码的解决。读取HTML内容后,返回的 NSDATA 转换为 NSSTRING 时经常出现乱码。来看详细内容讲解。

  1. //  
  2. //  WeatherFetcherAppDelegate.h  
  3. //  WeatherFetcher  
  4. //  
  5. //  Created by  cy on 10-8-9.  
  6. //  Copyright CY.COM 2010. All rights reserved.  
  7. //  
  8. #import <UIKit/UIKit.h> 
  9.  
  10. @interface WeatherFetcherAppDelegate : NSObject <UIApplicationDelegate,UISearchBarDelegate> {  
  11. UIWindow *window;  
  12. IBOutlet UIWebView *wv;  
  13. IBOutlet UISearchBar *sb;  
  14. IBOutlet UIActivityIndicatorView *ai;  
  15. NSMutableData *receivedData;  
  16. NSMutableData *data1;  
  17. NSMutableArray *elements;  
  18. NSString *element;  
  19. }  
  20.  
  21. @property (nonatomic, retain) IBOutlet UIWindow *window;  
  22. @property (nonatomic, retain) IBOutlet UIWebView *wv;  
  23.  
  24. @end  
  25.  
  26. //  
  27. //  WeatherFetcherAppDelegate.m  
  28. //  WeatherFetcher  
  29. //  
  30. //  Created by  cy on 10-8-9.  
  31. //  Copyright CY.COM 2010. All rights reserved.  
  32. //  
  33.  
  34. #import "WeatherFetcherAppDelegate.h"  
  35.  
  36. @implementation WeatherFetcherAppDelegate  
  37.  
  38. @synthesize window;  
  39. @synthesize wv;  
  40.  
  41. #pragma mark -  
  42. #pragma mark Application lifecycle  
  43.  
  44. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {      
  45.  
  46. // Override point for customization after application launch.  
  47.  
  48.     [window makeKeyAndVisible];  
  49. [ai setHidden:YES];  
  50. return YES;  
  51. }  
  52.  
  53. - (void)applicationWillResignActive:(UIApplication *)application {  
  54.     /*  
  55.      Sent when the application is about to move from active to inactive state. 
  56. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) 
  57. or when the user quits the application and it begins the transition to the background state.  
  58.      Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. 
  59. Games should use this method to pause the game.  
  60.      */  
  61. }  
  62.  
  63. - (void)applicationDidEnterBackground:(UIApplication *)application {  
  64.     /*  
  65.      Use this method to release shared resources, save user data, invalidate timers, 
  66. and store enough application state information to restore your application to its current state in case it is terminated later.   
  67.      If your application supports background execution, called instead of applicationWillTerminate: when the user quits.  
  68.      */  
  69. }  
  70.  
  71. - (void)applicationWillEnterForeground:(UIApplication *)application {  
  72.     /*  
  73.      Called as part of  transition from the background to the inactive state: here you can undo many of the changes 
  74. made on entering the background.  
  75.      */  
  76. }  
  77.  
  78. - (void)applicationDidBecomeActive:(UIApplication *)application {  
  79.     /*  
  80.      Restart any tasks that were paused (or not yet started) while the application was inactive. 
  81. If the application was previously in the background, optionally refresh the user interface.  
  82.      */  
  83. }  
  84.  
  85. - (void)applicationWillTerminate:(UIApplication *)application {  
  86.     /*  
  87.      Called when the application is about to terminate.  
  88.      See also applicationDidEnterBackground:.  
  89.      */  
  90. }  
  91.  
  92. #pragma mark -  
  93. #pragma mark Memory management  
  94.  
  95.  
  96. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {  
  97.     /*  
  98.      Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.  
  99.      */  
  100. }  
  101.  
  102. - (void)dealloc {  
  103.     [window release];  
  104.     [super dealloc];  
  105. }  
  106.  
  107. -(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{  
  108.  
  109. [sb resignFirstResponder];  
  110. [ai setHidden:NO];  
  111. [ai startAnimating];  
  112. // create the request     
  113. NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL   
  114.     URLWithString:@"http://wap.sohu.com/"]     
  115.   cachePolicy:NSURLRequestUseProtocolCachePolicy   
  116.   timeoutInterval:60.0];    
  117.  
  118. // create the connection with the request     
  119. // and start loading the data     
  120. NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest     
  121.     delegate:self];     
  122.  
  123. if (theConnection) {     
  124. // Create the NSMutableData that will hold     
  125. // the received data     
  126. // receivedData is declared as a method instance elsewhere     
  127. receivedData=[[NSMutableData data] retain];     
  128. } else {     
  129. // inform the user that the download could not be made     
  130. }     
  131. [ai stopAnimating];  
  132.  
  133. }  
  134. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response     
  135. {     
  136. // this method is called when the server has determined that it     
  137. // has enough information to create the NSURLResponse     
  138. // it can be called multiple times, for example in the case of a     
  139. // redirect, so each time we reset the data.     
  140. // receivedData is declared as a method instance elsewhere     
  141.     [receivedData setLength:0];     
  142. }     
  143.  
  144. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data     
  145. {     
  146. // append the new data to the receivedData     
  147. // receivedData is declared as a method instance elsewhere     
  148.     [receivedData appendData:data];     
  149. }     
  150. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error     
  151. {     
  152. // release the connection, and the data object     
  153. [connection release];     
  154. // receivedData is declared as a method instance elsewhere     
  155.     [receivedData release];     
  156.  
  157. }     
  158.  
  159. - (void)connectionDidFinishLoading:(NSURLConnection *)connection     
  160. {     
  161.  
  162. NSStringEncoding strEncode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);  
  163.     NSString *strReceive = [[NSString alloc] initWithData:receivedData encoding:strEncode];  
  164.  
  165.     [wv loadHTMLString:strReceive baseURL:nil];  
  166.  
  167. // release the connection, and the data object     
  168.     [connection release];     
  169.     [receivedData release];     
  170. }     
  171. @end 

小结:IOS开发应用中NSDATA转换为NSSTRING乱码问题解决的内容介绍完了想,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2009-06-19 11:16:14

java web中文乱码

2009-06-30 14:02:00

Struts乱码Eclipse

2014-04-21 15:59:59

iOS 7.1企业应用证书无效

2013-06-14 10:48:53

IIS 7

2011-02-23 13:48:05

Web

2011-04-25 13:06:38

EclipseLinux

2011-06-14 13:41:27

muleWSDL

2010-04-13 14:25:24

Oracle问题解决

2009-03-06 10:10:00

广播风暴网络

2011-06-13 16:16:32

Qt 中文问题

2011-06-27 16:44:59

Qmake

2010-01-05 10:02:56

LinuxRAID常见问题

2010-08-04 10:20:30

Flex组件开发

2010-04-28 18:01:15

Unix系统

2010-06-17 11:35:24

Ubuntu 修复Gr

2010-04-28 19:24:17

Hp unix

2010-09-07 09:08:03

DIV弹出层

2010-05-05 10:25:24

Unix操作系统

2011-11-28 22:45:19

Nginxsession

2009-12-28 10:56:45

WPF Image
点赞
收藏

51CTO技术栈公众号