Objective c UIwebview中的Html表单是否发送回ios?
我尝试在HTML表单和ios之间传递信息,我尝试了javascrip,但它不起作用Objective c UIwebview中的Html表单是否发送回ios?,objective-c,uiwebview,Objective C,Uiwebview,我尝试在HTML表单和ios之间传递信息,我尝试了javascrip,但它不起作用 // Init Web View UIWebView *webView; webView = [[UIWebView alloc] initWithFrame:boundsRect]; webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// Init Web View
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
[self addSubview:webView];
NSString *testArrayString = [webView stringByEvaluatingJavaScriptFromString:@"testString"];
NSArray *testArray = [testArrayString componentsSeparatedByString:@"#"];
NSLog(@"%@", testArray);
[webView release];
return self;
有什么想法吗
----编辑----
好了,伙计们,如果你只想让数据从表单传递到ios应用程序,你只需要对你的webview这样做
[webView setDelegate:self];
然后可以在程序标记中实现此方法
比如:
如果您想捕获一个请求,只需执行一个webview,就应该启动如下请求:
-(BOOL)webView:(UIwebView *) webView shouStartWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigation{
//implement code to do afeter the user click the form don't forget to return YES or NO because it is a BOOL
}
这就是我解决问题的方法任何问题请告诉我我会尽力帮助将数据从UIWebView传递回Objective-C运行时,您需要实现UIWebViewDelegates
webView:shouldStartingWithRequest:
并将表单数据作为URL中的参数传递。如果你用f.ex这样的方案设计你所有的URLmy app://?parameter1=value1¶meter2=value2
,您可以轻松筛选出web视图不应加载的URL,并返回NO
编辑:
添加了示例代码
- (void)initWebView
{
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
webView.delegate = self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
if ([url.scheme isEqualToString:@"http://"]) {
return YES;
}
else if ([url.scheme isEqualToString:@"my-app://"]){
// Process data from form
return NO;
}
return YES;
}
EDIT2:您正在初始化代码中释放web视图。这可以防止在加载表单url时调用委托方法
EDIT3:有关更多代码示例,请参见此问题:
stringByEvaluatingJavaScriptFromString:
希望您向其传递一个javascript片段,它将在web视图的上下文中对其进行计算testString
没有意义,它不是javascript,所以什么都没有发生。我在哪里可以实现我的问题是HTMl文件是本地的,这仍然有效。本地html使用文件://
url方案加载请求。要将数据从表单发送到应用程序,您只需在html文件中构造一个url,该url包含您希望传递给Objective-C-runtime的数据,并确保web视图尝试加载该url。通过单击锚定标记,或者使用设置窗口的javascript。位置应用程序可以很好地加载页面,但我仍然无法从文本框中提取数据:SIn java script:var url=“my app://?text=“+document.getElementById(“myTextArea”)。value;window.location=url
- (void)initWebView
{
UIWebView *webView;
webView = [[UIWebView alloc] initWithFrame:boundsRect];
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
webView.scalesPageToFit = YES;
webView.contentMode = UIViewContentModeCenter;
webView.multipleTouchEnabled = NO;
webView.userInteractionEnabled = YES;
webView.delegate = self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = request.URL;
if ([url.scheme isEqualToString:@"http://"]) {
return YES;
}
else if ([url.scheme isEqualToString:@"my-app://"]){
// Process data from form
return NO;
}
return YES;
}