Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

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

Reply
 
LinkBack Thread Tools Display Modes
Old 04-11-2008, 08:40 PM   #1 (permalink)
CoffeeKid
Guest
 
Posts: n/a
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
  Reply With Quote
Old 04-11-2008, 11:02 PM   #2 (permalink)
CoffeeKid
Guest
 
Posts: n/a
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
  Reply With Quote
Old 04-12-2008, 03:13 AM   #3 (permalink)
New 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)
Registered Member
 
Join Date: Apr 2008
Location: Colorado
Posts: 313
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.
jonc is offline   Reply With Quote
Old 04-15-2008, 04:01 PM   #5 (permalink)
New Member
 
Join Date: Apr 2008
Posts: 298
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)
New 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)
New 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)
New 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)
New 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


Similar Threads
Thread Thread Starter Forum Replies Last Post
UIWebView how to reload page every time Marco iPhone SDK Development 2 10-10-2010 06:37 PM
Getting data from an UIImage realberen iPhone SDK Development 11 03-16-2009 02:43 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
web page search nownot iPhone SDK Development 0 07-04-2008 01:30 PM


» Advertisements
» Stats
Members: 158,761
Threads: 89,201
Posts: 380,570
Top Poster: BrianSlick (7,129)
Welcome to our newest member, jam3skn0
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 09:41 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0