在通过RKObjectManager进行其余调用之后,当我没有互联网连接时,它不会加载对象。我用domain.local测试,我的WIFI是关闭的。
我知道我可以使用“reachabilityObserver”实现,但我不知道如何才能实现此目的。
我的代码:
#import "ViewInformationForm.h"
#import <RestKit/RestKit.h>
@interface User : NSObject {
NSNumber* _user_forfait;
NSNumber* _user_client_free;
NSNumber* _user_demande_portabilite;
NSNumber* _user_mail_confirm;
NSNumber* _user_mail_enregistrement_inscrption;
NSNumber* _user_mail_depart_expedition;
NSNumber* _user_mail_arrivee_expedition;
NSNumber* _user_activation;
NSNumber* _user_portabilite;
}
@property (nonatomic, retain) NSNumber* user_forfait;
@property (nonatomic, retain) NSNumber* user_client_free;
@property (nonatomic, retain) NSNumber* user_demande_portabilite;
@property (nonatomic, retain) NSNumber* user_mail_confirm;
@property (nonatomic, retain) NSNumber* user_mail_enregistrement_inscrption;
@property (nonatomic, retain) NSNumber* user_mail_depart_expedition;
@property (nonatomic, retain) NSNumber* user_mail_arrivee_expedition;
@property (nonatomic, retain) NSNumber* user_activation;
@property (nonatomic, retain) NSNumber* user_portabilite;
@end
@implementation User
@synthesize user_forfait = _user_forfait;
@synthesize user_client_free = _user_client_free;
@synthesize user_demande_portabilite = _user_demande_portabilite;
@synthesize user_mail_confirm = _user_mail_confirm;
@synthesize user_mail_enregistrement_inscrption = _user_mail_enregistrement_inscrption;
@synthesize user_mail_depart_expedition = _user_mail_depart_expedition;
@synthesize user_mail_arrivee_expedition = _user_mail_arrivee_expedition;
@synthesize user_activation = _user_activation;
@synthesize user_portabilite = _user_portabilite;
@end
@implementation ViewInformationForm;
@synthesize picker,pickerDate, forfaitNames;
@synthesize forfaitText, TextDateEnregistrement;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)LoadData {
RKObjectMapping* mapping = [RKObjectMapping mappingForClass:[User class]];
[mapping mapKeyPathsToAttributes:
@"data.user_forfait", @"user_forfait",
@"data.user_client_free", @"user_client_free",
@"data.user_demande_portabilite", @"user_demande_portabilite",
@"data.user_mail_confirm", @"user_mail_confirm",
@"data.user_mail_enregistrement_inscrption", @"user_mail_enregistrement_inscrption",
@"data.user_mail_depart_expedition", @"user_mail_depart_expedition",
@"data.user_mail_arrivee_expedition", @"user_mail_arrivee_expedition",
@"data.user_activation", @"user_activation",
@"data.user_portabilite", @"user_portabilite",
nil];
RKObjectManager *objectManager = [RKObjectManager sharedManager];
NSString* urlUID = [NSString stringWithFormat:@"/user/data?uid=%@",uuid];
RKObjectLoader *objectLoader = [objectManager objectLoaderWithResourcePath:urlUID delegate:self];
objectLoader.method = RKRequestMethodGET;
objectLoader.objectMapping = mapping;
[objectLoader send];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
User* user = [objects objectAtIndex:0];
NSString* info = [NSString stringWithFormat:
@"\n user_forfait : %@ \n"
@"user_client_free : %@",[user user_forfait], [user user_client_free]];
NSLog(@"%@",info);
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
RKLogError(@"Load of RKRequest %@ failed with error: %@", objectLoader, error);
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[RKObjectManager objectManagerWithBaseURL:gRKCatalogBaseURL];
[self LoadData];
[super viewDidLoad];
self.forfaitNames = [[NSArray alloc] initWithObjects:
@"-- Sélectionnez un forfait --", @"Forfait 19,99 €", @"Forfait 15,90 €", @"Forfait 2 €",
@"Forfait 0 €", nil];
}
错误:
2012-01-24 09:59:32.120 Free M. Stats[13438:10703] I restkit:RKLog.m:30 RestKit initialized...
2012-01-24 09:59:32.127 Free M. Stats[13438:10703] I restkit.network.reachability:RKReachabilityObserver.m:369 Network availability has been determined for reachability observer <RKReachabilityObserver: 0x8987300 host=0.0.0.0 isReachabilityDetermined=YES isMonitoringLocalWiFi=652464 reachabilityFlags=-R tc----->
2012-01-24 09:59:32.129 Free M. Stats[13438:10703] E restkit.network:RKRequest.m:464 Failed to send request to http://freemobile-stats.local/user/data?uid=b070b4f0a581cf1a16312b7bbb31353c due to unreachable network. Reachability observer = <RKReachabilityObserver: 0x8987300 host=0.0.0.0 isReachabilityDetermined=YES isMonitoringLocalWiFi=652464 reachabilityFlags=-R tc----->
2012-01-24 09:59:32.130 Free M. Stats[13438:10703] E app:ViewInformationForm.m:102 Load of RKRequest <RKObjectLoader: 0x6e69050> failed with error: Error Domain=org.restkit.RestKit.ErrorDomain Code=2 "The client is unable to contact the resource at http://freemobile-stats.local/user/data?uid=b070b4f0a581cf1a16312b7bbb31353c" UserInfo=0x6c99270 {NSLocalizedDescription=The client is unable to contact the resource at http://freemobile-stats.local/user/data?uid=b070b4f0a581cf1a16312b7bbb31353c}
感谢您的帮助。 AO。
答案 0 :(得分:2)
这是您在RestKit中注册可达性通知的方式:
// Register for changes in network availability
NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
[center addObserver:self selector:@selector(reachabilityDidChange:) name:RKReachabilityDidChangeNotification object:nil];
在这里你会收到通知:
- (void)reachabilityDidChange:(NSNotification *)notification {
RKReachabilityObserver* observer = (RKReachabilityObserver *) [notification object];
RKReachabilityNetworkStatus status = [observer networkStatus];
if (RKReachabilityNotReachable == status) {
RKLogInfo(@"No network access!");
} else if (RKReachabilityReachableViaWiFi == status) {
RKLogInfo(@"Online via WiFi!");
} else if (RKReachabilityReachableViaWWAN == status) {
RKLogInfo(@"Online via Edge or 3G!");
}
}
答案 1 :(得分:1)
您甚至不必注册Restkit可达性通知系统,因为它已经在RKRequest方法中实现。
因此,如果没有检测到互联网连接,您的请求将失败,并且会调用此委托(您可能已经实施过):
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError *)error {
// No internet !
}
请注意,也可以出于其他原因调用此委托,例如数据映射出错时。但对于简单的情况,它应该适合你想做的事情!
答案 2 :(得分:0)
显然,如果没有互联网连接,它将不会从互联网上加载数据。您可能希望尝试其中一个预先实现的Reachability类,例如:http://developer.apple.com/library/ios/#samplecode/Reachability/Listings/Classes_Reachability_h.html#//apple_ref/doc/uid/DTS40007324-Classes_Reachability_h-DontLinkElementID_5