在我的应用中,我要加载一个网站。要访问此网站,我需要向服务器发送身份验证,并且我想在UIWebView中显示此页面的内容。我试图通过使用此代码来解决它:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *stringUrl = @"http://54.204.6.246/magento8/acquista";
[self sendRequestToUrl:stringUrl withMethod:@"GET"];
}
- (id)sendRequestToUrl:(NSString*)URL withMethod:(NSString*)method {
NSURL *finalUrl;
if ([method isEqualToString:@"GET"]) {
finalUrl = [NSURL URLWithString:URL];
} else {
NSLog(@"Metodo non previsto");
}
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:finalUrl];
[request setHTTPMethod:method];
NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"user", @"password"];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];
[request setValue:@"x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-type"];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
if (connection) {
[connection start];
}
return connection;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
recivedData = [[NSMutableData alloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[recivedData appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%@", [NSString stringWithUTF8String:[recivedData bytes]]);
[self.webViewPayment loadData: recivedData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];
}
在我的NSLog
中,我看到网站的HTML,如果我将此HTML粘贴到文件中,并尝试打开它,我可以看到该页面没有问题。这是HTML代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Acquista</title>
<meta name="description" content="Default Description" />
<meta name="keywords" content="Magento, Varien, E-commerce" />
<meta name="robots" content="INDEX,FOLLOW" />
<link rel="icon" href="http://54.204.6.246/magento8/skin/frontend/default/default/favicon.ico" type="image/x-icon" />
<link rel="shortcut icon" href="http://54.204.6.246/magento8/skin/frontend/default/default/favicon.ico" type="image/x-icon" />
<!--[if lt IE 7]>
<script type="text/javascript">
//<![CDATA[
var BLANK_URL = 'http://54.204.6.246/magento8/js/blank.html';
var BLANK_IMG = 'http://54.204.6.246/magento8/js/spacer.gif';
//]]>
</script>
<![endif]-->
<link rel="stylesheet" type="text/css" href="http://54.204.6.246/magento8/skin/frontend/default/default/css/styles.css" media="all" />
<link rel="stylesheet" type="text/css" href="http://54.204.6.246/magento8/skin/frontend/base/default/css/widgets.css" media="all" />
<link rel="stylesheet" type="text/css" href="http://54.204.6.246/magento8/skin/frontend/default/default/css/print.css" media="print" />
<script type="text/javascript" src="http://54.204.6.246/magento8/js/prototype/prototype.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/lib/ccard.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/prototype/validation.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/scriptaculous/builder.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/scriptaculous/effects.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/scriptaculous/dragdrop.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/scriptaculous/controls.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/scriptaculous/slider.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/varien/js.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/varien/form.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/varien/menu.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/mage/translate.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/mage/cookies.js"></script>
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="http://54.204.6.246/magento8/skin/frontend/default/default/css/styles-ie.css" media="all" />
<![endif]-->
<!--[if lt IE 7]>
<script type="text/javascript" src="http://54.204.6.246/magento8/js/lib/ds-sleight.js"></script>
<script type="text/javascript" src="http://54.204.6.246/magento8/skin/frontend/base/default/js/ie6.js"></script>
<![endif]-->
<script type="text/javascript">
//<![CDATA[
Mage.Cookies.path = '/magento8';
Mage.Cookies.domain = '.54.204.6.246';
//]]>
</script>
<script type="text/javascript">
//<![CDATA[
optionalZipCountries = ["HK","IE","MO","PA"];
//]]>
</script>
<script type="text/javascript">//<![CDATA[
var Translator = new Translate([]);
//]]></script></head>
<body class="page-empty cms-page-view cms-acquista">
<div>
<div class="std"><div style="background-color: #f5f5f5;">
<p><img style="display: block; margin-left: auto; margin-right: auto;" src="http://oi42.tinypic.com/34gsdcl.jpg" alt="" width="320" height="110" /></p>
</div></div> </div>
</body>
</html>
当我尝试在iPhone / iPhone模拟器上加载页面时,它会显示一个空白页面。我正在使用的代码有什么问题?
使用上述代码后我所看到的图像
当我尝试加载HTML时,你可以看到我的模拟器显示的内容Dinesh在他的回答中向我展示并使用我的代码:
网站应该是这样的:
答案 0 :(得分:0)
在-(void)connectionDidFinishLoading:(NSURLConnection *)connection
检查你的self.webViewPayment是否有一些有效的地址(不应该是零)
如果以上不起作用,请尝试此
-(void)connectionDidFinishLoading:(NSURLConnection *)connection function
{
NSString* respString = [NSString stringWithUTF8String:[recivedData bytes]];
[self.webViewPayement loadHTMLString:respString baseURL:nil];
}
在你的。
答案 1 :(得分:0)
注意:
这不起作用:
[request setValue:@"x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Content-type"];
MIME类型x-www-form-urlencoded
没有任何参数。所以,你不能设置一个字符集。更重要的是,语法无效。您的服务器可能会抱怨并向您发送400(不良请求)。
改为使用
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"];
AFAIK,客户端无法为form-urlencoded参数指定字符编码。通常,字符编码由服务器在返回表单时给出。当客户端无法从表单中推断出字符编码时 - 或者根本没有表单时,例如在REST API中,客户端应该使用 Unicode UTF-8 。
好奇:你从哪里获得这种格式错误的内容?