我有一个显示文本的网站,我每天都会更新此文本,我想在iphone应用程序上显示此文本,如何从应用程序的网站上获取此文本? 我该怎么办 ? 感谢
答案 0 :(得分:2)
1→您需要连接您的Web服务器以及HTTP连接。
2→向服务器发出请求。
3→解析可能包含“文本”的服务器响应。
技术支持请阅读以下内容。
答案 1 :(得分:1)
我不建议将此作为从自己的 Web服务器获取字符串的最佳方式。 这应该指向正确的方向,不要指望它干净地编译。
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
/* set headers, etc. on request if needed */
[request setURL:[NSURL URLWithString:@"http://example.com/whatever"]];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:NULL error:NULL];
NSString *html = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
NSScanner *scanner = [NSScanner scannerWithString:html];
NSString *token = nil;
[scanner scanUpToString:@"<h1>" intoString:NULL];
[scanner scanUpToString:@"</h1>" intoString:&token];
这将从第一个h1
标记中捕获文本。
答案 2 :(得分:1)
最简单的方法是创建REST API。这可能听起来很难,但这很容易。在服务器端,创建一个仅包含原始文本的新页面。通常最好以JSON / XML格式保存它,但是一个简单的文本也可以工作。现在从iPhone,只需联系该地址,响应数据将包含文本。解析现有页面不是我推荐的,因为将来更改该页面可能会导致应用程序无法正常工作。
答案 3 :(得分:0)
这个答案很晚,但我认为它对你的未来仍有帮助。你可以去解析网站,这是正确的方法,但我会告诉你如何以不同的方式做到这一点,这也可以用来读取xml,html,.com,任何东西,以及.rss所以它可以阅读RSS Feed 在这里:
这可以得到你的第一段,如果你要求我会告诉你如何得到第二段等等。
//This is your URL
NSURL *URL = [NSURL URLWithString:@"URL HERE"];
//This is the data your pulling (dont change)
NSData *data = [NSData dataWithContentsOfURL:URL];
// Assuming data is in UTF8. (dont change)
NSString *string = [NSString stringWithUTF8String:[data bytes]];
//Your textView your not done.
description.text = string;
//Do this with your textview
NSString *webStringz = description.text;
// Leave this
NSString *mastaString;
mastaString = webStringz;
{
NSString *webString2222 = mastaString;
NSScanner *stringScanner2222 = [NSScanner scannerWithString:webString2222];
NSString *content2222 = [[NSString alloc] init];
//Change <p> to suit your need like <description> or <h1>
[stringScanner2222 scanUpToString:@"<p>" intoString:Nil];
[stringScanner2222 scanUpToString:@"." intoString:&content2222];
NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
description.text = filteredTitle;
}
标题?相同的交易会将<p>
更改为RSS <title>
和<description>
中的<title>
。
图片 ?相同的交易改变了<p> to what ever your RSS or website uses to get a image to find
But remember for both of them when you change the
`你看到你说stringByReplacingOccurences的链接也必须改变它。
然后你必须删除它并使你的代码像这样:
/This is your URL
NSURL *URL = [NSURL URLWithString:@"URL HERE"];
//This is the data your pulling (dont change)
NSData *data = [NSData dataWithContentsOfURL:URL];
// Assuming data is in UTF8. (dont change)
NSString *string = [NSString stringWithUTF8String:[data bytes]];
//Your textView your not done.
description.text = string;
NLog(@"%@", string)
//Do this with your textview
NSString *webStringz = description.text;
// Leave this
NSString *mastaString;
mastaString = webStringz;
现在检查你的日志它显示你的整个网站html或rss代码然后你滚动并阅读它并找到你的图像链接并检查它之前的代码并将String Scanner更改为你的需求非常棒,你必须改变stringByReplacingOccurences。
就像我说用这种方法做的时候图像有点棘手但是XML解析比你学习它容易得多,哈哈。如果您要求,我会告诉您如何操作。
确保:
如果您希望我向您展示如何在XML中进行评论。 如果您希望我向您展示如何查找第二段或图像或标题或其他只是评论。
如果你需要任何评论。
再见我提供的代码很有趣,任何错误只是评论! !!!!
:d