在 RestKit 中实现 RKReachabilityObserver 的最佳方式
我在 Xcode/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)
{...
我已经在多个视图上实现了这个(这似乎导致了问题。
什么是多视图应用程序的建议方法吗?
I have written a tab based application in Xcode/RestKit and am attempting to use the RKReachabilityObserver to determine Internet connectivity on the device.
Ideally I'd like to have a single reachability variable throughout my application (if this is possible) but currently my implementation is as per the code below and does not work very well when replicated over my 4 tabs.
If anybody has any suggestions of a better way to do this, I'd really appreciate your comments.
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");
}
}
then anywhere in my view, I simply do....
if (_networkIsAvailable == YES)
{...
I have implemented this over multiple views (which seems to be causing the problem.
What is the suggested approach for a multiple-view application?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
[RKClient sharedClient] 单例已经有一个属性(reachabilityObserver)。请随意使用那个。
您还可以订阅 RKReachabilityObserver 通知(如果您想在可达性状态发生变化时采取任何操作)
The [RKClient sharedClient] singleton already has a property for that (reachabilityObserver). Feel free to use that one.
You can also subscribe to RKReachabilityObserver notifications (if you want to take any action when reachability status changes)
以下是 RestKit 0.20 及更高版本中的一些更改。
可达性块的代码应如下所示:
Here is some changes in RestKit 0.20 and later.
The code of reachability block should looks like: