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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8762710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:55:37  来源:igfitidea点击:

Best way to implement RKReachabilityObserver in RestKit

xcodecocoa-touchrestkit

提问by Nick

I have written a tab based application in Xcode/RestKit and am attempting to use the RKReachabilityObserver to determine Internet connectivity on the device.

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

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.

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

If anybody has any suggestions of a better way to do this, I'd really appreciate your comments.

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

View.h

查看.h

@property (nonatomic, retain) RKReachabilityObserver *observer;

View.m

视图.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?

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

回答by mja

The [RKClient sharedClient] singleton already has a property for that (reachabilityObserver). Feel free to use that one.

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

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)

您还可以订阅 RKReachabilityObserver 通知(如果您想在可达性状态改变时采取任何行动)

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(reachabilityStatusChanged:) 
                                                 name:RKReachabilityDidChangeNotification object:nil];

回答by CTiPKA

Here is some changes in RestKit 0.20 and later. The code of reachability block should looks like:

这是 RestKit 0.20 及更高版本中的一些更改。可达性块的代码应如下所示:

    RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[RemoteTools serverUrl]];
[manager.HTTPClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    if (status == AFNetworkReachabilityStatusNotReachable) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
                                                        message:@"You must be connected to the internet to use this app."
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }
}];