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

Icons for universal application (iPhone/iPad)

by alex 17. May 2010 12:52

//Add icnon files to projects

//Add following to info.plist

 

<key>CFBundleIconFiles</key>

<array>

<string>Icon.png</string>

<string>iconiPad.png</string>

</array>

Currently rated 5.0 by 1 people

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

Tags:

Write NSString to file

by srudenko 26. March 2010 12:04

 

//Method writes a string to a text file

-(void) writeToTextFile{

//get the documents directory:

NSArray *paths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

//make a file name to write the data to using the documents directory:

NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt"

  documentsDirectory];

//create content - four lines of text

NSString *content = @"One\nTwo\nThree\nFour\nFive";

//save content to the documents directory

[content writeToFile:fileName 

  atomically:NO 

encoding:NSStringEncodingConversionAllowLossy 

  error:nil];

}

 

Currently rated 5.0 by 1 people

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

Tags:

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

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen