我是iOS开发的新手,我正在使用具有原生登录屏幕的webview创建应用程序。完成后,我需要关闭webview并返回到本机登录屏幕。我已经看到这个问题得到了解答,但我不明白答案。这是回复的帖子:Close UIWebView using javascript:window.close();
我目前有这个:
NSURL *url = [NSURL URLWithString: @"http://mysite/redirector.php"];
NSString *postData =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[_txtUsername text],[_txtPassword text]];
// Create the request object
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [postData dataUsingEncoding: NSUTF8StringEncoding]];
// create the web view
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
//Change Frame
webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y + 20, webView.frame.size.width, webView.frame.size.height);
// Load the request
[webView loadRequest: request];
//show the webview
[self.view addSubview:webView];
我需要在上面的代码中进行哪些更改才能使其工作?
如何“通过[NSURL path]
”阅读shouldStartLoadWithRequest:
中的模式?
提前致谢。
答案 0 :(得分:0)
只需在代码中添加以下内容
即可NSURL *url = [NSURL URLWithString: @"http://mysite/redirector.php"];
NSString *postData =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[_txtUsername text],[_txtPassword text]];
// Create the request object
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: @"POST"];
[request setHTTPBody: [postData dataUsingEncoding: NSUTF8StringEncoding]];
// create the web view
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
//Change Frame
webView.frame = CGRectMake(webView.frame.origin.x, webView.frame.origin.y + 20, webView.frame.size.width, webView.frame.size.height);
//assign delegate to webview
webView.delegate = self.
// Load the request
[webView loadRequest: request];
//show the webview
[self.view addSubview:webView];
//并在代理方法
下面实现 - (void)webViewDidFinishLoad:(UIWebView *)webView{
[webView removeFromSuperView];
}
//如果要在点击链接时关闭webview,请在代码
下面使用- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//Webview link press is hyperlink then it will called
if (navigationType == UIWebViewNavigationTypeLinkClicked){
[webView removeFromSuperView];
}
return YES;
}