Asp.net UIWebKit不足
像我之前的很多人一样,我有一些网络内容想在我的应用程序中显示。我的网站是在Microsoft Visual Web Developer中开发的基于ASP.NET的网站,它使用Ajax工具包和其他漂亮的插件。我可以在iPhone的mobile Safari浏览器中打开该网站,甚至还创建了一些手机友好页面 我(或者更准确地说,我的老板)想要一个iPhone应用程序,它存储用于访问我的站点的凭据,并在打开应用程序时自动发送这些凭据。问题是: 我的站点未加载到UIWebView中。它也不会遇到任何委托方法(完成加载、加载失败等)。它只是启动连接请求,然后坐在那里。该站点在Safari中的工作与我用于测试的设备相同,因此我认为问题与UIWebKit中没有Safari内置的一些工具有关 当我的站点在Safari中正确加载时,是什么(具体地)导致我的站点没有加载到UIWebKit中 我将发布我设置的方法,从我的呼叫中捕捉任何生命迹象Asp.net UIWebKit不足,asp.net,ios,uiwebview,ajaxcontroltoolkit,Asp.net,Ios,Uiwebview,Ajaxcontroltoolkit,像我之前的很多人一样,我有一些网络内容想在我的应用程序中显示。我的网站是在Microsoft Visual Web Developer中开发的基于ASP.NET的网站,它使用Ajax工具包和其他漂亮的插件。我可以在iPhone的mobile Safari浏览器中打开该网站,甚至还创建了一些手机友好页面 我(或者更准确地说,我的老板)想要一个iPhone应用程序,它存储用于访问我的站点的凭据,并在打开应用程序时自动发送这些凭据。问题是: 我的站点未加载到UIWebView中。它也不会遇到任何委托方
[myWebView loadRequest:myRequest];
当我运行我的应用程序并达到上面的loadRequest调用时,没有调用以下方法
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"WillSendForAuthenticationChallenge");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"Webview failed to load.");
if (error.code == NSURLErrorCancelled) return; // this is Error -999, called when the user forcibly ends the connection by starting a new connection.
UIAlertView *failure = [[UIAlertView alloc] initWithTitle:@"Failed" message:error.localizedDescription delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[failure show];
[spinner stopAnimating];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"Loading request: %@",request.URL);
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webview
{
NSLog(@"Webview finished loading.");
[spinner stopAnimating];
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
NSLog(@"Can Authenticate: %@",[protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]);
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"Received an authentication challenge.");
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}
我是这样解决的。UIWebView从不调用任何NSURLConnection方法,也没有任何内置的安全方法,因此当web视图即将加载页面时,我首先向同一地址发送一个NSURLConnection,然后它可以通过didReceiveAuthenticationChallenge等方法处理身份验证。变量“\u sessionChecked”是一个简单的BOOL,用于跟踪是否已为此页面发送了身份验证NSURLConnection。一旦完成,我就可以正常地将页面加载到web视图中
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"Loading request: %@",request.URL);
if (_sessionChecked) {
NSLog(@"Session already checked.");
return YES;
}
NSLog(@"Will check session.");
NSMutableURLRequest *newReq = request.mutableCopy;
[newReq setValue:USRAGNT forHTTPHeaderField:@"User-Agent"];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:newReq delegate:self];
if (conn == nil) {
NSLog(@"Cannot create connection.");
}
return NO;
}
当NSURLConnection从服务器获得响应时,调用didReceiveResponse方法。此响应包含两个重要内容:是否使用NSURLRequest提供的凭据成功验证了连接,以及如果是,则指向哪个url。成功验证后,我将新url发送到要加载的web视图
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"connection:didReceiveResponse");
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
_sessionChecked = YES;
NSString *newUrl = @"";
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
int status = [httpResponse statusCode];
if (status == 401 || status == 403) {
NSLog(@"Not authenticated. http status code: %d", status);
newUrl = @"";
[myWebView stopLoading];
[spinner stopAnimating];
UIAlertView *failed = [[UIAlertView alloc] initWithTitle:@"Authentication Failed" message:@"You do not have any credentials stored, or your stored credentials are not valid." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[failed show];
_sessionChecked = NO;
}
else {
NSLog(@"Authenticated. http status code: %d", status);
newUrl = [NSString stringWithFormat:@"%@", response.URL];
}
// cancel the connection. we got what we want from the response,
// no need to download the response data.
[connection cancel];
// start loading the new request in webView
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:newUrl]];
[myWebView loadRequest:req];
}
}
非常感谢Ardal Ahmet。如果有人能在这方面为您提供帮助,也许您可以告诉我们,在网站正常运行的情况下,您当前是如何向网站传递凭据的(显示一些代码),以及您希望如何使用UIWebKit(描述性地)执行此操作。您是否设置了webview委托?是的,我想是这样。webview连接到Interface Builder中的UIWebView对象,该对象的“委托”连接到“文件的所有者”。此外,为了澄清,我的webview可以加载其他网页而不会出现问题。我用它浏览过谷歌、苹果、一些博客等等。我遇到的麻烦是加载我自己的站点。它不是一个公共站点,它包含在我大楼的网络中。它是在我们的服务器上运行的IIS 7,但我不是It人员,所以我不知道所有的细节。不过,它在Safari中运行良好,当它开始加载时,会出现一个小弹出窗口,询问您的凭据,而该网站的工作方式正是它应该的。