[英]iPhone/Objective-C - Retrieving echoed PHP text from UIWebView
我有一个返回access_token的网站(即Facebook)。
redirect_uri has
已更改为指向我的Web服务器上的PHP文件。 PHP代码只是将用户脱机的facebook access_token
到页面。
抓住此access_token
以便将其存储在NSUserDefaults
的最佳方法是什么?
我假设我需要使用UIWebView
的:
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
以某种方式形状还是形式?
任何帮助将不胜感激。
不要使用网络视图! 它消耗资源并且为此目的很慢。
利用ASIHttpRequest之类的框架来触发http get请求或使用Apple的NSURLConnection 。 您甚至可以考虑使用https带来一些安全性。
尽管我同意您不应该使用UIWebView
来做到这一点。 回答您的问题。
NSString *theHTML = [_webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText"];
在使用该方法之前,我建议您尽可能使用以下解决方案。
NSString *theHTML = [NSString stringWithContentsOfURL:[NSURL URLWithString:url]];
更新:Alan Zeino指出,使用stringWithContentsOfURL:
并不是很理想。 因此,我提供了另一个异步的解决方案。 我的解决方案假定一切正常。 您必须实现错误处理和数据重用机制。
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// self.data is a NSMutableData property
[self.data appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *theHTML = [[[NSString alloc] initWithData:self.data encoding:NSASCIIStringEncoding] autorelease];
self.data = nil;
// TODO: Its up to you now how you want to handle the HTML.
}
要执行请求,请使用它。
NSURLRequest *theURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLConnection *theURLConnection = [[[NSURLConnection alloc] initWithRequest:theURLRequest delegate:self startImmediately:YES] autorelease];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.