用户默认不保存

时间:2013-07-11 05:27:53

标签: iphone objective-c nsuserdefaults

我刚刚构建了我的第一个应用程序,只剩下一件事了。我从网址获取数据。如果该URL失败或为零,那么我想从用户默认值中获取数据。

我有一个函数可以保存用户默认值来自URL的数据

- (void)saveToUserDefaults
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:data forKey:@"data"];
    [defaults synchronize];
}

这是我的一些代码。

NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
        data = [NSData dataWithContentsOfURL:url options:0 error:nil];
        [self saveToUserDefaults];
        if(url == nil){
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            data = [defaults dataForKey:@"data"];
        }
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

我从互联网断开连接并在模拟器中运行我的应用程序,我的应用程序返回空值。

我该怎么做才能解决这个问题?

3 个答案:

答案 0 :(得分:2)

为您的方案尝试此操作

//用于检查网络的方法是否可用

  - (BOOL)isNetworkAvailable
    {
   BOOL connected;
const char *host = "www.apple.com";
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host);
SCNetworkReachabilityFlags flags;
connected = SCNetworkReachabilityGetFlags(reachability, &flags);
isConnected = NO;
isConnected = connected && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
    return isConnected;
}

}

now try to check

        NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
                data = [NSData dataWithContentsOfURL:url options:0 error:nil];

        bool networkStatus = [self isNetworkAvailable];
                if(networkStatus){
                     [self saveToUserDefaults];
                }
    else
    {
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                    data = [defaults objectForKey:@"data"];
    }
                NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

答案 1 :(得分:0)

试试这个

- (void)saveToUserDefaults
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:data forKey:@"data"];

}

NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
        data = [NSData dataWithContentsOfURL:url options:0 error:nil];
        [self saveToUserDefaults];
        if(url == nil){
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            data = [defaults objectForKey:@"data"];
        }
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

答案 2 :(得分:0)

试试这个。

NSURL * url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];

NSURLRequest * request = [NSURLRequest requestWithURL:url];

NSOperationQueue * newQueue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:request
                                   queue:newQueue
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                               dispatch_async(dispatch_get_main_queue(), ^{

                                   NSArray * array = nil;

                                   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

                                   if (error == nil) {

                                       array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

                                       [defaults setObject:array forKey:@"data"];

                                   } else {

                                       array = [defaults objectForKey:@"data"];

                                   }

                                   NSLog(@"%@", array);

                               });

                           }];