iphone - 试图让网络错误警报显示在 iPhone 应用程序上?
<p><p>好的,我正在尝试让这个应用程序显示网络错误警报代码。我添加了 SystemConfiguration.framework 框架和 Apple 的“Reachability”示例代码。</p>
<p>这里是 viewcontroller.h 文件:</p>
<pre><code>#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
</code></pre>
<p>这里是 viewcontroller.m 文件:</p>
<pre><code>#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 = ;
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 {
;
// check for internet connection
[ addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [ retain];
;
// check if a pathway to a random host exists
hostReachable = [ retain];
;
// 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.
;
// 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 = ;
switch (internetStatus)
{
case NotReachable:
{
NSLog(@"The internet is down.");
self.internetActive = NO;
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 = ;
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 {
;
[ removeObserver:self];
}
@end
</code></pre>
<p>应用程序运行良好,没有任何错误或警告,但在没有互联网连接时不会显示警报。有人知道这是为什么吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您实际上并没有在任何地方显示警报。为此,您将执行类似于以下的操作:</p>
<pre><code>UIAlertView *errorView = [ initWithTitle:@"Internet Unavailable" message:@"MESSAGE ABOUT INTERNET UNAVAILABLE HERE." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
;
;
</code></pre>
<p>另外,重要的是要注意 viewDidLoad 在初始化后可以在 ViewController 上调用任意次数。最好在指定的初始化程序中声明任何通知观察者。</p></p>
<p style="font-size: 20px;">关于iphone - 试图让网络错误警报显示在 iPhone 应用程序上?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/4810329/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/4810329/
</a>
</p>
页:
[1]