gpt4 book ai didi

iphone - 试图让网络错误警报正确显示在 iPhone 应用程序上?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:27:05 26 4
gpt4 key购买 nike

好的,所以我正在尝试让这个应用程序显示网络错误警报代码。我添加了 SystemConfiguration.framework 框架和 Apple 的“Reachability”示例代码。

这是 viewcontroller.h 文件:

#import <UIKit/UIKit.h>

@class Reachability;

@interface Test_Internet_ConnectionViewController : UIViewController {

Reachability* internetReachable;
Reachability* hostReachable;
}

@property BOOL internetActive;
@property BOOL hostActive;

- (void) checkNetworkStatus:(NSNotification *)notice;

@end

这是 viewcontroller.m 文件:

#import "Test_Internet_ConnectionViewController.h"
#import "Reachability.h";

@implementation Test_Internet_ConnectionViewController

@synthesize internetActive;
@synthesize hostActive;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super 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
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)

{
case NotReachable:
{
NSLog(@"The internet is down.");
self.internetActive = NO;

UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Internet Unavailable" message:@"This page cannot load, please check your internet connection and try again." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[errorView show];
[errorView release];

break;

}
case ReachableViaWiFi:
{
NSLog(@"The internet is working via WIFI.");
self.internetActive = YES;

break;

}
case ReachableViaWWAN:
{
NSLog(@"The internet is working via WWAN.");
self.internetActive = YES;

break;

}
}

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

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

break;

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

break;

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

break;

}
}
}

- (void)dealloc {
[super dealloc];

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

@end

如果打开应用程序时没有互联网,错误会按预期工作,但如果应用程序在最初通过互联网连接打开后失去连接,错误会显示 3 次,而我显然只希望它显示一次。

我认为它可能在 Reachability 类中发送了 3 次通知,可能是在 3 个不同的地方。我不知道它为什么这样做,但我认为解决方案可能是从 Reachability 类中删除我不想要的两个调用,但我不知道该怎么做。

有人能帮帮我吗?提前致谢。

编辑:

8个警告和4个错误如下:

'TestViewController' may not respond to '+checkConnectivity'

Initialization makes integer from pointer without a cast

Local declaration of 'connectivity' hides instance variable

'MyReachability' undeclared

'reachability' undeclared

'TestViewController' may not respond to '-siteAvailable'

'TestViewController' may not respond to '-addConnectivityView'

Redefinition of '-[TestViewController reachabilityChanged:]'

'MyReachability' undeclared

TestViewController may not respond to '-siteAvailable'

TestViewController may not respond to '-addConnectivityView'

TestViewController may not respond to '-removeConnectivityView'

最佳答案

是的,它出于某种原因多次发送通知。为了克服这个问题,我在应用程序委托(delegate)中维护了一个网络状态变量,它是 kReachabilityChangedNotification 的观察者。该 View 是自定义 kViewReachabilityChangedNotification 的观察者,它在值实际更改时由应用程序委托(delegate)发送。不仅如此,在收到来自可达性的通知后,我调用了一个可达性函数来获取当前网络状态,并相应地更改了我的应用程序委托(delegate)变量。然后我可以向 View 发送通知以更新自身。

这个变通办法很丑陋,但有效。我花了很多时间来理解为什么以这种方式发送通知,最后放弃寻找这种解决方案。

编辑:这是您的示例 - 我仍然没有放置所有工作示例的好地方,因此请尝试从代码示例中找出解决方案:子类可达性:

@interface MyReachability : 可达性{}+(BOOL) 连接到网络;@结束

@implementation MyReachability

+ (BOOL) connectedToNetwork {
// Create zero addy
struct sockaddr_in zeroAddress;
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;

// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityFlags flags;

BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);

if (!didRetrieveFlags)
{
return NO;
}

BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
if (needsConnection && (flags & kSCNetworkReachabilityFlagsIsWWAN)) {
needsConnection = NO;
}
return (isReachable && !needsConnection) ? YES : NO;
}

@结束在 YourApplicationDelegate.h 中:

@interface YourApplicationDelegate :NSObject
{
BOOL connectivity;
}
- (void) reachabilityChanged:(NSNotification *) note;
- (BOOL) checkConnectivity;
@end

在 YourApplicationDelegate.m 中添加函数 initReachability调用它以初始化来自 Reachability 类的通知。

- (void) initReachability
{
MyReachability * reachability = [ MyReachability sharedReachability ];

//[ reachability setHostName: [self hostName] ];
[ reachability setNetworkStatusNotificationsEnabled: YES ];

[self siteAvailable];

NSNotificationCenter * notificationCenter = [ NSNotificationCenter defaultCenter ];
[ notificationCenter addObserver: self
selector: @selector( reachabilityChanged: )
name: @"kNetworkReachabilityChangedNotification"
object: nil ];

connectivity = [MyReachability testConnection:NO];
if (!connectivity) {
if (![MyReachability testConnection:NO]) {
[self addConnectivityView];
}
}
}


- (BOOL) checkConnectivity
{
return connectivity;
}

- (void) reachabilityChanged:(NSNotification *) note
{
BOOL newConnectivity = [MyReachability testConnection:NO];
if (connectivity != newConnectivity) {
connectivity = newConnectivity;
[self siteAvailable];
if (![MyReachability testConnection:NO]) {
[self addConnectivityView];
} else {
[self removeConnectivityView];
connectivity = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"kAppNetworkReachabilityChangedNotification" object:nil];
}
}
}

请注意,@"kAppNetworkReachabilityChangedNotification"是一个自定义通知,您的 View 应该订阅它。

在你的 View Controller 的 viewDidLoad 函数中:

-(void) viewDidLoad
{
// your customization
NSNotificationCenter * notificationCenter = [ NSNotificationCenter defaultCenter ];
[ notificationCenter addObserver: self
selector: @selector( reachabilityChanged: )
name: @"kAppNetworkReachabilityChangedNotification"
object: nil ];

}


- (void) reachabilityChanged: (NSNotification *) note
{
BOOL connectivity = [YourAppDelegate checkConnectivity];

if (connectivity) {
// update your view accordingly
}

}

关于iphone - 试图让网络错误警报正确显示在 iPhone 应用程序上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4821710/

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