Basic URL
NSURL *url = [NSURL URLWithString:@"http://www.mydomain.com/some_folder/movie.mov"];
//or
NSString *urlAddress = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
Simple Downloading data from a URL
Synchronous methods which block until they have the result:
NSData *downloadData = [NSData dataWithContentsOfURL:url];
Same synchronous method but with Options and Error
//----- DOWNLOAD FILE -----
NSError *error = nil;
NSURL *url = [NSURL URLWithString:@"http://www.mydomain.com/somefile.xml"];
NSData *DownloadedData = [NSData dataWithContentsOfURL:url options: NSDataReadingUncached error: &error];
if (error)
NSLog(@"Download error: %@",error);
Authentication
A simple method of including the username and password in the URL
@"http://username:[email protected]/somefile"
Providing credentials before accessing the URL
NSURLCredential *credential = [[NSURLCredential alloc]
initWithUser:@"root"
password:@"12345"
persistence: NSURLCredentialPersistenceForSession];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:@"192.168.0.131"
port:80
protocol:@"http"
realm:nil
authenticationMethod:NSURLAuthenticationMethodDefault];
[[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential:credential
forProtectionSpace:protectionSpace];
[protectionSpace release];
[credential release];