0

I want to call javascript functions from my objective-c. This for iphone. I understand how the call to the javascript function is done with this line

NSString *returnValue = [webView stringByEvaluatingJavascriptFromString:method];

But I dont actually want a webview to be seen or used as part of the UI. So can I just create an instance of it right before calling the method above in some way.

I have tried:

UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectZero];
[webView loadRequest:[NSUrLRequest requestWithUrl:url]];

but nothing seems to happen. So is there a method similar to the above that I am meant to use.

1
  • Adding it to the view is not the main issue. See my answer for the full solution. Commented Jul 9, 2013 at 10:18

2 Answers 2

1

add the webview in your controller's view in order to run the javascript. it won't be visible since it has zero height and width.

Sign up to request clarification or add additional context in comments.

7 Comments

I added it to the view before thinking something like that would happen but I got the error "application windows are expected to have a root view controller"
where did you try to add it? you should do this inside a view controller. [self addSubview:web view];
I must have done something wrong earlier as I tried it again and it worked this time. So thanks for the help.
Sorry slight follow up question but my returnValue is not taking a NSString as the result. Do you know why this might be?? In the console it says @<variable is not NSString>
Return Value: The result of running script or nil if it fails. Cross check it. it might be nil.
|
0

Your issue is not related to the size of the UIWebView. It is simply a timing issue. The following code works as desired with a zero rect UIWebView instance. Note the use of UIWebViewDelegate method webViewDidFinishLoad:. UIWebView is slow and it takes time for the view to load its content and be prepared to accept JavaScript commands.

@interface NGViewController () <UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *hiddenWebView;
@end

@implementation NGViewController

@synthesize hiddenWebView = _hiddenWebView;

- (void)viewWillLayoutSubviews
{
  [super viewWillLayoutSubviews];

  if (!self.hiddenWebView.superview)
  {
    [self.view addSubview:self.hiddenWebView];
  }
}

- (UIWebView *)hiddenWebView
{
  if (!_hiddenWebView) {
    _hiddenWebView = [UIWebView.alloc initWithFrame:CGRectZero];
    _hiddenWebView.delegate = self;
    [_hiddenWebView loadHTMLString:@"<html><head><script>window.james = 'My name';</script></head><body><h1>Hidden Web View by James</h1></body></html>" baseURL:nil];
  }
  return _hiddenWebView;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
  NSString *documentBodyString = [self.hiddenWebView stringByEvaluatingJavaScriptFromString:@"window.james;"];
  NSLog(@"The body's inner HTML is: %@", documentBodyString);
}

@end

The output of which is:

2013-07-09 03:13:29.347 WebViewTest[14261:c07] The body's inner HTML is: My name

2 Comments

cannot I not just use webViewDidFinishLoad instead of manually delaying??
@walsh06 — did you try it? The point is you have a timing issue, and I've provided you with a working example. I've updated the answer to show that delegate method also works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.