iphone - User defaults not saving -


i built first app , there 1 thing left do. getting data url. if url fails or nil data user defaults.

i have function saves user defaults data off url

- (void)savetouserdefaults {     nsuserdefaults *defaults = [nsuserdefaults standarduserdefaults];     [defaults setobject:data forkey:@"data"];     [defaults synchronize]; } 

and here more of 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 internet , ran app in simulator , app returned empty values.

what can fix this?

try scenario

// method used check network 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 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]; 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -