Strange problem with stringByEvaluatingJavaScriptFromString not working with NSUSerDefault value…please help!

StackOverflow https://stackoverflow.com/questions/4197150

Question

I am working on a bookmarking feature for a book reader iOS app I have. The way it is setup now, the user scrolls through a view, and when they want to save, or bookmark their spot before they leave, they hit a save button on the bottom toolbar.

When they hit this save button, the saved action is called:

-(IBAction) savePlace:(id)sender{

     int pageYOffset = [[webView stringByEvaluatingJavaScriptFromString:@"window.pageYOffset"] intValue];
     NSLog(@"%d Set Y Value", pageYOffset);
     [[NSUserDefaults standardUserDefaults] setSavedSpot:pageYOffset];

}

Surprisingly enough, I have got this part working. I can hit the save button, the console will read say 200 for where I'm at on the screen, and then when I leave and come back, 200 is again printed out to the console thanks to this method that is called by NSUSerDefaults when the app loads:

- (void) setCurrentSpot:(NSUInteger)ySpot {

       NSLog(@"%d Saved Y Value", ySpot);

         [webView stringByEvaluatingJavaScriptFromString: [NSString stringWithFormat: @"window.scrollTo(0, %d);", ySpot]];
}

BUT! Nothing happens...I know for a fact that I am saving and correctly retrieving the correct Y-axis value, but when that JavaScript method is called, it won't fire.

To further complicate things, I went ahead and made a custom IBAction that accesses that same ySpot value, and used the exact same JavaScript method to move the view to position 200, and it works perfectly!

What am I missing? I don't see what is going on. Thanks

Was it helpful?

Solution

edit: misread your question,

Perhaps the method you are calling to call that javascript is being called too fast? and not giving the UIWebview time to initialize and react? if it works when you wait on the webview to come upand then call it, that is the only thing i could see.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top