gpt4 book ai didi

xcode - 在 RestKit 中实现 RKReachabilityObserver 的最佳方式

转载 作者:行者123 更新时间:2023-12-04 16:26:19 25 4
gpt4 key购买 nike

我在 Xcode/RestKit 中编写了一个基于选项卡的应用程序,并尝试使用 RKReachabilityObserver 来确定设备上的 Internet 连接。

理想情况下,我希望在整个应用程序中有一个可访问性变量(如果可能的话),但目前我的实现是按照下面的代码进行的,并且在我的 4 个选项卡上复制时效果不佳。

如果有人对更好的方法有任何建议,我将非常感谢您的评论。

查看.h

@property (nonatomic, retain) RKReachabilityObserver *observer;

查看.m
@interface AppViewController()
{
RKReachabilityObserver *_observer;
}
@property (nonatomic) BOOL networkIsAvailable;
@synthesize observer = _observer;

-(id)initWithCoder:(NSCoder *)aDecoder {

if ((self = [super initWithCoder:aDecoder])) {

self.observer = [[RKReachabilityObserver alloc] initWithHost:@"mydomain"];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:RKReachabilityDidChangeNotification
object:_observer];
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

// determine network availability
if (! [_observer isReachabilityDetermined]) {
_networkIsAvailable = YES;
}
else
{
_networkIsAvailable = NO;
}

_text.returnKeyType = UIReturnKeyDone;
_text.delegate = self;
}

- (void)reachabilityChanged:(NSNotification *)notification {
RKReachabilityObserver* observer = (RKReachabilityObserver *) [notification object];
if ([observer isNetworkReachable]) {
if ([observer isConnectionRequired]) {
_networkIsAvailable = YES;
NSLog(@"Reachable");
return;
}
}
else
{
_networkIsAvailable = NO;
NSLog(@"Not reachable");
}
}

然后在我看来,我只是做....
if (_networkIsAvailable == YES)
{...

我已经在多个 View 上实现了这个(这似乎是导致问题的原因。

多 View 应用程序的建议方法是什么?

最佳答案

[RKClient sharedClient] 单例已经有一个属性(reachabilityObserver)。随意使用那个。

if ([[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined] && [[RKClient sharedClient] isNetworkReachable]) {
....
}

您还可以订阅 RKReachabilityObserver 通知(如果您想在可达性状态改变时采取任何行动)
    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(reachabilityStatusChanged:)
name:RKReachabilityDidChangeNotification object:nil];

关于xcode - 在 RestKit 中实现 RKReachabilityObserver 的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8762710/

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