我在Xcode/RestKit中编写了一个基于选项卡的应用程序,并试图使用RKReachabilityObserver来确定设备上的Internet连接。在RestKit中实现RKReachabilityObserver的最佳方式
理想情况下,我希望在整个应用程序中都有一个可达性变量(如果可以的话),但目前我的实现是按照以下代码实现的,并且在通过4个选项卡复制时效果不佳。
如果有人有任何更好的方法来做到这一点的建议,我会非常感谢您的意见。
View.h
@property (nonatomic, retain) RKReachabilityObserver *observer;
View.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)
{...
我有在多个视图上实现了这一点(这似乎是导致问题。
多视图应用程序的建议方法是什么?
我使用了单例,而且它完美地工作。谢谢。 – Nick 2012-01-06 18:58:49
嗨mja。有没有办法用sharedClient单例来触摸选定的主机?而且,第一次加载的时间大约是2秒。这是正常的吗? – Nick 2012-01-09 09:04:19
iOS5上有关于可达性API的问题。最好将baseURL定义为IP地址(如果可能的话),然后RestKit会使用IP来确定状态 - 这些问题与确定可达性状态时iOS处理DNS请求的方式有关。是的,可能会有一些延迟。你正在测试设备还是模拟器?如果设备,你有什么iOS(也连接 - 边缘/ 3G/WiFi) – mja 2012-01-09 10:24:24