0

I just built my first app and there is only one thing left to do. I am getting data from a url. If that URL fails or is nil then I would like to get the data from the User Defaults.

I have a function that saves the user defaults from the data off the URL

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

and here is some more of my code.

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];

I disconnected from the internet and ran my app in a simulator and my app returned empty values.

What can I do to fix this?

βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
user1269625
  • 3,121
  • 26
  • 79
  • 111
  • 1
    This question has nothing to do with Xcode. Xcode is the IDE you use to develop apps and this is a purely code-related question. – dandan78 Jul 11 '13 at 05:31
  • 1
    @dandan78 You are my hero! –  Jul 11 '13 at 05:33
  • 1
    @h2 lol! Actually, it was you who got me started on this. Everyone has to do their part to keep the site clean. – dandan78 Jul 11 '13 at 05:41
  • Test it on a real device – shannoga Jul 11 '13 at 05:43
  • try latest method and commend back – dhaya Jul 11 '13 at 06:12
  • Doesn't matter if you are online or not, in above code, `url` is never nil. So it's perfectly normal that its not saving to defaults. – Desdenova Jul 11 '13 at 06:14
  • @H2CO3 I've seen you remove the Xcode tag from a post where it belongs. If you are obsessed with this and doing it everywhere, you should read more carefully. – WolfLink Jul 11 '13 at 06:35
  • Also the question does not have "nothing" to do with Xcode, even if the tag is not meant for questions like this. – WolfLink Jul 11 '13 at 06:36
  • @WolfLink Why do you think so? Would this (or any such) question be different if the author used Eclipse or `emacs` and `make` instead of Xcode? Of course no. –  Jul 11 '13 at 06:47
  • @H2CO3 im not referring to this question I just felt I had to say something when you seemed ecstatic at dandan and I had an encounter with you the other day and I thought it was just one weird incident. – WolfLink Jul 11 '13 at 06:49
  • 1
    @WolfLink Unfortunately, that's not the only case. People keep mistagging questions, and they also keep having no idea about the difference between the IDE, the language, the compiler, the libraries and the application. –  Jul 11 '13 at 06:51
  • @H2CO3 Yes I see that. I looked at your profile and you have done a lot of good retagging. However, there are a few times where I strongly disagree with your retags (like my encounter the other day) but what really stuck me was the persistence with which you pursued your verdict. – WolfLink Jul 11 '13 at 06:54
  • @H2CO3 if you want to see the question it was this one http://stackoverflow.com/questions/17519314/is-iatkos-can-run-application-on-ios-device – WolfLink Jul 11 '13 at 07:01
  • @H2CO3 and while looking at your profile I found this one as well: http://stackoverflow.com/questions/17553657/how-to-get-values-from-sscanf-s – WolfLink Jul 11 '13 at 07:01

3 Answers3

3

Try this for your scenario

// method used to check network is available or not

  - (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];
dhaya
  • 1,522
  • 13
  • 21
  • You have been a big help thanks. but your code above gives me a warning `_bridge casts have no effect when not using ARC` – user1269625 Jul 11 '13 at 06:05
  • And also in xcode when I try to run my script it says build failed. – user1269625 Jul 11 '13 at 06:08
  • Undefined symbols for architecture i386: "_CFNetDiagnosticCopyNetworkStatusPassively", referenced from: -[ViewController isNetworkAvailable] in ViewController.o "_CFNetDiagnosticCreateWithURL", referenced from: -[ViewController isNetworkAvailable] in ViewController.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation) – user1269625 Jul 11 '13 at 06:11
  • import #import header and try latest method – dhaya Jul 11 '13 at 06:11
  • is `isConnected` suppose to be NSString? its not declaried – user1269625 Jul 11 '13 at 06:14
  • i put `isConnected` as bool and my app will not build `Undefined symbols for architecture i386: "_SCNetworkReachabilityCreateWithName", referenced from: -[ViewController isNetworkAvailable] in ViewController.o "_SCNetworkReachabilityGetFlags", referenced from: -[ViewController isNetworkAvailable] in ViewController.o ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation)` – user1269625 Jul 11 '13 at 06:16
  • isConnected is BOOL value. please declare in .h file – dhaya Jul 11 '13 at 06:17
  • import #import – dhaya Jul 11 '13 at 06:20
  • I have that, my app will still not build – user1269625 Jul 11 '13 at 06:21
  • jsuskeca@yahoo.ca sorry for the late response, I got tired and went to sleep. – user1269625 Jul 11 '13 at 17:52
0

try this

- (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];
dhaya
  • 1,522
  • 13
  • 21
  • 2
    So all you did is removing the call to "synchronize"? –  Jul 11 '13 at 05:34
  • not only Synchronize method.here the problem is he set value for object [defaults setObject:data forKey:@"data"]; but get into same data type method – dhaya Jul 11 '13 at 05:44
0

Try this one.

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);

                               });

                           }];
Desdenova
  • 5,326
  • 8
  • 37
  • 45