Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use injected JavaScript to scroll UIWebView to a point

I have been reading up on this all day and simply cannot solve the problem. I am attempting to load a web page and after it is done loading automatically scroll it to a pre determined point. I have been reading tutorials such as this http://iphoneincubator.com/blog/windows-views/how-to-inject-javascript-functions-into-a-uiwebview and still no luck.

Here is my .m where the problem is occurring. I'm desperate please help! Also, it is telling me that my webViewDidFinishLoad is overwriting the instance variable but that shouldn't be a problem I believe

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *urlAddress = @"http://www.twitter.com/richrines";
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];

    // Do any additional setup after loading the view from its nib.
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
     [webView stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"  
     "script.type = 'text/javascript';"  
     "script.text = \"function myFunction() { "  
     "window.scrollTo(100,100)"
     "}\";"  
     "document.getElementsByTagName('head')[0].appendChild(script);"];  

     [webView stringByEvaluatingJavaScriptFromString:@"myFunction();"];
}
like image 300
rich Avatar asked Jul 30 '11 00:07

rich


1 Answers

Why are you doing it in such a complicated manner? This should be all you need:

[webView stringByEvaluatingJavaScriptFromString:@"window.scrollTo(100,100)"];
like image 55
Anomie Avatar answered Oct 13 '22 12:10

Anomie