Mal ein wenig Objective-C

11/17/2013 00:58 snow#1
Mal ein wenig Objective-C, alle Samples benötigen zwingend das ASIHTTPRequest-Framework.

elitepvpers.com Login:

Code:
- (void)loginWithUsername:(NSString *)username Password:(NSString *)password
{
   NSString *loginURL = @"http://www.elitepvpers.com/forum/login.php?do=login";
   NSString *requestBodyString = [NSString stringWithFormat:@"vb_login_username=%@&vb_login_password=%@&cookieuser=1&s=&securitytoken=guest&do=login&vb_login_md5password=&vb_login_md5password_utf=", username, password];
    
   NSURL *url = [NSURL URLWithString:loginURL];
   __weak ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setPostBody:[NSMutableData dataWithData:[requestBodyString dataUsingEncoding:NSUTF8StringEncoding]]];
   [request setShouldRedirect:NO];
   [request addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d", [requestBodyString length]]];
   [request addRequestHeader:@"Content-Type"   value:@"application/x-www-form-urlencoded"];
    
   [request setCompletionBlock:^{
       
   		if ([[request responseString] rangeOfString:@"Thank you for logging in"].location != NSNotFound || [[request responseString] rangeOfString:@"Danke für deine Anmeldung"].location != NSNotFound) {
        
         //success
   		} else {
         //error
   		}	       
    }];
    
    [request setFailedBlock:^{
        //network error?
    }];
    
    [request startAsynchronous];
}
Treasure erstellen (davor einloggen, Cookies werden durch ASIHTTP gesichert):

Code:
- (void)createTreasureWithTitle:(NSString *)title text:(NSString *)text price:(NSString *)price
{
    
    NSString *postBody = [NSString stringWithFormat:@"title=%@&content=%@&cost=%@&createtreasure=Submit", title, text, price];
    NSString *url = [NSString stringWithFormat:@"http://www.elitepvpers.com/theblackmarket/treasures/"];
    
    ASIHTTPRequest *_request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:url]];
    __weak ASIHTTPRequest *request = _request;
    
    [request addRequestHeader:@"Content-Length" value:[NSString stringWithFormat:@"%d", [postBody length]]];
    [request addRequestHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
    
    [request setPostBody:[NSMutableData dataWithData:[postBody dataUsingEncoding:NSUTF8StringEncoding]]];
    
    [request setShouldRedirect:NO];
    
    [request setCompletionBlock:^{
            
        if ([[request responseString] rangeOfString:@"<div>Deine Treasure wurde erfolgreich erstellt.</div>"].location != NSNotFound || [[request responseString] rangeOfString:@"<div>Your treasure has been successfully created.</div>"].location != NSNotFound) {

			//success            
        } else if ([[request responseString] rangeOfString:@"<li>You've to wait 15 seconds before you can create your next treasure.</li>"].location != NSNotFound) {

			// wait 15 seconds, hm
        } else {
        
        	// error    
        }
    }];
    
    [request setFailedBlock:^{

		//network error?        
    }];
    
    [request startAsynchronous];
}