Call selectors from your code. Create own buttons

by alex 26. May 2010 08:32

///////////////////BUTON

//-- button.h file

@interface MyButton : NSObject {

id receiver;

SEL selector;

}

 

 

//-- button.m file

-(void)addTarget:(id)target action:(SEL)action{

receiver = target;

selector =  action;

}

 

-(void)touch{

if (receiver) {

[receiver performSelector:selector];

}

}

 

//////////////////RECEIVER 

//-- receiver.m file

 

...

MyButton *mybutton = [[MyButton alloc] init];

[mybutton addTarget:self action:@selector(actionMethod)];

...

 

-(void)actionMethod{

//some code

}

Currently rated 4.2 by 5 people

  • Currently 4.2/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Send NSString to windows Web Service through SOAP

by srudenko 26. March 2010 10:11

- (void) Send :(NSString*) tarXML

{

    recordResults = FALSE;

    

    NSString *soapMessage = [NSString stringWithFormat:

                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"

                             "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"

                             "<soap:Body>\n"

                             "<TestDecrypt xmlns=\"http://tempuri.org/\">\n"

                             "<tarData>%@</tarData>\n"

                             "</TestDecrypt>\n"

                             "</soap:Body>\n"

                             "</soap:Envelope>\n", tarXML

                             ];

    NSLog(soapMessage);

    

    NSURL *url = [NSURL URLWithString:@"http://sergeynotebook/WebService1/Service1.asmx"];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [theRequest addValue: @"http://tempuri.org/TestDecrypt" forHTTPHeaderField:@"SOAPAction"];

    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

    [theRequest setHTTPMethod:@"POST"];

    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    

    if( theConnection )

    {

        webData = [[NSMutableData data] retain];

    }

    else

    {

        NSLog(@"theConnection is NULL");

    }

    

}

 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    [webData setLength: 0];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [webData appendData:data];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"ERROR with theConenction");

    [connection release];

    [webData release];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSLog(@"DONE. Received Bytes: %d", [webData length]);

    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

    NSLog(theXML);

    [theXML release];

    

    

    [connection release];

    [webData release];

}

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Encrypt string with certificate from p12 file

by srudenko 26. March 2010 10:04

#import "NSDataAdditions.h"

 

@implementation Cryptography

 

const size_t BUFFER_SIZE = 240;

const size_t CIPHER_BUFFER_SIZE = 1024;

const uint32_t PADDING = kSecPaddingPKCS1;

 

- (NSString*) test1

{

    uint8_t *plainBuffer;

    uint8_t *decryptedBuffer;

    const char inputString[] = "some data";

    int len = strlen(inputString);

    

    plainBuffer = (uint8_t *)calloc(len, sizeof(uint8_t));

    

    strncpy( (char *)plainBuffer, inputString, len);

    

    

    NSString * path = [[NSBundle mainBundle] pathForResource:@"test1" ofType:@"p12"];

    assert(path != nil);

    

    NSData * data = [NSData dataWithContentsOfFile:path];

    assert(data != nil);

    

    

    CFArrayRef tmpCFArrayRef = CFArrayCreate(kCFAllocatorDefault, NULL, 0, NULL);//(CFArrayRef)items;

    

    NSMutableDictionary * options = [[NSMutableDictionary alloc] init];

// Set the public key query dictionary.

    [options setObject:@"some password for p12 file" forKey:(id)kSecImportExportPassphrase];

    SecPKCS12Import((CFDataRef) data, (CFDictionaryRef)options, &tmpCFArrayRef);

    

    NSMutableDictionary * items = (NSMutableDictionary*) [tmpCFArrayRef objectAtIndex:0];

    kCFAllocatorDefault, (const void **) &cert, 1, NULL);

    

    SecTrustRef trust = (SecTrustRef)[items objectForKey:(id)kSecImportItemTrust];

    

    pub_key_leaf = SecTrustCopyPublicKey(trust);

    int cipherBufferTotalSize = ceil(len/(float)BUFFER_SIZE)*256;

    uint8_t * cipherBuffer = (uint8_t *)calloc(cipherBufferTotalSize, sizeof(uint8_t));

    

    int procPlainBuffer = 0;

    int procCipherBuffer = 0;

    while (procPlainBuffer < len) {

        

        uint8_t * plainBufferChunc = (uint8_t *)calloc(BUFFER_SIZE, sizeof(uint8_t));

        uint8_t * cipherBufferChunc = (uint8_t *)calloc(256, sizeof(uint8_t));

        

        memcpy(plainBufferChunc, plainBuffer + procPlainBuffer, BUFFER_SIZE);

        

        [self encryptChunk :plainBufferChunc :cipherBufferChunc];

        procPlainBuffer += BUFFER_SIZE;

        

        memcpy(cipherBuffer + procCipherBuffer, cipherBufferChunc, 256);

        procCipherBuffer += 256;

        

        free(cipherBufferChunc);

        free(plainBufferChunc);

    }

    

    NSData* finalData = [[[NSData alloc] initWithBytes:cipherBuffer

                                                length:cipherBufferTotalSize] autorelease];

    

    NSString* retRes = [finalData base64Encoding];

    

    return retRes;

}

Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Hide back button

by alex 18. March 2010 13:40

[self.navigationItem setHidesBackButton:YES];

Currently rated 3.5 by 4 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Convert image to/from text (Base64)

by alex 14. March 2010 15:56

#import "NSDataAdditions.h"

 
 
 

-(NSString *)getStringFromImage:(UIImage *)image{

if(image){

NSData *dataObj = UIImagePNGRepresentation(image);

return [dataObj base64Encoding];

} else {

return @"";

}

}
 
//Convert back 

NSData *dataObj = [NSData dataWithBase64EncodedString:beforeStringImage];

UIImage *beforeImage = [UIImage imageWithData:dataObj];

 

NSDataAdditions.zip (2.80 kb)

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Check if camera is available

by alex 28. February 2010 12:34

if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){

cameraButton.enabled = NO;

} else {

cameraButton.enabled = YES;

}

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Show file included into project in UIWebView

by alex 22. February 2010 16:33

NSString *path = [NSString stringWithString:[[NSBundle mainBundle] pathForResource:@"feedback" ofType:@"html"]];

webView.opaque = NO;

webView.backgroundColor = [UIColor clearColor];

[webView setDelegate:self];

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:path isDirectory:NO]]];


Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Get MD5 hash from NSData

by alex 16. February 2010 10:57

#import "NSData+md5.h"

#import <CommonCrypto/CommonDigest.h>

 

 

@implementation NSData (Md5)

 

-(NSString*)md5{

const char *cStr = [self bytes];

unsigned char digest[CC_MD5_DIGEST_LENGTH];

CC_MD5( cStr, [self length], digest );

NSString* s = [NSString stringWithFormat: @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",

  digest[0], digest[1], 

  digest[2], digest[3],

  digest[4], digest[5],

  digest[6], digest[7],

  digest[8], digest[9],

  digest[10], digest[11],

  digest[12], digest[13],

  digest[14], digest[15]];

return s;

}


 

@end

Currently rated 3.7 by 3 people

  • Currently 3.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Get Coordinates from Address

by alex 15. February 2010 15:47

 

-(CLLocationCoordinate2D) addressLocation:(NSString *)input {

    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv"

  [input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];

    NSArray *listItems = [locationString componentsSeparatedByString:@","];

    double latitude = 0.0;

    double longitude = 0.0;

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {

        latitude = [[listItems objectAtIndex:2] doubleValue];

        longitude = [[listItems objectAtIndex:3] doubleValue];

    }

    else {

//Show error

    }

    CLLocationCoordinate2D location;

    location.latitude = latitude;

    location.longitude = longitude;

    return location;

}

 

Currently rated 4.4 by 7 people

  • Currently 4.428571/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Connect to Evernote

by alex 14. February 2010 14:02

#import "THTTPClient.h"

#import "TBinaryProtocol.h"

#import "UserStore.h"

#import "NoteStore.h"

 

 

- (void)Test

{

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

// Keep this key private

NSString *consumerKey = [[[NSString alloc]

  initWithString: @"YOUR_CONSUMER_KEY_HERE" ] autorelease];

NSString *consumerSecret = [[[NSString alloc]

initWithString: @"YOUR_CONSUMER_SECRET_HERE"] autorelease];

// For testing we use the sandbox server.

NSURL *userStoreUri = [[[NSURL alloc]

initWithString: @"https://sandbox.evernote.com/edam/user"] autorelease];

NSString *noteStoreUriBase = [[[NSString alloc]

  initWithString: @"http://sandbox.evernote.com/edam/note/"] autorelease];

// These are for test purposes. At some point the user will provide his/her own.

NSString *username = [[[NSString alloc]

  initWithString: @"YOUR_USERNAME_HERE"] autorelease];

NSString *password = [[[NSString alloc]

  initWithString: @"YOUR_PASSWORD_HERE"] autorelease];

THTTPClient *userStoreHttpClient = [[[THTTPClient alloc]

initWithURL:userStoreUri] autorelease];

TBinaryProtocol *userStoreProtocol = [[[TBinaryProtocol alloc]

  initWithTransport:userStoreHttpClient] autorelease];

EDAMUserStoreClient *userStore = [[[EDAMUserStoreClient alloc]

  initWithProtocol:userStoreProtocol] autorelease];

EDAMNotebook* defaultNotebook = NULL;

BOOL versionOk = [userStore checkVersion:@"Cocoa EDAMTest" :

  [EDAMUserStoreConstants EDAM_VERSION_MAJOR] :

  [EDAMUserStoreConstants EDAM_VERSION_MINOR]];

if (versionOk == YES)

{

EDAMAuthenticationResult* authResult =

[userStore authenticate:username :password

  :consumerKey :consumerSecret];

EDAMUser *user = [authResult user];

NSString *authToken = [authResult authenticationToken];

NSLog(@"Authentication was successful for: %@", [user username]);

NSLog(@"Authentication token: %@", authToken);

NSURL *noteStoreUri =  [[[NSURL alloc]

initWithString:[NSString stringWithFormat:@"%@%@",

noteStoreUriBase, [user shardId]] ]autorelease];

THTTPClient *noteStoreHttpClient = [[[THTTPClient alloc]

initWithURL:noteStoreUri] autorelease];

TBinaryProtocol *noteStoreProtocol = [[[TBinaryProtocol alloc]

  initWithTransport:noteStoreHttpClient] autorelease];

EDAMNoteStoreClient *noteStore = [[[EDAMNoteStoreClient alloc]

  initWithProtocol:noteStoreProtocol] autorelease];

NSArray *notebooks = [[noteStore listNotebooks:authToken] autorelease];

NSLog(@"Found %d notebooks", [notebooks count]);

for (int i = 0; i < [notebooks count]; i++)

{

EDAMNotebook* notebook = (EDAMNotebook*)[notebooks objectAtIndex:i];

if ([notebook defaultNotebook] == YES)

{

defaultNotebook = notebook;

}

NSLog(@" * %@", [notebook name]);

}

NSLog(@"Creating a new note in default notebook: %@", [defaultNotebook name]);

// Skipping the image resource section...

EDAMNote *note = [[[EDAMNote alloc] init] autorelease];

[note setNotebookGuid:[defaultNotebook guid]];

[note setTitle:@"Test note from Cocoa Test."];

NSMutableString* contentString = [[[NSMutableString alloc] init] autorelease];

[contentString setString: @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"];

[contentString appendString:@"<!DOCTYPE en-note SYSTEM \"http://xml.evernote.com/pub/enml.dtd\">"];

[contentString appendString:@" <en-note>Here is the Olive Tree Test note.<br/>"];

[contentString appendString:@" </en-note>"];

[note setContent:contentString];

[note setCreated:(long long)[[NSDate date] timeIntervalSince1970] * 1000];

EDAMNote *createdNote = [noteStore createNote:authToken :note];

if (createdNote != NULL)

{

NSLog(@"Created note: %@", [createdNote title]);

}

}

[pool drain];

}


Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen