开发者

iphone: how to get text from web site and show it on application

i have web site that show a text and i update this text every day , i want to show this text on iphone applic开发者_如何学Pythonation , how can i get this text from web site from application ? what should i do ? thanks


1-> you require to connect with your web server thought HTTP connection.

2-> Make the request to server.

3-> Parse server response that may contain your "Text".

For technical assistance Read below.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html


I don't recommend this as the best way to obtain a string from your own web server. This should point you in the right direction, don't expect it to compile cleanly.

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];

This will capture text from first h1 tag.


The simplest way is to create a REST API. That might sound tough but it's really easy. On the server side, create a new page which holds only the raw text. Usually it's best to keep it there in JSON/XML format, but a simple text will also work. Now from the iPhone, just contact that address and the response data will contain the text. Parsing an existing page is not something I recommend, because changing that page in the future might result in the app not working anymore.


This is a answer quite late but I think it still might help in your future. You can go into parsing the website and that is the right way to do it but I will show you how to do it a different way, this can also be used to read xml, html, .com, anything and also, .rss so it can read RSS Feeds.
Here :

This can get your first paragraph, if you request I will show you how to get the second paragraph and so on.

 //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;


    }

Title ? Same deal change the <p> to a <title> in RSS <description> and <title>. Image ? Same deal change the <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

` you see the link which says stringByReplacingOccurences of you have to change that as well.

out then you have to delete this and make your code like this :

/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;

Now check your log it shows your whole website html or rss code then you scroll and read it and find your image link and check the code before it and change the String Scanner to your needs which is quite awesome and you have to change the stringByReplacingOccurences of.

Like I said images are a bit tricky when you do it with this method but XML Parsing is a lot easier ONCE you learn it , lol. If you request I will show you how to do it.

Make sure :

If you want me to show you how to do it in XML just comment. If you want me to show you how to find the second paragraph or image or title or something just comment.

IF YOU NEED ANYTHING JUST COMMENT.

Bye have fun with code I provided, anything wrong JUST COMMENT! !!!!

:D

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜