我有一个返回access_token(即Facebook)的网站。
redirect_uri has
已更改为指向我的Web服务器上的PHP文件。 PHP代码简单地将用户离线facebook access_token
发送到页面。
获取此access_token
的最佳方法是什么,以便将其存储在NSUserDefaults
内?
我假设我需要使用UIWebView
:
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
以某种方式塑造或形成?
非常感谢任何帮助。
答案 0 :(得分:2)
不要使用webview!它消耗资源并且为此目的而缓慢。
使用像ASIHttpRequest这样的框架来触发http get请求或使用Apple的NSURLConnection。您甚至可以考虑使用https来带来一些安全性。
答案 1 :(得分:1)
虽然我同意你不应该使用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];