views:

42

answers:

1

Hi,

I have a embedded uiwebview in my app which will in turn call a webpage. This webpage contains some data which should be saved in the user defaults of the iphone. How to achieve this ?

+1  A: 

It very much depends on what your page and data looks like... The basic approach is to use -stringByEvaluatingJavaScriptFromString: to retrieve content, e.g.:

NSString *script = @"document.getElementById('myTextInput').value";
NSString *result = [webView stringByEvaluatingJavaScriptFromString:script];
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                       result, @"MyTextInputValue",
                       // ... more?
                       nil];
[[NSUserDefaults standardUserDefaults] setObject:dict forKey:@"MyWebViewData"];

If the data is only used by scripts in the page, you could also simply use a script function that returns you one JSON string to store:

NSString *script = @"window.getJsonData()";
NSString *json   = [webView stringByEvaluatingJavaScriptFromString:script];
Georg Fritzsche