webview 页面返回原生页面
方法就是在html5 代码中添加:
function chooseCar(){ //为按钮的点击时间
var url="testapp:";
document.location = url;
}
然后实现代理方法
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *requestString = [[request URL] absoluteString];
NSArray *components = [requestString componentsSeparatedByString:@":"];
if ( components.count >0&&[(NSString *)[components objectAtIndex:0] isEqualToString:@"testapp"]) {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Alert from Cocoa Touch" message:@"test"
delegate:self cancelButtonTitle:nil
otherButtonTitles:@"OK", nil];
[alert show];
return NO;
}
return YES;
}
本文介绍了一种从WebView页面返回到原生页面的方法。通过在HTML5代码中定义特定函数并利用URL scheme触发原生应用的行为,实现了WebView与原生应用间的交互。同时展示了iOS平台下如何捕获这些请求并做出响应。
3万+

被折叠的 条评论
为什么被折叠?



