目标C,打开一个网站,而不打开它在Safari
我想找到一种方法来加载一个网站(用于服务器调用的目的),但我不希望它既不在safari中也不在UIWebView中打开。我通常在Safari目标C,打开一个网站,而不打开它在Safari
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:@"http://www.google.com"]];
,为UIWebViews中
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thelink]]];
使用此代码开放我试图用一个UIWebView而实际上不必物理上的,但页面没有加载(我知道,因为服务器调用中根本没有提出)
感谢
只需使用
012如果连接是= NULL,则一切正常
您应该使用的NSURLRequest,例如:
NSString * url = @"http://myapi.mysite.com/api.php";
NSMutableURLRequest * aRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSHTTPURLResponse * httpResponse = nil;
NSError * urlError = nil;
NSData * responseData = [NSURLConnection sendSynchronousRequest:aRequest returningResponse:&httpResponse error:&urlError];
哪里responseData是响应的身体,你可以使用的HttpResponse检查状态码等等......
你不应该假设'+ sendSynchronousRequest:returningResponse:error:'返回的'NSURLResponse'是一个'NSHTTPURLResponse'对象。在假定对象是子类的类型之前执行'-isKindOfClass:'要安全得多。 – 2012-02-08 16:32:03
当然,我省略了一些检查,如urlError,httpResonse与nil等不同 – Daniel 2012-02-08 16:33:43
NSURLConnection是你的朋友:
NSURL *URL = [NSURL URLWithString:@"http://www.google.com"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:URL];
// configure req to have post data and such
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
// do something with data
好˚F或者简单的GET,但是对于POST来说,这种方法是完全无法使用的。 – 2012-02-08 16:33:12
这怎么可能是“正确的解决方案”......住院病人用户......对不起 – Daniel 2012-02-08 16:46:43