• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 06:16:05 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

好的,我正在尝试让这个应用程序显示网络错误警报代码。我添加了 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) checkNetworkStatusNSNotification *)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)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)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 selectorselector(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)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)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) checkNetworkStatusNSNotification *)notice
{
    // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    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 = [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

应用程序运行良好,没有任何错误或警告,但在没有互联网连接时不会显示警报。有人知道这是为什么吗?



Best Answer-推荐答案


您实际上并没有在任何地方显示警报。为此,您将执行类似于以下的操作:

UIAlertView *errorView = [[UIAlertView alloc] initWithTitle"Internet Unavailable" message"MESSAGE ABOUT INTERNET UNAVAILABLE HERE." delegate:nil cancelButtonTitle"Dismiss" otherButtonTitles:nil];
[errorView show];
[errorView release];

另外,重要的是要注意 viewDidLoad 在初始化后可以在 View Controller 上调用任意次数。最好在指定的初始化程序中声明任何通知观察者。

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap