window.close(); for closing UIWebView [duplicate] - iphone

Is there a way to get a callback to objective-c when a certain event has been detected in a UIWebView? Can Javascript send a callback to Objective-C?

Update - don't use UIWebView anymore. Use WKWebView, or better yet (if it fits your needs and you're building for iOS 9), a Safari View Controller.
But if you must use UIWebView, in your UIWebView delegate, provide an implementation for webView:shouldStartLoadWithRequest:navigationType:
In your HTML or Javascript files, add functions that send URLs to a custom scheme (for readability purposes, the custom scheme isn't required). All the URLs sent will be passed to your Objective-C method implementation, and then you can do what you'd like.

Just to illustrate the solution by "bpapa" with actual code:
WARNING: untested code
Implement this method in the UIWebView's delegate...
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
if ( [[[inRequest URL] scheme] isEqualToString:#"callback"] ) {
// Do something interesting...
return NO;
}
return YES;
}
...then put a link in the webwieb like this:
Click me
And it should activate your callback-code. Obviously, you could trigger it with a javascript instead of a plain link.

Related

Calling Objective-C method by clicking text inside UIWebView

I query a SQLite db and the query returns some links: I can display them in a UITextView or in a UIWebView.
What I need to know is: is there a way to bind an action to a link inside a UIWebView (or even UITextView)? I don't want to open a webpage, I just want to execute a method of Objective-C as the link was a button. Is this possible?
Yes, this is possible via JavaScript, see this doc: Calling Objective-C Methods From JavaScript.
Use the delegate method
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)aRequest navigationType:(UIWebViewNavigationType)navigationType
and check for UIWebViewNavigationTypeLinkClicked, then inspect the [aRequest URL] to determine what the link action should be.

UIWebView: open some links in Safari, some within the view

My app features content that (for text formatting reasons) is presented in an UIWebView. Within the content there are links, some of which should open their target in mobile Safari, while others should navigate within the content.
So far, I've catched the link requests using a UIWebView delegate. In my implementation of
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
I'd check the requests URL using lastPathComponent or pathComponents for known elements to determine whether to open the link externally or within the view.
However, I just found out said methods are only available since iOS 4.0, which would render the app useless on iPad. Plus I have the feeling I'm using a dirty solution here.
Is there another way to somehow "mark" the links within my content in a way that makes them easy to distinguish later, when processing the request in the delegate method?
Thanks alot!!
You could covert the URL request into a string, and do a compare for a subdirectory on your website, such as in URLs that only start with "http://www.sample.com/myapp/myappswebcontent/", against the initial substring of your URL. Anything else, send to Safari.
You should set a policy delegate of web view:
For instance in the controller, that contains a web view
[webView setPolicyDelegate:self];
and then override a decidePolicyForNavigation method (this is just an example):
- (void)webView:(WebView *)sender decidePolicyForNavigationAction: (NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id <WebPolicyDecisionListener>)listener
{
if ([[actionInformation objectForKey:WebActionNavigationTypeKey] intValue] == WebNavigationTypeLinkClicked) {
[listener ignore];
[[NSWorkspace sharedWorkspace] openURL:[request URL]];
}
else
[listener use];
}
you can distinguish there kind of link and ignore or use the listener. If you ignore it, you can open the link in safari, if you use it, the link will open in your webview.
HTH

iphone notification from javascript to objective C

I've designed my main view using HTML, CSS and some images. It looks very nice. I'm using some images that are clickable in HTML code.
On click of some image, I want to load some view accordingly.
Is it possible? If yes, please suggest me how to do it.
If tutorial or some sample code is available please share it.
Implement the following UIWebView Delegate Method and call your Objective methods
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSLog(#"LINK CLICKED......");
//return NO;
}
return YES;
}
You can look into PhoneGap for some sample code.
There are two parts. First you define a custom scheme for you application that is only used to communicate between the web page and the delegate. Could just be 'foo:' as it will be private to the page and host. You then handle this custom scheme in the delegate shouldStartLoadWithRequest method and return NO.
With a clickable link, you can just set the href to something that starts with your custom scheme. If you need to send messages at arbitrary times, you can use javascript to set the window.location to a url with your custom scheme.

iPhone architect choice for large text flow w/links

I am designing an app which will present large amounts of text that is interspersed with notes and references as clickable images. On a PC I'd use a control that shows HTML, but in the iPhone I am not able to intercept the touches of images and links too well using the UIWeb control.
Should I use a UIScroll and build the text as lables and UIImages perhaps?
Looking for the best way forward in my design phase.
I don't know what your requirements are obviously, but it is possible to capture the click on an link in a UIWebView and take some alternative action. In one of my Apps, I have a UIWebView with one particular link which I want to route differently, while I let all other links open as web pages in the UIWebView as normal. Here's the code snippet from the app which accomplishes this. It is within a UIViewController which loads the UIWebView:
- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [ request URL ];
if( [[url path] isEqualToString:#"/the_special_link.html"] ) {
// Take some alternative action and then stop the page from loading...
// (code to take some special action goes here)
return NO;
}
else {
return YES;
}
}
This is a delegate method call, so when I set up the UIWebView, which I do programmatically in the Controller loadView method, I set the WebView's delegate to that same Controller:
myWebView.delegate = self;

UIWebView didFinishLoading fires multiple times

I have some code that needs to run after the a UIWebView finishes loading a document. For that I've set the UIWebView's delegate to my controller, and implemented the webViewDidFinishLoading method.
This gets called multiple times, depending on the type of page to load. I'm not sure if it's because of ajax requests, requests for images, or maybe even iframes.
Is there a way to tell that the main request has finished, meaning the HTML is completely loaded?
Or perhaps delay my code from firing until all of those events are done firing?
You can do something like this to check when loading is finished. Because you can have a lot of content on the same page you need it.
- (void)webViewDidFinishLoad:(UIWebView *)webview {
if (webview.isLoading)
return;
// do some work
}
It could be enlightening (if you haven't gone this far yet) to NSLog a trace of load starts and finishes.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"Loading: %#", [request URL]);
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(#"didFinish: %#; stillLoading: %#", [[webView request]URL],
(webView.loading?#"YES":#"NO"));
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"didFail: %#; stillLoading: %#", [[webView request]URL],
(webView.loading?#"YES":#"NO"));
}
I just watched the calls to all three in one of my projects which loads a help page from my bundle and contains embedded resources (external css, YUI!, images). The only request that comes through is the initial page load, shouldStartLoadWithRequest isn't called for any of the dependencies. So it is curious why your didFinishLoad is called multiple times.
Perhaps what you're seeing is due to redirects, or as mentioned, ajax calls within a loaded page. But you at least should be able balance calls to shouldStartLoad and either of the other two delegate functions and be able to determine when the loading is finished.
Check this one it so simply and easy way to achieve no need to write too much code:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if ([[webView stringByEvaluatingJavaScriptFromString:#"document.readyState"] isEqualToString:#"complete"]) {
// UIWebView object has fully loaded.
}
}
This question is already solved, but I see it lacks an answer that actually explains why multiple calls to webViewDidFinishLoad are actually expected behavior
The aforementioned method is called every time the webview finishes loading a frame. From the UIWebViewDelegate protocol documentation:
webViewDidFinishLoad:
Sent after a web view finishes loading a frame.
In fact, this is also true for all the other methods that comprise the UIWebViewDelegate protocol.
Try this it will work fine
-(void)webViewDidFinishLoad:(UIWebView *)webview
{
if (webview.isLoading)
return;
else
{
// Use the code here which ever you need to run after webview loaded
}
}
This happens because the callback method is called every time a frame is done loading. In order to prevent this set the "suppressesIncrementalRendering" property of the webview to true. this will prevent the webview from rendering until the entire data is loaded into the memory. This did the trick for me
I have notice something similar and it was a confusion: I have a UITabBarController, it seems to preload all ViewControllers linked to its tabs on launching the App (in spite of showing just first_Tab_ViewController), so when several tabs have ViewController with WebView their respective webViewDidFinishLoad are called and if I have copied pasted:
NSLog(#"size width %0.0f height %0.0f", fitingSize.width, fittingSize.height);
in several, I get several output in console that appears to be a double calling when they really are single calling in two different UIWebViews.
You could check the loading and request properties in the webViewDidFinishLoad method
Possibly related to this issue is a property on UIWebView introduced in iOS6: suppressesIncrementalRendering.