How to know UIWebView has finished loading - objective-c

how can I check when user clicks and page has loaded? Delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
Not working..

When the user taps a link, the UIWebView's delegate is sent the message webView:shouldStartLoadWithRequest:navigationType: and when the content is done loading, it sends the message webViewDidFinishLoad:.
Make sure you've set the delegate property on the web view.

Related

OSX - Disable External Site Linking for Webview

I'm working to view a local html page inside a WebView and I want to disable going to any external website if the user clicks any button within the page i.e. <a href="www.google.com">
You want to set a policyDelegate for your view. See the docs for WebPolicyDelegate. The specific method you want to implement is:
- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request
frame:(WebFrame *)frame
decisionListener:(id<WebPolicyDecisionListener>)listener
The simplest thing to do is just call [listener ignore] for everything. That will also prevent any back/forward navigation, reloads, or form submits from working. If you want more control, you can look at the actionInformation dictionary and check the type before calling [listener ignore] or [listener use].

UIPageViewController cancel page swipe

I have a UIPageViewController with 7 pages. The user must enter all the required fields in each page before going to the next page. If the user doesn't enter a required field i should display a warning popup and keep the user in the same page if he tries to go to the next page. Does anyone know how i can cancel page swipe and keep the user in the same page if the user doesn't enter a required field?
Thanks,
Anand.
If you have a datasource for the pageviewcontroller, you can return nil for the method:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
You are probably setting all viewcontrollers from start, but to do this you will need to set the initial viewcontroller, and provide the next and previous viewcontrollers from the datasource methods.
It will not disable the scrolling in the scrollview, meaning the user can scroll and 'bounce' against last page.
EDIT:
Please note that UIPageViewController does cache answers from it's datasource; When you return an answer for a given index, it will not ask for it again till scroll goes sufficiently far or you manually set the viewControllers.
For your refresh to work, as a workaround, you could call:
pageViewcontroller setViewControllers:[pageViewcontroller viewControllers] direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
to make it drop the caches and request the controller from your datasource again. (Thanks Douglas Hill)

Redirection of alert box button

I would like to know if we can actually redirect the alert box to a specific view. Meaning that when they clicked on "View" which is on the notification alert, it will redirect them to a particular view, just like the text message notification pop up. Is there any idea on how this works?
From your question, you could mean two types of alert dialogs:
The generic "alert box" you mention, or UIAlertView
A UILocalNotification alert dialog, shown when the application is in the background ("just like the text message notification pop up")
I will address them in order.
First, how to handle a UIAlertView "View" button click.
Implement the alertView:didDismissWithButtonIndex: method of the UIAlertViewDelegate protocol in your controller class, and when you init the UIAlertView set its delegate to self. Then when the user clicks a button marked e.g. "View", do this:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:#"View"])
{
// take the user to a specific view
} else { // handle other cases if you have any
}
}
Secondly, how to handle a UILocalNotification which triggers an application launch.
Apple docs on UILocalNotification state:
If the notification is an alert and the user taps the action button (or, if the device is locked, drags open the action slider), the application is launched. In the application:didFinishLaunchingWithOptions: method the application delegate can obtain the UILocalNotification object from the passed-in options dictionary by using the UIApplicationLaunchOptionsLocalNotificationKey key. The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly.
On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.
You need to write code for handling this launch case in your app delegate class, in the application:didFinishLaunchingWithOptions: method.
IF you happen to get a UILocalNotification while the app is running, Apple docs state:
If the application is foremost and visible when the system delivers the notification, no alert is shown, no icon is badged, and no sound is played. However, the application:didReceiveLocalNotification: is called if the application delegate implements it. The UILocalNotification instance is passed into this method, and the delegate can check its properties or access any custom data from the userInfo dictionary.
EDIT: To take the user to a specific view straight away, you can manually push something onto a UINavigationController stack (if your app usually operates with navigation controllers, it makes sense to do this), or present a modal view controller. I've linked there to guides for both.

Stop UIPopover from dismissing automatically

I was wondering if there was a way to stop an iPad popover from dismissing automatically whenever you touch the screen outside the popover? If not, is there some kind of method similar to "popoverDidDismiss" that I could call to tell when the popover was dismissed?
Yes you can. This is right out of the Apple documentation.
When a popover is dismissed due to user taps outside the popover view, the popover automatically notifies its delegate of the action. If you provide a delegate, you can use this object to prevent the dismissal of the popover or perform additional actions in response to the dismissal. The popoverControllerShouldDismissPopover: delegate method lets you control whether the popover should actually be dismissed. If your delegate does not implement the method, or if your implementation returns YES, the controller dismisses the popover and sends a popoverControllerDidDismissPopover: message to the delegate.
Just return NO to the delegate method popoverControllerShouldDismissPopover:
Here is a link for further reading.
Popover Guide
- (BOOL) popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
return NO;
}
That does it for you and you may assign a specific bar button item or something else in your popover to dismiss the popover.
even u can use
self.modallnpopover = yes;
if you want to dismiss it in a particular view
self.modallnpopover = no;
if you dont want to dismiss it

Intercept unused tap events in a UIWebView

I'm working on an iPad app which is based on a UIWebView: to explain it in the simplest possible terms, the app shows one big interactive webview and in addition it supports custom gestures.
I need to catch events that represent single taps on the webview, but only when the taps have not already been consumed by the webview (i.e. they are not the beginning of a scroll/zoom operation, they are not taps on links, they are not taps that trigger some javascript).
The UIWebView is very greedy with its events, and in my experience it tends not to propagate them, even when they are not consumed. To catch the events, I ended up subclassing the main UIWindow (see http://mithin.in/2009/08/26/detecting-taps-and-events-on-uiwebview-the-right-way/). This is working well, but the problem is I'm not able to recognize whether the taps I'm getting have triggered some javascript in the webview or not.
As an additional restriction, I have no control over the javascript that's going to run in the UIWebView or the HTML that it's going to be displayed.
So, the question goes like this: what would be a way to detect all and only the tap events which did not trigger any other action in the UIWebView itself, especially javascript actions?
~
Should you be curious, the source code of the project I'm working on is on GitHub: to find it, just ask Google to point you to Baker Framework.
Do this with JavaScript. First, after your webview finishes loading, attach a javascript click handler to the body to soak up any unused clicks. Have it load a made-up domain as it's action:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *script = #"document.body.onclick = function () { document.location.href = 'http://clickhandler'; }"
[webView stringByEvaluatingJavaScriptFromString:script];
}
Then in your webview delegate, look out for attempts to load that made up domain and intercept them. You now have a way to intercept that javascript click handler with native code and react accordingly:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.host isEqualToString:#"clickhandler"])
{
//handle click
//your logic here
return NO;
}
return YES;
}