gpt4 book ai didi

iphone - 在 Reachability 2.2 中使用 NSAlerts 和 NSNotifications

转载 作者:行者123 更新时间:2023-11-29 13:50:39 25 4
gpt4 key购买 nike

我在新的 iOS 4.2 项目中实现了 Apple 的 Reachability 2.2 类。我只是希望在设备失去网络连接时出现警报 View 。 (因此符合 App Store 的界面可用性要求。)我使用了 this previous SO question作为我的出发点。该应用程序似乎正在正确通知(当连接中断或恢复时),但我在 NS 警报 View 中循环。我认为我的错误一定是有点基本,但我无法捕捉到它。如果没有 NS AlertView 有更简洁的方法来做到这一点,我也对此持开放态度。我在下面的代码中省略了一些方法,但该应用程序非常简单,只有一个 ViewController。

ViewController.h :

#import <UIKit/UIKit.h>
#import "Reachability.h"

@class Reachability;

@interface ViewController : UIViewController {

IBOutlet UITextView *liveOutputTextView;
IBOutlet UITextView *textView;
Reachability* internetReachable;
Reachability* hostReachable;

}

-(IBAction)action1:(id)sender;
-(IBAction)action2:(id)sender;

-(void)textFieldDidUpdate:(id)sender;
-(void)checkNetworkStatus:(NSNotification *)notice;

@end

ViewController.m

#import "ViewController.h"
#import "Reachability.h"

@implementation ViewController

@synthesize liveOutputTextField;

- (void)checkNetworkStatus:(NSNotification *)notice

{
// called after network status changes

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

switch (internetStatus)

case NotReachable:
{
NSLog(@"The internet is inaccessible.");


UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Internet inaccessible."
message:@"Internet inaccessible."
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];

[alert show];
[alert release];

break;

}
case ReachableViaWiFi:

{
NSLog(@"Internet Connetion is UP.");


UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Internet is Up"
message:@"Internet Up"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];

break;
}


case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");

break;
}

}


NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
switch (hostStatus)

{
case NotReachable:
{
NSLog(@"A gateway to the host server is down.");


UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Host is Down"
message:@"Host Down"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];

break;

}
case ReachableViaWiFi:
{
NSLog(@"A gateway to the host server is working via WIFI.");

break;

}
case ReachableViaWWAN:
{
NSLog(@"A gateway to the host server is working via WWAN.");

break;
}
}

}



- (void)viewDidLoad {

// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];

internetReachable = [[Reachability reachabilityForInternetConnection] retain ];
[internetReachable startNotifier];


// check if a pathway to a random host exists

hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"]retain ];
[hostReachable startNotifier];

// now patiently wait for the notification


}

- (void)viewDidAppear:(BOOL)animated {

// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];


internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];

// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];

// now patiently wait for the notification

}


-(IBAction) action1:(id)sender {

// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];

internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];

// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];

// now patiently wait for the notification

// ** LEFT OUT ACTUAL CODE FOR BREVITY **

}

-(IBAction) action2:(id)sender {

// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkNetworkStatus:)
name:kReachabilityChangedNotification
object:nil];

internetReachable = [[Reachability reachabilityForInternetConnection] retain];
[internetReachable startNotifier];

// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
[hostReachable startNotifier];

// now patiently wait for the notification

// ** LEFT OUT ACTUAL CODE FOR BREVITY **

}

- (void)viewDidUnload {

// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;


[[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)dealloc {

[super dealloc];

}

最佳答案

请记住,在发送和处理通知后,需要从通知中心删除该通知。否则,只要您的可达性状态发生变化,您就会收到警报。

例如,要删除通知,您可以:

[[NSNotificationCenter defaultCenter] removeObserver:self
name:kReachabilityChangedNotification
object:nil];

如有需要记得稍后再添加。

关于iphone - 在 Reachability 2.2 中使用 NSAlerts 和 NSNotifications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4776092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com