一:確認網絡環境3G/WIFI
1. 添加源文件和framework
開發Web等網絡應用程序的時候,須要確認網絡環境,鏈接狀況等信息。若是沒有處理它們,是不會經過Apple的審查的。
Apple 的 例程 Reachability 中介紹了取得/檢測網絡狀態的方法。要在應用程序程序中使用Reachability,首先要完成以下兩部:
1.1. 添加源文件:
在你的程序中使用 Reachability 只須將該例程中的 Reachability.h 和 Reachability.m 拷貝到你的工程中。以下圖:
1.2.添加framework:
將SystemConfiguration.framework 添加進工程。以下圖:
2. 網絡狀態
Reachability.h中定義了三種網絡狀態:
typedef enum {
NotReachable = 0, //無鏈接
ReachableViaWiFi, //使用3G/GPRS網絡
ReachableViaWWAN //使用WiFi網絡
} NetworkStatus;
所以能夠這樣檢查網絡狀態:
Reachability *r = [Reachability reachabilityWithHostName:@「
www.apple.com」];
switch ([r currentReachabilityStatus]) {
case NotReachable:
// 沒有網絡鏈接
break;
case ReachableViaWWAN:
// 使用3G網絡
break;
case ReachableViaWiFi:
// 使用WiFi網絡
break;
}
3.檢查當前網絡環境
程序啓動時,若是想檢測可用的網絡環境,能夠像這樣
// 是否wifi
+ (BOOL) IsEnableWIFI {
return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);
}
// 是否3G
+ (BOOL) IsEnable3G {
return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);
}
例子:
- (void)viewWillAppear:(BOOL)animated {
if (([Reachability reachabilityForInternetConnection].currentReachabilityStatus == NotReachable) &&
([Reachability reachabilityForLocalWiFi].currentReachabilityStatus == NotReachable)) {
self.navigationItem.hidesBackButton = YES;
[self.navigationItem setLeftBarButtonItem:nil animated:NO];
}
}
4. 連接狀態的實時通知
網絡鏈接狀態的實時檢查,通知在網絡應用中也是十分必要的。接續狀態發生變化時,須要及時地通知用戶:
Reachability 1.5版本
// My.AppDelegate.h
#import "Reachability.h"
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
NetworkStatus remoteHostStatus;
}
@property NetworkStatus remoteHostStatus;
@end
// My.AppDelegate.m
#import "MyAppDelegate.h"
@implementation MyAppDelegate
@synthesize remoteHostStatus;
// 更新網絡狀態
- (void)updateStatus {
self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];
}
// 通知網絡狀態
- (void)reachabilityChanged:(NSNotification *)note {
[self updateStatus];
if (self.remoteHostStatus == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil)
message:NSLocalizedString (@"NotReachable", nil)
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
// 程序啓動器,啓動網絡監視
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// 設置網絡檢測的站點
[[Reachability sharedReachability] setHostName:@"www.apple.com"];
[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];
// 設置網絡狀態變化時的通知函數
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)
name:@"kNetworkReachabilityChangedNotification" object:nil];
[self updateStatus];
}
- (void)dealloc {
// 刪除通知對象
[[NSNotificationCenter defaultCenter] removeObserver:self];
[window release];
[super dealloc];
}
Reachability 2.0版本
// MyAppDelegate.h
@class Reachability;
@interface MyAppDelegate : NSObject <UIApplicationDelegate> {
Reachability *hostReach;
}
@end
// MyAppDelegate.m
- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];
if (status == NotReachable) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""
message:@"NotReachable"
delegate:nil
cancelButtonTitle:@"YES" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// ...
// 監測網絡狀況
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
hostReach = [[Reachability reachabilityWithHostName:@"www.google.com"] retain];
hostReach startNotifer];
// ...
}
二:使用NSConnection下載數據
1.建立NSConnection對象,設置委託對象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[self urlString]]];
[NSURLConnection connectionWithRequest:request delegate:self];
2. NSURLConnection delegate委託方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
3. 實現委託方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// store data
[self.receivedData setLength:0]; //一般在這裏先清空接受數據的緩存
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
/* appends the new data to the received data */
[self.receivedData appendData:data]; //可能屢次收到數據,把新的數據添加在現有數據最後
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
// 錯誤處理
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// disconnect
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *returnString = [[NSString alloc] initWithData:self.receivedData encoding:NSUTF8StringEncoding];
NSLog(returnString);
[self urlLoaded:[self urlString] data:self.receivedData];
firstTimeDownloaded = YES;
}
三:使用NSXMLParser解析xml文件
1. 設置委託對象,開始解析
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; //或者也可使用initWithContentsOfURL直接下載文件,可是有一個緣由不這麼作:
// It's also possible to have NSXMLParser download the data, by passing it a URL, but this is not desirable
// because it gives less control over the network, particularly in responding to connection errors.
[parser setDelegate:self];
[parser parse];
2. 經常使用的委託方法
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError;
static NSString *feedURLString = @"http://www.yifeiyang.net/test/test.xml";
3. 應用舉例
- (void)parseXMLFileAtURL:(NSURL *)URL parseError:(NSError **)error
{
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
NSError *parseError = [parser parserError];
if (parseError && error) {
*error = parseError;
}
[parser release];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString*)qName attributes:(NSDictionary *)attributeDict{
// 元素開始句柄
if (qName) {
elementName = qName;
}
if ([elementName isEqualToString:@"user"]) {
// 輸出屬性值
NSLog(@"Name is %@ , Age is %@", [attributeDict objectForKey:@"name"], [attributeDict objectForKey:@"age"]);
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
// 元素終了句柄
if (qName) {
elementName = qName;
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
// 取得元素的text
}
NSError *parseError = nil;
[self parseXMLFileAtURL:[NSURL URLWithString:feedURLString] parseError:&parseError]; java
使用NSOperation和NSOperationQueue啓動多線程
在app store中的不少應用程序很是的笨重,他們有好的界面,但操做性不好,好比說當程序從網上或本地載入數據的時候,界面被凍結了,用戶只能等程序徹底載入數據以後才能進行操做。 當 打開一個應用程序時,iphone會產生一個包含main方法的線程,所用程序中的界面都是運行在這個線程之中的(table views, tab bars, alerts…),有時候咱們會用數據填充這些view,如今問 題是如何有效的載入數據,而且用戶還能自如的操做程序。方法是啓動新的線 程,專門用於數據的下載,而主線程不會由於下載數據被阻塞。 無論使用任何編程語言,在實現多線程時都是一件很麻煩的事情。更糟糕的是,一旦出 錯,這種錯誤一般至關糟糕。然而,幸運的是apple從os x10.5在這方面作了不少的改進,NSThread的引入,使得開發多線程應用程序容易多了。除此以外,它們還引入了兩個全新的 類,NSOperation和NSOperationQueue。 接下來咱們經過一個實例來剖析如何使用這兩個類實現多線程。這裏指示展現這兩個類的基本用法,固然這不是使用他們的惟一辦法。 如 果你熟悉java或者它的別的變種語言的話 ,你會發現NSOperation對象很像java.lang.Runnable接口,就像java.lang.Runnable接口那 樣,NSOperation類也被設計爲可擴展的,並且只有一個須要重寫的方法。它就是-(void)main。使用NSOperation的最簡單的方 式就是把一個NSOperation對象加入到NSOperationQueue隊列中,一旦這個對象被加入到隊列,隊列就開始處理這個對象,直到這個對 象的全部操做完成。而後它被隊列釋放。 下面的例子中,使用一個獲取網頁,並對其解析程NSXMLDocument,最後將解析獲得的NSXMLDocument返回給主線程。 PageLoadOperation.h@interface PageLoadOperation : NSOperation { NSURL *targetURL;} @property(retain) NSURL *targetURL; - (id)initWithURL:(NSURL*)url;@end PageLoadOperation.m #import "PageLoadOperation.h"#import "AppDelegate.h"@implementation PageLoadOperation@synthesize targetURL;- (id)initWithURL:(NSURL*)url;{ if (![super init]) return nil; [self setTargetURL:url]; return self;}- (void)dealloc { [targetURL release], targetURL = nil; [super dealloc]; } - (void)main { NSString *webpageString = [[[NSString alloc] initWithContentsOfURL:[self targetURL]] autorelease]; NSError *error = nil; NSXMLDocument *document = [[NSXMLDocument alloc] initWithXMLString:webpageString options:NSXMLDocumentTidyHTML error:&error]; if (!document) { NSLog(@"%s Error loading document (%@): %@", _cmd, [[self targetURL] absoluteString], error); return; } [[AppDelegate shared] performSelectorOnMainThread:@selector(pageLoaded:) withObject:document waitUntilDone:YES]; [document release]; } @end 正 如咱們所看到的那樣,這個類至關的簡單,在它的init方法中接受一個url並保存起來,當main函數被調用的時候,它使用這個保存的url建立一個字 符串,並將這個字符串傳遞給NSXMLDocumentinit方法。若是加載的xml數據沒有出錯,數據會被傳遞給AppDelegate,它處於主線 程中。到此,這個線程的任務就完成了。在主線程中註銷操做隊列的時候,會將這個NSOperation對象釋放。 AppDelegate.h @interface AppDelegate : NSObject { NSOperationQueue *queue; }+ (id)shared;- (void)pageLoaded:(NSXMLDocument*)document;@endAppDelegate.m #import "AppDelegate.h"#import "PageLoadOperation.h"@implementation AppDelegate static AppDelegate *shared; static NSArray *urlArray; - (id)init { if (shared) { [self autorelease]; return shared; } if (![super init]) return nil; NSMutableArray *array = [[NSMutableArray alloc] init];[array addObject:@"http://www.google.com"];[array addObject:@"http://www.apple.com"];[array addObject:@"http://www.yahoo.com"];[array addObject:@"http://www.zarrastudios.com"];[array addObject:@"http://www.macosxhints.com"];urlArray = array; queue = [[NSOperationQueue alloc] init];shared = self;return self; } • (void)applicationDidFinishLaunching: (NSNotification *)aNotification { for (NSString *urlString in urlArray) { NSURL *url = [NSURL URLWithString:urlString]; PageLoadOperation *plo = [[PageLoadOperation alloc] initWithURL:url]; [queue addOperation:plo]; [plo release]; } } - (void)dealloc { [queue release], queue = nil; [super dealloc]; } + (id)shared; { if (!shared) { [[AppDelegate alloc] init]; } return shared; } - (void)pageLoaded:(NSXMLDocument*)document; { NSLog(@"%s Do something with the XMLDocument: %@", _cmd, document); } @end NSOperationQueue的並行控制(NSOperationQueue Concurrency) 在 上面這個簡單的例子中,咱們很難看出這些操做是並行運行的,然而,若是你你的操做花費的時間遠遠比這裏的要長,你將會發現,隊列是同時執行這些操做的。幸 運的是,若是你想要爲隊列限制同時只能運行幾個操做,你可使用NSOperationQueue的 setMaxConcurrentOperationCount:方法。例如,[queue setMaxConcurrentOperationCount:2];