WEB开发网
开发学院手机开发iPhone 开发 iOS开发笔记:网络编程总结 阅读

iOS开发笔记:网络编程总结

 2010-10-13 02:04:00 来源:本站整理   
核心提示:NotReachable)) {self.navigationItem.hidesBackButton = YES;[self.navigationItem setLeftBarButtonItem:nil animated:NO];}}4. 链接状态的实时通知网络连接状态的实时检查,通知在网络应用中也是十分必要的,i
NotReachable)) {

self.navigationItem.hidesBackButton = YES;

[self.navigationItem setLeftBarButtonItem:nil animated:NO];

}

}

4. 链接状态的实时通知

网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户:

Reachability 1.5版本

// My.AppDelegate.h

#import "Reachability.h"

@interface MyAppDelegate : NSObject {

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];

上一页  1 2 3 4 5  下一页

Tags:iOS 开发 笔记

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接