iphone charset howto - iphone

i've a class (downloaded from internet) that read a value of a file in this way (and after split the file):
NSArray* array = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
where fileContents is my fullpathoffile.
in which way i must write my string?
i'm trying with this:
NSString *data = [NSString stringWithFormat:#"%#,%#""\n", latitudine.text, longitudine.text];
NSFileHandle *handle;
handle = [NSFileHandle fileHandleForWritingAtPath: [self dataFilePath] ];
[handle truncateFileAtOffset:[handle seekToEndOfFile]];
[handle writeData:[data dataUsingEncoding:NSUTF8StringEncoding]];
i'm going crazy :D
Any helpS?!?

i've solve in this way:
NSString *data = somethingblablabla ;
NSData *myData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSFileHandle *writeData = [NSFileHandle fileHandleForWritingAtPath:[self dataPath]];
[writeData truncateFileAtOffset:[writeData seekToEndOfFile]];
[writeData writeData:myData];

Related

Convert this url to base 64

I tried this way but it's not working.
This is the url to convert to base 64
NSString *strUrlB64 = [NSString stringWithFormat:#"https://play.google.com/store/apps/details?id=com.ShiftSharerfree_new&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5TGlmdFNoYXJlcmZyZWVfbmV3Il0"];
NSData *dataUrl = [NSData dataFromBase64String:strUrlB64];
Then tried to send this to another url.
NSString *urlStr = [NSString stringWithFormat:#"http://server39.pnht.com/360ad/apps/ads/%#/android/136/ord0.910950258132325?json=1&package_url=%#",self.mStrPid, dataUrl];
It is giving me output: NSString *urlStr = [NSString stringWithFormat:#"http://server39.pbgs.com/360ads/apps/ad/%#/android/1360/ord0.910950252132325?json=1&package_url=%#",self.mStrPid, url];
It is not base 64. please guide how to get base 64 out of it.
I am using base 64 to encode and decode some text,
can u tell me if it works with URLs,
- (NSString *)base64Encode:(NSString *)plainText
{
NSData *plainTextData = [plainText dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainTextData base64EncodedString];
return base64String;
}
- (NSString *)base64Decode:(NSString *)base64String
{
NSData *plainTextData = [NSData dataFromBase64String:base64String];
NSString *plainText = [[NSString alloc] initWithData:plainTextData encoding:NSUTF8StringEncoding];
return plainText;
}

Question concerning File Handling

i got the following problem :
i am trying to create a file with the following Code :
NSString *homeDirectory = NSHomeDirectory();
NSString *documentDirectory = [homeDirectory stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:#"Test.txt"];
NSFileHandle *savedFile = nil;
savedFile = [[NSFileHandle FileHandleForWritingAtPath:filePath]retain];
NSString *data = #"testmessage";
[savedFile writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[savedFile synchronizeFile];
[savedFile closeFile];
My Question is the following. Running this on a MAC does not give me any Errors whatsoever. But the File itself seems to be nonexistant or at least not locateable.
I need to find and access the File and i have to be able to export it to my Mac using a mobile iOS Device
thanks in advance :)
h4wkeye
You need to check that savedFile is not nil (see the docs here).
NSString *homeDirectory = NSHomeDirectory();
NSString *documentDirectory = [homeDirectory stringByAppendingPathComponent:#"Documents"];
NSString *filePath = [documentDirectory stringByAppendingPathComponent:#"Test.txt"];
NSFileHandle *savedFile = nil;
savedFile = [[NSFileHandle fileHandleForWritingAtPath:filePath]retain];
if (nil == savedFile) {
NSLog(#"Could not create saved file at %#", filePath);
}
NSString *data = #"testmessage";
[savedFile writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
[savedFile synchronizeFile];
[savedFile closeFile];

iphone posting data to a url

This is the string I need to post http://www.mysite.com/save?details={"name"}
Iam trying the following code;
NSString *post =[NSString stringWithFormat:#"details={\"name\"}"];
NSString *hostStr = #"http://www.mysite.com/save?";
hostStr = [hostStr stringByAppendingString:post];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: hostStr]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
but the serverOutput always = null, unless i do just {name}. but i need the qutoes in there :(
You should convert everythign to valid HTTP symbols.
e.g.:
hostStr = [hostStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
...my guess is that your webserver is refusing your invalid URL.
wrong ..
NSString *post =[NSString stringWithFormat:#"details={\"name\"}"];
Correct:
NSString *post =[NSString stringWithString:#"details={\"name\"}"];

NSData not working correctly

NSData *myRequest = [NSString stringWithFormat:(#"&site=%#&key=%#",tmpSite,tmpKey)];
Why is this not working ?
Thanks
Problem is that you are assigning NSString instance to variable of NSData type. Try this:
NSString* s = [NSString stringWithFormat:#"&site=%#&key=%#",tmpSite,tmpKey];
NSData* d = [s dataUsingEncoding:NSASCIIStringEncoding]; //or any other encoding!
NSData *myRequest = [[NSString stringWithFormat:(#"&site=%#&key=%#",tmpSite,tmpKey)] dataUsingEncoding: NSASCIIStringEncoding];
You can't just mix the two classes like that.

how to update data from web to exisitng file for iphone

NSString *applicationDocumentsDir =
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:#"sample1.cfg"];
NSURL *instructionsURLd = [[NSURL alloc] initFileURLWithPath:filePath];
NSData *dataXML = [NSData dataWithContentsOfURL:instructionsURLd];
[dataXML writeToFile:storePath atomically:YES];
NSLog(#"matrix path %#",applicationDocumentsDir);
NSLog(#"neo path %#",storePath);
using this i can save data from my app to Documents folder right now sample1.cfg ia having 10 data now i want to add 10 more from web when user click on update button ?? so how to add 10 more data plz help
Thanks
...
NSData *dataXML = [NSData dataWithContentsOfURL:instructionsURLd];
NSMutableData *file = [[NSMutableData alloc] initWithContentsOfFile:storePath];
[file appendData:dataXML];
[file writeToFile:storePath atomically:YES];
...
This'll append the dataXML. If you want to prepend the data, use this:
...
NSMutableData *dataXML = [[NSData dataWithContentsOfURL:instructionsURLd] mutableCopy];
NSData *file = [[NSData alloc] initWithContentsOfFile:storePath];
[dataXML appendData:file];
[dataXML writeToFile:storePath atomically:YES];
...