我有一个应用程序,当你按下一个按钮时,它会加载一个php页面。我不需要从中获取任何字符串。我只需要请求页面,而不会在按下按钮时被用户看到。我不想使用 uiwebview,因为它们会消耗太多资源。
如何加载 php 页面而不在按下按钮时向用户显示?(可以插入到 IBAction 中的优选代码)。
附言。我已经用谷歌搜索了 1/2 小时左右,我找不到我在这里问的任何东西。
谢谢!
你真的不需要网页上的任何数据,你只需要加载一次它来通知Web服务器用户做了什么?您可以简单地使用:
NSString* urlString = @"http://blaablaa.com/hello.php";
NSURL *theURL = [NSURL URLWithString:urlString];
NSURLRequest *req = [NSURLRequest requestWithURL:theURL];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:req delegate:self];
如果需要获取请求的结果(是成功还是失败),请实现
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
// your stuff here
}
在你的课堂上。
只需使用stringWithContentsOfURL:encoding:error:
NSError *error;
NSURL *url = [NSURL URLWithString:@"http://server.com/file.php"];
NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&error];