我想知道是否可以在 UIWebView 中按下提交按钮时获取 POST 数据并保存它。我应该使用 javascript 并添加事件监听器,以便在提交通过之前获取值吗?如果是这样,我如何才能将数据返回到我在 obj c 中的代码?否则,有没有更简单的方法?谢谢
最佳答案
您需要实现 UIWebViewDelegate。 iOS 会调用名为“shouldStartLoadWithRequest”的 Hook 。
伪代码如下。
@interface MyController<UIWebViewDelegate>
@end
@implementation MyController
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
// do your magic
NSData *data = request.HTTPBody; // contains the HTTP body as in an HTTP POST request.
// return YES to continue to load the URL
}
@end
关于objective-c - 从 UIWebView 抓取 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10457749/