Wednesday, June 30, 2010

pointer to integer

If you get the error “warning:comparison between pointer and integer”, odds are you are thinking right, but implementing your comparison wrong.

ur doin it wrong

if(count == 2) {

the right way

if([count intValue] == 2) {

Tuesday, June 29, 2010

saving xml locally

///////////////////////////////
NSURL *url = [[NSURL alloc] initWithString:@"http:/xml.php"];

NSData *data = [NSData dataWithContentsOfURL:url];
NSString *applicationDocumentsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"icellxml.php"];


NSLog(@"new xml %@",storePath );

// write to file atomically (using temp file)
[data writeToFile:storePath atomically:TRUE];

NSLog(@" saved data is %@",storePath);

// NSData *data = [NSData dataWithBytes:ptr length:len]; to get back


////////////////////////////

NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:storePath]) {
//open it and read it
NSLog(@"data file found. reading into memory");

} else {
NSLog(@"no file found. creating empty array");

}



////////// PULLING DATA IN OFFLINE MODE



NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:storePath]) {
//open it and read it
NSLog(@"data file found. reading into memory");




NSURL *urla = [[[NSURL alloc] init] fileURLWithPath:storePath];





storePath = [storePath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

NSString *urlb = [[NSURL fileURLWithPath:storePath] absoluteString];




NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory,
NSUserDomainMask, YES
);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSURL *urlc = [NSURL fileURLWithPath:[documentsDirectory
stringByAppendingString:@"/icellxml1.php" ]];


NSLog(@"welcome %@",documentsDirectory );
NSLog(@"werkgwkerwekwegrw %@",urlc);






NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:urlc];

//Initialize the delegate.

XMLParser *parser = [[XMLParser alloc] initXMLParser];

//Set delegate

[xmlParser setDelegate:parser];

//Start parsing the XML file.

BOOL success = [xmlParser parse];

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

if(success)
{
NSLog(@"BINGO");
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
else
{
NSLog(@"DAM DAM DAM !!!");
}

Monday, June 28, 2010

pull data from string

lets say locationString = 1,2,3,4

NSLog(@" GEO MAP is %@",locationString);

//NSString *myData = [NSString stringWithContentsOfFile:myPath];
NSArray *posArray = [[NSArray alloc] initWithArray:[locationString componentsSeparatedByString:@","]];
NSLog(@"count = %d",[posArray count]);
NSLog(@"first one = %f", [[posArray objectAtIndex:2] floatValue]);
NSLog(@"2nd one = %f", [[posArray objectAtIndex:3] floatValue]);


output will be 3,4

getting lati and longi

theAddress = [addressInput.text stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv",
theAddress];
NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];

Sunday, June 27, 2010

saving and retreving data

/// setting dic
NSUserDefaults *saveDict = [NSUserDefaults standardUserDefaults];
[saveDict setObject:geotext forKey:@"keyToLookupString"];

// getting dic

NSUserDefaults *gete = [NSUserDefaults standardUserDefaults];

// getting an NSString
NSString *myString = [gete stringForKey:@"keyToLookupString"];
NSLog(@" this output is %@",myString);

Thursday, June 24, 2010

sending image and text to server

NSString *uTitle=text.text;
NSString *uMessage=myTextView.text;
NSData *imageData = UIImageJPEGRepresentation(imageView.image, .9);
NSString *post =[[NSString alloc] initWithFormat:@"&title=%@&message=%@&imagedetal=%@",uTitle,uMessage,imageData];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

///////////////////////////


// setting up the URL to post to
NSString *urlString = @"http:/_post.php";

// setting up the request object now
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];


[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];


NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];




NSMutableData *body = [NSMutableData data];
//[body appendData:[[NSString stringWithFormat:@"%@", post] dataUsingEncoding:NSUTF8StringEncoding]];

// title
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:uTitle] dataUsingEncoding:NSUTF8StringEncoding]]; // title
// message
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:uMessage] dataUsingEncoding:NSUTF8StringEncoding]]; // title



/// image

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"ipodfile.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"action=upload&"] dataUsingEncoding:NSUTF8StringEncoding]];
///





[body appendData:[NSData dataWithData:imageData]];







[request setHTTPBody:body];
NSString *msgLength = [NSString stringWithFormat:@"%d", [body length]];
[request addValue: msgLength forHTTPHeaderField:@"Content-Length"];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

NSLog(returnString);
[self alert];

sending lattitude and long to server

// assemble the POST data

NSString *post = [NSString stringWithFormat:@"Latitude=%@&Longitude=%@¬e=%@",
[self urlEncodeValue:latitude],
[self urlEncodeValue:longitude],
[self urlEncodeValue:note],
];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:@"http://www.mypositionserver.com/position.php"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

Tuesday, June 22, 2010

post images to server

http://iphone.zcentric.com/2008/08/29/post-a-uiimage-to-the-web/

Monday, June 21, 2010

camera control on iphne

http://icodeblog.com/2009/07/28/getting-images-from-the-iphone-photo-library-or-camera-using-uiimagepickercontroller/


this is the link for camera demo

Thursday, June 17, 2010

Base 64 implementation

i got it form cocoadev.com


//// class name Base64.h
#import


@interface Base64 : NSObject {

}
+ (void) initialize;
+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length;
+ (NSString*) encode:(NSData*) rawBytes;
+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength;
+ (NSData*) decode:(NSString*) string;
@end


///


/// class name Base64.m

#import "Base64.h"


@implementation Base64
#define ArrayLength(x) (sizeof(x)/sizeof(*(x)))

static char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char decodingTable[128];

+ (void) initialize {
if (self == [Base64 class]) {
memset(decodingTable, 0, ArrayLength(decodingTable));
for (NSInteger i = 0; i < ArrayLength(encodingTable); i++) {
decodingTable[encodingTable[i]] = i;
}
}
}


+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length {
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;

for (NSInteger i = 0; i < length; i += 3) {
NSInteger value = 0;
for (NSInteger j = i; j < (i + 3); j++) {
value <<= 8;

if (j < length) {
value |= (0xFF & input[j]);
}
}

NSInteger index = (i / 3) * 4;
output[index + 0] = encodingTable[(value >> 18) & 0x3F];
output[index + 1] = encodingTable[(value >> 12) & 0x3F];
output[index + 2] = (i + 1) < length ? encodingTable[(value >> 6) & 0x3F] : '=';
output[index + 3] = (i + 2) < length ? encodingTable[(value >> 0) & 0x3F] : '=';
}

return [[[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding] autorelease];
}


+ (NSString*) encode:(NSData*) rawBytes {
return [self encode:(const uint8_t*) rawBytes.bytes length:rawBytes.length];
}


+ (NSData*) decode:(const char*) string length:(NSInteger) inputLength {
if ((string == NULL) || (inputLength % 4 != 0)) {
return nil;
}

while (inputLength > 0 && string[inputLength - 1] == '=') {
inputLength--;
}

NSInteger outputLength = inputLength * 3 / 4;
NSMutableData* data = [NSMutableData dataWithLength:outputLength];
uint8_t* output = data.mutableBytes;

NSInteger inputPoint = 0;
NSInteger outputPoint = 0;
while (inputPoint < inputLength) {
char i0 = string[inputPoint++];
char i1 = string[inputPoint++];
char i2 = inputPoint < inputLength ? string[inputPoint++] : 'A'; /* 'A' will decode to \0 */
char i3 = inputPoint < inputLength ? string[inputPoint++] : 'A';

output[outputPoint++] = (decodingTable[i0] << 2) | (decodingTable[i1] >> 4);
if (outputPoint < outputLength) {
output[outputPoint++] = ((decodingTable[i1] & 0xf) << 4) | (decodingTable[i2] >> 2);
}
if (outputPoint < outputLength) {
output[outputPoint++] = ((decodingTable[i2] & 0x3) << 6) | decodingTable[i3];
}
}

return data;
}


+ (NSData*) decode:(NSString*) string {
return [self decode:[string cStringUsingEncoding:NSASCIIStringEncoding] length:string.length];
}

@end



/// main class import base64.h


and here we go

//
NSString *post1 =[NSString stringWithFormat:@"%@|%@",tfUsername.text, tfPassword.text];


NSData *data = [post1 dataUsingEncoding: NSASCIIStringEncoding];
NSData *encryptedData = [post1 dataUsingEncoding: NSASCIIStringEncoding];


[Base64 initialize];


NSString *b64EncStr1=[Base64 encode:encryptedData];

NSLog(@"Base 64 encoded = %@",b64EncStr1);

NSScanner

If you want to pull data from xml then use NSScanner :)



NSString *a =serverOutput2;// ur string
NSString *checkForValidity;


NSScanner *scanner = [NSScanner scannerWithString:a];

[scanner scanUpToString:@"" intoString:NULL];// write the word u want to scan in @
[scanner scanString:@"" intoString:NULL];
[scanner scanUpToString:@"
" intoString:&checkForValidity];
NSLog(@"%@",checkForValidity);

FEEDJIT Live