Javascript return a string to objective c - objective-c

i got a webview and i would like to capture an input from a webview HTML form. i have tried but i cant can some one help me please
this is my webview code
// Init Web View
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[self addSubview:webView];
[webView release];
return self;
To get value from webView, i used below code.
NSString* value = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementById('text').value"]‌​;
NSLog(#"got: %#",value);

You could look into how phonegap http://phonegap.com/ solves this.

Related

Why does createWebviewWithConfiguration not change the displayed view when window.open is called?

I'm quite new to WKWebView and am struggling with this problem where my current view doesnt change when invoking windows.open from javascript see below...
function logo_click() {
window.open(some_valid_url);//url is valid www.google.com for example
}
I'm quite sure that this function is hit since I stepped on it using Safari Tech Preview. And I'm even getting the breakpoint in my createWebviewWithConfiguration() function in xcode obj-c when logo_click is invoked,
-(WKWebView*)webView:(WKWebView )webView createWebViewWithConfiguration:(nonnull WKWebViewConfiguration )inConfig forNavigationAction:(nonnull WKNavigationAction )navigationAction windowFeatures:(nonnull WKWindowFeatures )windowFeatures
{
_webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:inConfig];
if (!navigationAction.targetFrame.isMainFrame) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self.webView loadRequest:navigationAction.request];
//[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.google.com"]]];
}
return _webView;
}
I even tried hardcoding google.com to see if it can navigate from there but my displayed view is still the previous one. I already checked the tutorial on
iphonedevsdk.com/forum/iphone-sdk-development/122635-wkwebview-wont-open-external-links.html
but my view still doesn't change. What could I be missing?
I solved it using the following code...
-(WKWebView*)webView:(WKWebView *)webView createWebViewWithConfiguration:(nonnull WKWebViewConfiguration *)inConfig forNavigationAction:(nonnull WKNavigationAction *)navigationAction windowFeatures:(nonnull WKWindowFeatures *)windowFeatures
{
[_webView removeFromSuperview];
_webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:inConfig];
if (!navigationAction.targetFrame.isMainFrame) {
//[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
NSURLRequest* req = navigationAction.request;
[self.webView loadRequest:req];
}
_webView.navigationDelegate = self;
_webView.UIDelegate = self;
_webView.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height);
[self.view addSubview:_webView];
return _webView;
}

Override tap gesture in tttaributredlabel link

In my application , I have a tttattributedlabel detect link and open in web view . if I set uitapgesture on this attributed label then gesture is always fire .
link is not working in any case if tap gesture set on attributed label .
You can try with it.
NSString *pathStr=#"http://stackoverflow.com/questions/29427320/override-tap-gesture-in-tttaributredlabel-link?noredirect=1#comment47025717_29427320";
if ([pathStr hasPrefix:#"http"]) {
UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
myWebView.autoresizesSubviews = YES;
myWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSURL *myUrl = [NSURL URLWithString:pathStr];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:myUrl];
[myWebView loadRequest:myRequest];
myWebView.delegate=self;
[self.view addSubview: myWebView];
}

iOS: Force UIWebView to reload / prevent caching

I have an iPhone app that shows a website. This website changes it's content from time to time but my app doesn't reload it every time it is opened. I tried to prevent the caching of the site, but without success.
This is my init method:
- (id)init:(NSString *)url withTitle:(NSString *)theTitle withPageName:(NSString *)pageName {
self = [super init];
if(self) {
NSLog(#"websiteviewcontroller init");
self.title = theTitle;
self.url = url;
self.pageName = pageName;
CGRect frame = [[UIScreen mainScreen] applicationFrame];
webView = [[UIWebView alloc] initWithFrame:frame];
NSURL *theURL = [NSURL URLWithString:url];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
// support zooming
webView.scalesPageToFit = YES;
//[[NSURLCache sharedURLCache] removeAllCachedResponses];
[webView reload];
}
return self;
}
This is my viewWillAppear-Method:
- (void)viewWillAppear:(BOOL)animated {
CGRect frame = [[UIScreen mainScreen] applicationFrame];
webView = [[UIWebView alloc] initWithFrame:frame];
NSURL *theURL = [NSURL URLWithString:url];
[webView loadRequest:[NSURLRequest requestWithURL:theURL]];
[webView reload];
// support zooming
webView.scalesPageToFit = YES;
}
Please help me to solve this problem. All answers are much appreciated.
This code snipplet fixed my problem:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
The answer is just for the next question. For a ore complete answer:
UIwebview without Cache

How to tell when UIWebView has finished displaying content

I'm making a UIWebView load a pdf like this:
UIWebView *pdfView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];
pdfView.delegate = self;
[pdfView loadRequest:request];
And then doing some stuff with the view once it loads:
- (void)webViewDidFinishLoad:(UIWebView*)sender {
[sender takeScreenshot]; // my UIView category screenshot method
[sender release];
}
It all works, apart from the fact that at the point webViewDidFinishLoad is called, the view hasn't displayed anything yet, it displays a few milliseconds later. So is there any way to tell when the UIWebView has finished displaying its content (a pdf in this case)?
You could do:
[self performselector:#selector(getScreenshot) withObject:sender afterDelay:0.1];
-(void) getScreenshot:(UIWebView *)webView {
[webView takeScreenshot];
[webView release];
}
Or something similar.

How to zoom webView with pdf file

How to get zoom in UIWebView.
I loaded pdf file from resource bundle in a UIWebView.
Now trying to zoom the webview. so its will get zooming effect to the pdf file.
So.
How can i zoom the UIWebView.
Any sample example. Please help me out.
Just use the following code:
self.pdfviewwebview.scalesPageToFit = YES;
self.pdfviewwebview.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.pdfviewwebview.delegate = self;
The most tricky thing here which most people forget is to set the webview's delegate,so take care about that.
UIWebView *tempWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, self.view.bounds.size.height)];
tempWebView.backgroundColor = [UIColor whiteColor];
**tempWebView.scalesPageToFit = YES; //this enables the zooming**
tempWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
tempWebView.delegate = self;
[self.view addSubview:tempWebView];
[self setWebView:tempWebView];
NSString *path = [[NSBundle mainBundle] pathForResource:#"your_file" ofType:#"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[self.webView loadRequest:request];
A pinch-to-zoom gesture can be simulated by holding down the Option key. On your simulator, two grey circles will appear indicating that its simulating a multi-touch, pinch-to-zoom gesture.