Wen*_*idd 9 objective-c event-handling uiwebview ios
我正在尝试从我在iOS应用程序中的UIWebView中打开的网页上收听事件(特别是按钮点击).我知道在其他语言中有一些方法可以将事件监听器附加到某些Web调用,但我还没有找到在Objective-C中执行此操作的方法.我也没有在网上发现任何人说不能这样做,所以我决定应该问.我在文档中看到你可以通过Javascript进行Objective-C调用,但是我无法控制我想监视的网页.所以我需要一个允许我完全在Objective-C中监听这个事件的解决方案.
编辑:如果细节有帮助,我试图让用户在Facebook上制作一个wallpost.我正在UIWebview(http://www.facebook.com/sharer/sharer.php?u=)中加载Facebook共享页面,并希望监视用户何时单击"共享链接"以便我可以关闭Web视图.
谢谢大家的时间!
til*_*tem 11
您可以使用UIWebViewDelegate:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
Run Code Online (Sandbox Code Playgroud)
UIWebViewNavigationType值为:
enum {
UIWebViewNavigationTypeLinkClicked,
UIWebViewNavigationTypeFormSubmitted,
UIWebViewNavigationTypeBackForward,
UIWebViewNavigationTypeReload,
UIWebViewNavigationTypeFormResubmitted,
UIWebViewNavigationTypeOther
};typedef NSUInteger UIWebViewNavigationType;
Run Code Online (Sandbox Code Playgroud)
可以检查这个,然后查看NSURLRequest以获取有关它的信息
使用自定义方案.当用户点击按钮时,使用自定义方案(myScheme:// buttonTapped)ping一个url并执行以下任一操作:
在webview委托方法中捕获它shouldStartLoadWithRequest...(即检查URL是否包含您的自定义方案)并将其路由到适当的目标c选择器.要么
注册自定义URL协议并将其设置为处理您的自定义URL方案.像下面这样的东西:
@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
{
}
| 归档时间: |
|
| 查看次数: |
11367 次 |
| 最近记录: |