IOS开发应用中NSDATA转换为NSSTRING时乱码问题解决是本文要介绍的内容,主要是来学习乱码的解决问题,本文通过NSDATA和NSSTRING来实现乱码的解决。读取HTML内容后,返回的 NSDATA 转换为 NSSTRING 时经常出现乱码。来看详细内容讲解。
- //
- // WeatherFetcherAppDelegate.h
- // WeatherFetcher
- //
- // Created by cy on 10-8-9.
- // Copyright CY.COM 2010. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- @interface WeatherFetcherAppDelegate : NSObject <UIApplicationDelegate,UISearchBarDelegate> {
- UIWindow *window;
- IBOutlet UIWebView *wv;
- IBOutlet UISearchBar *sb;
- IBOutlet UIActivityIndicatorView *ai;
- NSMutableData *receivedData;
- NSMutableData *data1;
- NSMutableArray *elements;
- NSString *element;
- }
- @property (nonatomic, retain) IBOutlet UIWindow *window;
- @property (nonatomic, retain) IBOutlet UIWebView *wv;
- @end
- //
- // WeatherFetcherAppDelegate.m
- // WeatherFetcher
- //
- // Created by cy on 10-8-9.
- // Copyright CY.COM 2010. All rights reserved.
- //
- #import "WeatherFetcherAppDelegate.h"
- @implementation WeatherFetcherAppDelegate
- @synthesize window;
- @synthesize wv;
- #pragma mark -
- #pragma mark Application lifecycle
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- [window makeKeyAndVisible];
- [ai setHidden:YES];
- return YES;
- }
- - (void)applicationWillResignActive:(UIApplication *)application {
- /*
- Sent when the application is about to move from active to inactive state.
- This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message)
- or when the user quits the application and it begins the transition to the background state.
- Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates.
- Games should use this method to pause the game.
- */
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application {
- /*
- Use this method to release shared resources, save user data, invalidate timers,
- and store enough application state information to restore your application to its current state in case it is terminated later.
- If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
- */
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application {
- /*
- Called as part of transition from the background to the inactive state: here you can undo many of the changes
- made on entering the background.
- */
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application {
- /*
- Restart any tasks that were paused (or not yet started) while the application was inactive.
- If the application was previously in the background, optionally refresh the user interface.
- */
- }
- - (void)applicationWillTerminate:(UIApplication *)application {
- /*
- Called when the application is about to terminate.
- See also applicationDidEnterBackground:.
- */
- }
- #pragma mark -
- #pragma mark Memory management
- - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
- /*
- Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
- */
- }
- - (void)dealloc {
- [window release];
- [super dealloc];
- }
- -(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
- [sb resignFirstResponder];
- [ai setHidden:NO];
- [ai startAnimating];
- // create the request
- NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL
- URLWithString:@"http://wap.sohu.com/"]
- cachePolicy:NSURLRequestUseProtocolCachePolicy
- timeoutInterval:60.0];
- // create the connection with the request
- // and start loading the data
- NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest
- delegate:self];
- if (theConnection) {
- // Create the NSMutableData that will hold
- // the received data
- // receivedData is declared as a method instance elsewhere
- receivedData=[[NSMutableData data] retain];
- } else {
- // inform the user that the download could not be made
- }
- [ai stopAnimating];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
- {
- // this method is called when the server has determined that it
- // has enough information to create the NSURLResponse
- // it can be called multiple times, for example in the case of a
- // redirect, so each time we reset the data.
- // receivedData is declared as a method instance elsewhere
- [receivedData setLength:0];
- }
- - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
- {
- // append the new data to the receivedData
- // receivedData is declared as a method instance elsewhere
- [receivedData appendData:data];
- }
- - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- {
- // release the connection, and the data object
- [connection release];
- // receivedData is declared as a method instance elsewhere
- [receivedData release];
- }
- - (void)connectionDidFinishLoading:(NSURLConnection *)connection
- {
- NSStringEncoding strEncode = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
- NSString *strReceive = [[NSString alloc] initWithData:receivedData encoding:strEncode];
- [wv loadHTMLString:strReceive baseURL:nil];
- // release the connection, and the data object
- [connection release];
- [receivedData release];
- }
- @end
小结:IOS开发应用中NSDATA转换为NSSTRING时乱码问题解决的内容介绍完了想,希望通过本文的学习能对你有所帮助!