Giter VIP home page Giter VIP logo

jwurlconnection's Introduction

JWURLConnection

An extension on NSURLConnection with upload possibilities, blocks and delegate protocol

This class implements a rich delegate protocol, blocks and selectory. It also provides a POST data upload capability and a connection queue class.

JWURLConnection comes along with JWByteWrite and JWByteReader classes which will be passed to the delegate for easy progress calculations.

Demo

I'll try to provide an example app in the next weeks, since I don't have much time yet.

Installation

Easily drop the four files into your project and #include "JWURLConnection.h

Example Usage

// Easy Block callbacks
- (void)startEasyBlockConnection {
	JWURLConnection *conn = [JWURLConnection connectionWithRequest:req delegate:nil];
	
	[conn setReceivedResponse:^(JWURLConnection *con, NSHTTPURLResponse *resp) {
		[output setText:[NSString stringWithFormat:@"%@\ndid receive response: %@", [output text], [resp allHeaderFields]]];
	}];
	
	[conn setSentBytes:^(JWURLConnection *con, JWByteWriter *bw) {
		NSLog(@"%@\ndid write %i bytes", [output text], [bw bytesWritten]);
	}];
	
	[conn setFinished:^(JWURLConnection *con, NSData *da) {
		NSLog(@"%@\ndid finish: %@", [output text], [[NSString alloc] initWithData:da encoding:NSUTF8StringEncoding]);
	}];
	
	[conn setFailed:^(JWURLConnection *con, NSError *err) {
		NSLog(@"%@\ndid fail: %@", [output text], [err description]);
	}];
	// Trust own SSL certificate for HTTPS
	[conn setWillSendRequestForAuthenticationChallenge:^(NSURLAuthenticationChallenge *challenge) {
			
		SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
		SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, 0);
		NSData *remoteCertificateData = CFBridgingRelease(SecCertificateCopyData(certificate));
		NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"MyLocalCertificate" ofType:@"cer"];
		NSData *localCertData = [NSData dataWithContentsOfFile:cerPath];
		
		if ([remoteCertificateData isEqualToData:localCertData]) {
			NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust];
			[[challenge sender] useCredential:credential forAuthenticatinChallenge:challenge];
		}
		else {
			[[challenge sender] cancelAuthenticationChallenge:challenge];
		}
	}];
	[conn start];
}

// Queueing
- (void)startQueuedConnections {
	JWURLConnection *imageConnection = [JWURLConnection connectionByFormUploadingData:[NSData dataWithContentsOfFile:[NSBundle pathToResource:@"image" ofType:@"jpg" inDirectory:imgDir]]];
	JWURLConnection *soundConnection = [JWURLConnection connectionByFormUploadingData:[NSData dataWithContentsOfFile:[NSBundle pathToResource:@"voices" ofType:@"mp3W inDirectory:soundDir]]];
	
	JWURLConnectionQueue *connQueue = [JWURLConnectionQueue queueWithURLConnections:@[imageConnection, soundConnection]];
	[connQueue setDidSendBytesSelector:@selector(queueDidSendBytes:)];
	[connQueue setDelegate:self];
	[connQueue startQueue];
}
- (void)queueDidSendBytes:(JWByteWriter *)br {
	NSLog(@"did write %i if%ibytes", [br totalBytesWritten], [br totalBytesExpectedToWrite]);
}

Credits

Inspired by ASIHTTPRequest and many others

Contact / Reference

Julian Weinert

License

JWURLConnection is available under the GPL V2 license. See the LICENSE file for more info.

jwurlconnection's People

Contributors

julian-weinert avatar patrick-petersen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.