在RestKit中实现RKReachabilityObserver的最佳方法 [英] Best way to implement RKReachabilityObserver in RestKit
问题描述
我在Xcode / RestKit中编写了一个基于标签的应用程序,并尝试使用RKReachabilityObserver来确定设备上的互联网连接。
在我的应用程序中有一个单一的可达性变量(如果这是可能的),但目前我的实现是根据下面的代码,并且在我的4个选项卡上复制不工作。
View.h 如果有人提出更好的方法, p> @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];
//确定网络可用性
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(@不可达);
}
}
..
if(_networkIsAvailable == YES)
{...
我在多个视图上实现了这一点(这似乎是导致问题的原因)。
多视图应用程序的建议方法是什么?
[RKClient sharedClient] singleton已经有一个属性
if([[[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined]&&& ; [[RKClient sharedClient] isNetworkReachable]){
....
}
您还可以订阅RKReachabilityObserver通知(如果您要在可达性状态更改时采取任何操作)
[[NSNotificationCenter defaultCenter ] addObserver:self
selector:@selector(reachabilityStatusChanged :)
name:RKReachabilityDidChangeNotification object:nil];
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?
The [RKClient sharedClient] singleton already has a property for that (reachabilityObserver). Feel free to use that one.
if ([[[RKClient sharedClient] reachabilityObserver] isReachabilityDetermined] && [[RKClient sharedClient] isNetworkReachable]) {
....
}
You can also subscribe to RKReachabilityObserver notifications (if you want to take any action when reachability status changes)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityStatusChanged:)
name:RKReachabilityDidChangeNotification object:nil];
这篇关于在RestKit中实现RKReachabilityObserver的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!