jjzjj

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

coder 2024-01-22 原文

好的,所以我正在尝试让这个应用程序显示网络错误警报代码。我添加了 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/

有关iphone - 试图让网络错误警报正确显示在 iPhone 应用程序上?的更多相关文章

  1. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  2. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  3. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  4. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  5. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  6. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  7. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  8. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  9. ruby-on-rails - 如何使用 instance_variable_set 正确设置实例变量? - 2

    我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击

  10. ruby-on-rails - Rails 应用程序中的 Rails : How are you using application_controller. rb 是新手吗? - 2

    刚入门rails,开始慢慢理解。有人可以解释或给我一些关于在application_controller中编码的好处或时间和原因的想法吗?有哪些用例。您如何为Rails应用程序使用应用程序Controller?我不想在那里放太多代码,因为据我了解,每个请求都会调用此Controller。这是真的? 最佳答案 ApplicationController实际上是您应用程序中的每个其他Controller都将从中继承的类(尽管这不是强制性的)。我同意不要用太多代码弄乱它并保持干净整洁的态度,尽管在某些情况下ApplicationContr

随机推荐