iPhone/Objective-C - 从 UIWebView 检索回显的 PHP 文本

发布于 2024-11-02 07:18:54 字数 434 浏览 0 评论 0 原文

我有一个返回 access_token 的网站(即 Facebook)。

redirect_uri 已更改为指向我的 Web 服务器上的 PHP 文件。 PHP 代码只是将离线用户的 facebook access_token 回显到页面。

获取此 access_token 以便我可以将其存储在 NSUserDefaults 中的最佳方式是什么?

我假设我需要使用 UIWebView's:

[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

以某种方式形状或形式?

任何帮助将不胜感激。

I have a website that returns an access_token (i.e. Facebook).

The redirect_uri has been changed to point to a PHP file on my web server. The PHP code simply echoes the users offline facebook access_token to the page.

What would be the best way to grab this access_token so I can store it within NSUserDefaults?

I'm assuming I'd need to use UIWebView's:

[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];

In some way shape or form?

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久夏青 2024-11-09 07:18:54

不要使用网络视图!它会消耗资源并且速度很慢。

使用像 ASIHttpRequest 这样的框架来触发 http get 请求或使用 Apple 的 NSURLConnection。您甚至可以考虑使用 https 来带来一些安全性。

Don't use a webview! It consumes resources and is slow for that purpose.

Make use of a Framework like ASIHttpRequest to fire a http get request or use Apple's NSURLConnection. You may even consider using https to bring in some security.

请别遗忘我 2024-11-09 07:18:54

尽管我同意您不应该使用 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];

Although I agree you shouldn't use UIWebView to do that. To answer your question.

NSString *theHTML = [_webView stringByEvaluatingJavaScriptFromString:@"document.body.innerText"];

Before you go use that, I would suggest you use the following solution instead if possible.

NSString *theHTML = [NSString stringWithContentsOfURL:[NSURL URLWithString:url]];

UPDATE:
Alan Zeino point out that using stringWithContentsOfURL: is not a very ideal. So I provide another solution that is async. My solution assume everything will work. You have to implements error handling and data reusing mechanism.

- (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.
}

To execute the request, use this.

NSURLRequest *theURLRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSURLConnection *theURLConnection = [[[NSURLConnection alloc] initWithRequest:theURLRequest delegate:self startImmediately:YES] autorelease];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文