Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listening for events in a UIWebView (iOS)

I'm trying to listen for an event (specifically a button click) from a webpage I have opened in a UIWebView in my iOS app. I know that in other languages there are ways to attach event listeners to certain web calls, but I haven't yet found a way to do it in Objective-C. I also haven't found anyone online who says it can't be done, so I decided I should ask. I saw in the documentation that you can make Objective-C calls from Javascript, but I do not have control of the webpage I am wanting to monitor. So I need a solution that allows me to listen for this event entirely in Objective-C.

EDIT: If specifics would help, I am trying to allow the user to make a wallpost on Facebook. I am loading the Facebook sharer page in a UIWebview (http://www.facebook.com/sharer/sharer.php?u=) and wanting to monitor when the user clicks "Share Link" so that I can close the web view.

Thank you all for your time!

like image 485
WendiKidd Avatar asked Dec 07 '11 21:12

WendiKidd


People also ask

Is UIWebView deprecated?

As of iOS 12.0, 'UIWebView' has been explicitly marked as deprecated. Apple is encouraging developers to adopt WKWebView instead, which was introduced in iOS 8.0.

What is UIWebView on Apple Iphone?

A view that embeds web content in your app.

What is iOS WKWebView?

A WKWebView object is a platform-native view that you use to incorporate web content seamlessly into your app's UI. A web view supports a full web-browsing experience, and presents HTML, CSS, and JavaScript content alongside your app's native views.

How does WebView work in iOS?

WebView can be defined as an object which can display the interactive web content and load HTML strings within the iOS application for an in-app browser. It is an instance of the WKWebView class, which inherits the UIView class.


2 Answers

You can use the UIWebViewDelegate:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

The UIWebViewNavigationType values are :

enum {
  UIWebViewNavigationTypeLinkClicked,
  UIWebViewNavigationTypeFormSubmitted,
  UIWebViewNavigationTypeBackForward,
  UIWebViewNavigationTypeReload,
  UIWebViewNavigationTypeFormResubmitted,
  UIWebViewNavigationTypeOther
};typedef NSUInteger UIWebViewNavigationType;

Can check this and then look at the NSURLRequest to get info about that

like image 177
tiltem Avatar answered Oct 27 '22 01:10

tiltem


Use a custom scheme. When the user taps the button, ping a url with the custom scheme (myScheme://buttonTapped) and either:

  1. Catch this in the webview delegate method shouldStartLoadWithRequest... (ie check if the URL contains your custom scheme) and route it to the appropriate objective c selector. Or

  2. Register a custom URL protocol and set it up to handle your custom URL scheme. Something like the below:

@implementation MyURLProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request
{
    return [[[request URL] scheme] isEqualToString:@"myScheme"];
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
{
    return request;
}

- (void)startLoading
{
    NSURLRequest * request = [self request];
    id client = [self client];

    NSData * data = [NSMutableData dataWithCapacity:0];
    NSHTTPURLResponse * response = [[[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:@"HTTP/1.1" headerFields:nil] autorelease];
    [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    [client URLProtocol:self didLoadData:data];
    [client URLProtocolDidFinishLoading:self];

    [[NSNotificationCenter defaultCenter] postNotificationName:kSchemeNotification object:nil userInfo:payload];
}

- (void)stopLoading
{

}
like image 39
Alfie Hanssen Avatar answered Oct 27 '22 00:10

Alfie Hanssen