Can't convert nsurl into base 64 - iphone

I am doing this way. but i am not getting where is wrong.
NSString *strUrl = [NSString stringWithFormat:#"https://play.google.com/stor/apps/details?id=com.ShiftSharerfree_new&feature=search_result#?t=W251bGwsMSwyLDEsImNvbS5TaGlmdFNoYXJlcmZyZWVfbmV3Il"];
NSString *originalString = [NSString stringWithFormat:#"%#", strUrl];
NSData *data = [NSData dataFromBase64String:originalString];
NSString *data = [base64 originalString];
NSLog(#"data:%#", data);//[data base64EncodedString]);
Please guide me in above.

Perhaps you need a base64 conversion library, as mentioned here:
Convert between UIImage and Base64 string
I had a similar error with code for converting images to base64, using [base64 encode...] calls.

the first thing i'd noted is that the NSData and the NSString have the same name. Could this be the error?

Related

NSUTF8StringEncoding or stringWithUTF8String

I am having some issues in dealing with characters in NSStrings.
I read a XML file converted to NSData, it is okay, but when I transfer the "Element name" it has not converted to UTF-8.
I've tried here are some examples of the site, but with no success.
My Code is -
NSString * S = [NSString stringWithFormat: # "%#", D];
S = [S stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
Try this..
NSString *url = S;
NSString *check=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
check=[check stringByReplacingOccurrencesOfString:#"%0A" withString:#""];

NSURL URLWithString: giving nil

Please see the below code :
UIImage *image;
NSString *str = [[[Data getInstance]arrPic]objectAtIndex:rowIndex];
NSLog(str);
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL:url];
image = [UIImage imageWithData:data];
str is giving me http://MyDomain/Pics\\1.png but url is giving me nil.
Just try using this,
[NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
From the documentation, the URLWithString: methods takes a well-formed URL string :
This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘#’. Note that ‘%’ escapes are translated via UTF-8.
I suggest you retry the same using NSString's (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding; method before.
As of iOS9, stringByAddingPercentEscapesUsingEncoding is deprecated. To safely escape a URL string, use:
NSMutableCharacterSet *alphaNumSymbols = [NSMutableCharacterSet characterSetWithCharactersInString:#"~!##$&*()-_+=[]:;',/?."];
[alphaNumSymbols formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
str = [str stringByAddingPercentEncodingWithAllowedCharacters:alphaNumSymbols];
This creates sets of characters to keep as is and asks everything outside of these CharacterSets to be converted to %percent encoded values.

NSString encoding for accents displays crazy characters

I am downloading data (text) from a server.
I have tried with both:NSISOLatin1StringEncoding and NSASCIIStringEncoding
But I keep seeing things like: {"estado":"M\u00e9xico"}
Noting that it should read México and not M\u00e9xico (with an accent over the e).
Looking online I figured that \u00e9 is in fact é link.
But the NSString is not able to interpret this and instead prints weird things on my UILabels:
I would really really appreciate your help on this.
Alsso, if you are itnerested, you can download the data from here: http://www.miorden.com/demo/iphone/estadoJSON.php
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.miorden.com/demo/iphone/estadoJSON.php"]];
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(#"Downloaded: %#", string);
string = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.miorden.com/demo/iphone/estadoJSON.php"] encoding:NSISOLatin1StringEncoding error:nil];
NSLog(#"Downloaded: %#", string);
I have been literally trying for days and it is killing me!
Thank you so much!
That appears to be unicode, try NSUTF8StringEncoding.
The data is in JSON format, so you'll need to JSON decode it too.
For example using this: https://github.com/TouchCode/TouchJSON
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://www.miorden.com/demo/iphone/estadoJSON.php"]];
NSError *error;
NSArray *array = [[CJSONDeserializer deserializer] deserializeAsArray:data error:&error];
NSLog(#"Test: %#", [[array objectAtIndex:11] valueForKey:#"estado"]);
outputs
2011-08-11 09:35:45.742 enctest[63236:407] Test: México

how to decode UTF8 string in iphone

I want to decode an UT8 encoded string.
The input string is "øæ-test-2.txt"
and after decoding it should become
"øæ-test-2.txt"
I found many API to encode the NSString or NSData to UT8 (NSUTF8StringEncoding) but was not able to find the way to decode it.
What I have tried until now:-
NSString *str = [[NSString alloc] initWithUTF8String:[strToDecode cStringUsingEncoding:NSUTF8StringEncoding]];
AND
[strToDecode stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
AND
[NSString stringWithUTF8String:[strToDecode cStringUsingEncoding:[NSString defaultCStringEncoding]]]
I have tried the same input string and I get the proper output in third party decoder.
But was not able to get success
Any hint in right direction would be highly appreciated.
I use this one.
NSString *encoded_string = #"ü";
const char *ch = [encoded_string cStringUsingEncoding:NSISOLatin1StringEncoding];
NSString *decode_string = [[NSString alloc]initWithCString:ch encoding:NSUTF8StringEncoding];
NSLog(#"%#",decode_String)
[NSString stringWithUTF8String:]

to print local xml file through NSData?

i want to take some xml data from local path,and to parse,but when i use following code
NSLog returns(content) different texts which is differed from xml file, how can i get exact xml data to check ,it consists correct xml data or not? any help please? when i parse , it returns nothing..i have saved the file as .xml and copied to local resource folder?
NSString *xmlFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"samp.xml"];
NSString *xmlFileContents = [NSString stringWithContentsOfFile:xmlFilePath];
NSData *data = [NSData dataWithBytes:[xmlFileContents UTF8String] length:[xmlFileContents lengthOfBytesUsingEncoding: NSUTF8StringEncoding]];
NSString *content=[[NSString alloc]
initWithBytes:[data bytes]
length:[data length]
encoding:NSUTF8StringEncoding];
NSLog(#"%#",content);
This is almost assuredly an encoding problem. Make sure your xml file is in UTF8 or convert it to UTF8 before you try to create the NSData object. Once that's done, the following code produces the same output as input.
NSString *open = [NSString stringWithContentsOfFile: [#"~/Desktop/note" stringByExpandingTildeInPath] encoding: NSUTF8StringEncoding error: NULL];
NSData *data = [NSData dataWithBytes: [open UTF8String] length: [open lengthOfBytesUsingEncoding: NSUTF8StringEncoding]];
NSString *save = [NSString stringWithUTF8String: [data bytes]];
[save writeToFile: [#"~/Desktop/note2" stringByExpandingTildeInPath] atomically: NO encoding: NSUTF8StringEncoding error: NULL];
You'll probably want to use one of the NSXML classes, unless you want to do all of the parsing yourself.