0

我刚刚构建了我的第一个应用程序,只剩下一件事要做。我正在从 url 获取数据。如果该 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];

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

我能做些什么来解决这个问题?

4

3 回答 3

3

试试这个你的场景

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

  - (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];
于 2013-07-11T05:53:58.593 回答
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);

                               });

                           }];
于 2013-07-11T06:28:47.843 回答
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];
于 2013-07-11T05:31:30.430 回答