Go Back   iPhone Dev SDK Forum > Development Forums > iPhone SDK Development

Featured Member Applications

TanZen ($0.99)

Endless Walls ($0.99)

Air Hockey ($0.99)

SUPER STRIKE - Motion Bowl ($0.99)

BarSlot ($0.99)

MeterRead ($0.99)

Colorblind Helper ($4.99)

gContacts ($1.99)

ProgCalc ($1.99)

Forex On The Go Lite (FREE)

HUE knewit! ($0.99)

Want your application to be advertised here?

» Advertisements


Visit our friends over at The App Show! Steve and Dave produce a weekly show shining a light on the iPhone 2.0 software and the applications being developed by the amazing development community for the iPhone SDK!
» Online Users: 99
18 members and 81 guests
blackyE, BSDimwit, bugnote, colinhumber, EeKay, FetaBoy, foxtucker, hyang, logicpaw, mahr, Moose, pashik, RickMaddy, roberthuttinger, Smittens, spinyanteater, uprise78, volk
Most users ever online was 207, 10-24-2008 at 09:29 AM.
» Stats
Members: 3,915
Threads: 5,625
Posts: 23,285
Top Poster: scottiphone (705)
Welcome to our newest member, raysms
Reply
 
LinkBack Thread Tools Display Modes
Old 04-11-2008, 08:40 PM   #1 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 6
Default Pulling Data From A Web Page

Hi Everyone,

I'm trying to use the CFHTTPRequest library to pull data from a web page. All the web page has is a pipe deliminated string of some information about our server status. I want to pull this into the iPhone and display it in a pretty gui interface.

My issue is that i can't figure out how to get the body of the web page. I've looked through a few examples and I keep getting lost. Below is the code i'm using, please give me some idea on how i would get the body, and then throw it into a string where i can then put the data into variables within a class.

Code:
	CFHTTPMessageRef request;
	CFReadStreamRef stream;
	//init: url is a string like "www.someplace.com"
	NSString *theURL = [NSString stringWithFormat:@"http://%@", url];
	NSURL *urlObj = [NSURL URLWithString:theURL];
	

	request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("GET"), (CFURLRef)urlObj, kCFHTTPVersion1_0);
	//fetch	
	stream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault,request);
	
	CFReadStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
	
	CFHTTPMessageRef response = (CFHTTPMessageRef) CFReadStreamCopyProperty(stream, kCFStreamPropertyHTTPResponseHeader);
	
        //not sure if this is correct :(
	CFDataRef *body = CFHTTPMessageCopyBody(response);

       //now what?

Am I heading in the right direction? Or is there a better way to do this?

If I am doing it right, how do I turn body into a string of actual text that I can manipulate?

Thanks,
CoffeeKid
CoffeeKid is offline   Reply With Quote
Old 04-11-2008, 11:02 PM   #2 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 6
Default Re: Pulling Data From A Web Page

I figured it out. For anyone that also needs to do this, its simplier then it looks.

Here's the Code:
Code:
	NSString *urlStr = [NSString stringWithFormat:@"%@/index.php?some_name=%@", url, name];
	NSURL *theURL = [[NSURL alloc] initWithString:urlStr];
	
        //get the data from the web page
	NSString * results = [NSString stringWithContentsOfURL:theURL];
	
        //Seperate the data by a | delimiter
	NSArray * chunks = [results componentsSeparatedByString:@"|"];
	
        //put the data into some class variables
	NSScanner * scanner = [[NSScanner alloc] initWithString:[chunks objectAtIndex:0]];
	[scanner scanFloat:&load];
	scanner = [[NSScanner alloc] initWithString:[chunks objectAtIndex:3]];
	[scanner scanInt:&diskUsage];
	users = [[NSString alloc] initWithString:[chunks objectAtIndex:4]];
Hope this helps someone, because this took me HOURS to figure out!

-CoffeeKid
CoffeeKid is offline   Reply With Quote
Old 04-12-2008, 03:13 AM   #3 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 7
Default Re: Pulling Data From A Web Page

Thanks for that. I can't believe how easy that is. I was just about to start having to figure that out. Now my concern is how to pull data from between two sets of known characters instead of splitting based on a single delimeter. So if anyone has any ideas, I am all ears. Nothing jumps out at me in the API to do this easily. I can figure out the harder way, but didn't know if their was something as simple as splitting was for you.

Example:

hey

I would want to get the word 'hey'
Cander is offline   Reply With Quote
Old 04-12-2008, 09:59 PM   #4 (permalink)
Senior Member
 
Join Date: Apr 2008
Location: Colorado
Posts: 315
Send a message via AIM to jonc
Default Re: Pulling Data From A Web Page

Thanks for sharing. I was about to have to figure out how to do that.
__________________
xCodeForum
jonc is offline   Reply With Quote
Old 04-15-2008, 04:01 PM   #5 (permalink)
Senior Member
 
Join Date: Apr 2008
Posts: 243
Default Re: Pulling Data From A Web Page

Thought, would add more to it, here is method if you want to add escape characters to your string.
Code:
NSString *urlString = [myString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString: urlString];
javid.alimohideen is offline   Reply With Quote
Old 04-16-2008, 01:51 AM   #6 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 1
Default Re: Pulling Data From A Web Page

Quote:
Originally Posted by Cander
Thanks for that. I can't believe how easy that is. I was just about to start having to figure that out. Now my concern is how to pull data from between two sets of known characters instead of splitting based on a single delimeter. So if anyone has any ideas, I am all ears. Nothing jumps out at me in the API to do this easily. I can figure out the harder way, but didn't know if their was something as simple as splitting was for you.

Example:

hey

I would want to get the word 'hey'
You could use the method scanUpToCharactersFromSet:intoString: to find multiple characters, but that won't work for a lot of things, and probably won't work for you here. What you really want is to use regular expressions to do the parsing. AFAIK there is nothing in Coca to handle it, but you could use NSTask to call a command-line function like grep, or Python, Perl, or Ruby script (all of which have built-in support for REs), or use a third-party Cocoa framework.

Pretty odd that Cocoa has no regular expression support directly, a major oversight if you ask me (you'd think it would have been pretty obvious when they were coming up with NSScanner, even back in the NeXT days).
HiRez is offline   Reply With Quote
Old 04-16-2008, 09:01 AM   #7 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 7
Default Re: Pulling Data From A Web Page

I got it working using NSScanner. Works perfectly.

Code:
- (void) getPilots:(NSString *)data { 
 	NSString *foundData = @""; 	
                NSScanner *scanner = [NSScanner scannerWithString:data];
 	// loop through the passed html data 	
                while (![scanner isAtEnd]) {
 		NSInteger leftPos, rightPos;
 		[scanner scanUpToString: @"left text to find" intoString: nil]; 		                leftPos = [scanner scanLocation];
 		 if (![scanner scanUpToString: @"right text to fin" intoString: nil])
 			break; 		 		
                                 rightPos = [scanner scanLocation] + 1;
 		 leftPos += 19; // add the number of characters of the left text to find
 		// get data between the left and right text
                                 foundData = [data substringWithRange: NSMakeRange(leftPos, (rightPos - leftPos) - 2)];
 		[playersArray addObject:foundData]; 
 	}
 }
Yeah the lack of RegEx seems very odd. After years of using it in .NET I feel naked without it.
Cander is offline   Reply With Quote
Old 04-17-2008, 12:19 AM   #8 (permalink)
Senior Member
 
Join Date: Apr 2008
Location: Onomatopoeia, Lugubriousylvania
Posts: 225
Default Re: Pulling Data From A Web Page

If your data is big, consider using NSURLConnection instead-- it allows you to load a URL asynchronously so that your UI doesn't completely lock up. Remember, your users might be stuck with the EDGE network for a while...
bonehead is offline   Reply With Quote
Old 04-17-2008, 12:43 PM   #9 (permalink)
Junior Member
 
Join Date: Apr 2008
Posts: 7
Default Re: Pulling Data From A Web Page

Quote:
Originally Posted by bonehead
If your data is big, consider using NSURLConnection instead-- it allows you to load a URL asynchronously so that your UI doesn't completely lock up. Remember, your users might be stuck with the EDGE network for a while...

Awesome. I was actually wondering about that last night on how not to lock up the view while loading.
Cander is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting data from an UIImage realberen iPhone SDK Development 10 10-30-2008 01:10 AM
Need Help Posting an image from iPhone to php page via POST ddavtian iPhone SDK Development 4 09-07-2008 09:08 PM
pixel data iamdave iPhone SDK Development 2 08-12-2008 09:38 PM
UIWebView how to reload page every time Marco iPhone SDK Development 1 07-06-2008 01:38 AM
web page search nownot iPhone SDK Development 0 07-04-2008 01:30 PM

Powered by vBadvanced CMPS v3.0.1

All times are GMT -5. The time now is 05:56 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0