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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Requests

Reply
 
LinkBack Thread Tools Display Modes
Old 06-23-2009, 10:38 PM   #1 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 2
NoGi is on a distinguished road
Default Tutorial Request: Reading from a secure webpage

Hi,

I am wanting to convert my Widget into a application. Having problems trying to get my head around XCode. Would appreciate if someone could show me how to open a website based on supplied user credentials and pass the contents into a string for manipulation.

The way I do it in the Widget is: (URL has been changed for the post)

Code:
var xmlhttp = null;
xmlhttp = new XMLHttpRequest();

// Log the user into the page
xmlhttp.open("post","https://awebsite.com/path/index.php?function=checksomething",false);
xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send("check_username=" + username + "&password=" + password);
            
var RetrievedWebPage = xmlhttp.responseText;
           
// Log the user out of the page
xmlhttp.open("post","https://awebsite.com/path/index.php?function=logout",false);
xmlhttp.send();           
xmlhttp=null;
NoGi is offline   Reply With Quote
Old 06-26-2009, 11:55 AM   #2 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 1
thegreyarea is on a distinguished road
Default

If I understand your above correctly, your question should be how do I post data to a website and get the returned page. I don't see anything that would indicate an authentication screen (such as one brought up by htaccess) is appearing and you need to get around it. If that's the case, this is all you need to do.

Code:
// Create the username and password string.
// username and password are the username and password to login with
NSString *postString = [[NSString alloc] initWithFormat:@"check_username=%@&password=%@", username, password];
// Package the string in an NSData object
NSData *requestData = [NSData dataWithBytes: [postString UTF8String] length: [postString length]];

// Create the URL request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString:webURL]];  // create the URL request
[request setHTTPMethod: @"POST"];   // you're sending POST data
[request setHTTPBody: requestData];  // apply the post data to be sent

// Call the URL
NSURLResponse *response;  // holds the response from the server
NSError *error;   // holds any errors
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&error];  // call the URL

/* If the response from the server is a web page, dataReturned will hold the string of the HTML returned. */
NSString *dataReturned = [[NSString alloc] initWithData:returnData encoding:NSASCIIStringEncoding];
On the last line, I use NSASCIIStringEncoding. The call should use NSUTF8StringEncoding, but I've noticed this fails frequently with many websites. I've never had a failure with NSASCIIStringEncoding.

Check the Apple documentation on NSMutableURLRequest. There are numerous parameters you can set for the web request, such as the timeout, referring URL, and any cookies you may want to send.
thegreyarea is offline   Reply With Quote
Old 06-27-2009, 10:07 PM   #3 (permalink)
New Member
 
Join Date: Jun 2009
Posts: 2
NoGi is on a distinguished road
Default

Thank you. I will give that a go.
NoGi 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



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,961
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

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