Problems with escaped characters in JSON string - iphone

I have a JSON-string where I know where the problem is, I just can't figure out what to do. I have looked up the "forbidden characters" in a JSON-string but it just doesn't work.
When you run the show-method for FBStreamDialog for iPhone a view comes up with how it's going to look like when it's finally posted on the wall.
This happends when the "description"-property in my JSON-string is hard coded like #"Testing". But as soon as I add the text fetched from a data source which looks like this, it doesn't work:
"description":"
LIVE: Uk's No:1 Reggae Singer Bitty Mclean + Joey Fever, Sthlms No:1 Reggae Voice.
DJs
Deejay Flash & Micke Goulos + Mc Fabulous G.
The Vinyl Bar
Up...
"
Note: I only show the "description"-property of the JSON-string, because there is where the problem is.
So what I tried to do was, as I've explained before, to add the string "Testing" in the "description"-property. This worked. But I wanted to have the data source property of "description", of course. So I tried to replace all the characters that isn't a letter with this code:
shortString = [shortString stringByReplacingOccurrencesOfString:#"&" withString:#"och"];
shortString = [shortString stringByReplacingOccurrencesOfString:#"+" withString:#"plus"];
shortString = [shortString stringByReplacingOccurrencesOfString:#"," withString:#"komma"];
shortString = [shortString stringByReplacingOccurrencesOfString:#"'" withString:#"apostrof"];
shortString = [shortString stringByReplacingOccurrencesOfString:#":" withString:#"colon"];
The output of that is:
"description":"LIVEcolon Ukapostrofs Nocolon1 Reggae Singer Bitty Mclean plus Joey Feverkomma Sthlms Nocolon1 Reggae Voice.
DJs
Deejay Flash och Micke Goulos plus Mc Fabulous G.
The Vinyl Bar
Up...
Which looks like a approvable JSON string?
But apparently not, because the facebook view never shows how it's going to look if I use the data source "description"-property. It just shows the text box "What's on your mind".
This is driving me crazy.

Finally!
I understand why you didn't answer this question. How could you know that facebook connect doesn't allow \n in their StreamDialog's. Not for iPhone anyway.
So the solution was to replace \n with a whitespace or whatever you want.

Related

How to use UITextInputMode

I need create a method which detects keyboard input language . for example when keyboard language is French does something and when is English does something else
I search on the Internet and found UITextInputMode but I don't know how to use it , I would be grateful if you help me . thanks
It's quite simple, you can do it this way:
UITextInputMode *textInput = [UITextInputMode currentInputMode];
NSString *primaryLanguage = textInput.primaryLanguage;
NSLog(#"Current text input is: %#", primaryLanguage);
As noted in Apple docs, "The value of this property is a BCP 47 language code such as “es”, “en-US”, or “fr-CA”".
If you need to be notified about changes, you add your controller as observer for UITextInputCurrentInputModeDidChangeNotification

problem with unicode in NSString

i have string like: "YouTube - ‪VID 0001‬‏" and i want the string like : YouTube - VID 0001 that means i want to remove the unicodes.
is there a way to remove unicodes like ‪ in iphone apps, if so please help me.
Does this similar question and solution solve your problem?
I'd also recommend heeding the advice given in the comments on the linked question and making sure that you have a valid reason for not wanting unicode.
Try this if you just want to remove the quotes (")
NSString *urString = #"\"YouTube - VID 0001‏\"";
NSString *formattedString = [urString stringByReplacingOccurrencesOfString:#"\"" withString:#""];

decoding problem uiwebview

i am using uiwebview in my application. there are some links when user clicks a http search starts. it works fine but i have problems while getting "%58 den ysnky'ye tepki" it is given as "X'den ysnky'ye tepki". it has problems with % char.
identifier:%58'den%20ysnky'ye%20tepki
decoded identifier:X'den ysnky'ye tepki
i am using stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding to decode the string like that;
NSLog(#"identifier:%#", identifier);
identifier = [identifier stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"decoded identifier:%#", identifier);
how can i get the correct string?
thanks...
It looks like your string may not be encoded properly in the first place. %58 is the correct encoding for the letter “X” (see this ASCII table). As far as I can tell, therefore, the decode is behaving properly.
What are you expecting?

How to store text as paragraphs in SQLite database in a iPhone app?

In my iPhone app, I have a requirement to store a huge amount of text. I have paragraphs of text to be stored in my database along with the newline characters.
What should I do to store the text as paragraphs in SQLite database?
For example, I want to store paragraphs like the ones below in:
(the mother of the faithful believers) The commencement of the Divine Inspiration to Allah's Apostle was in the form of good dreams which came true like bright day light, and then the love of seclusion was bestowed upon him. He used to go in seclusion in the cave of Hira where he used to worship (Allah alone) continuously for many days before his desire to see his family. He used to take with him the journey food for the stay and then come back to (his wife) Khadija to take his food like-wise again till suddenly the Truth descended upon him while he was in the cave of Hira. The angel came to him and asked him to read. The Prophet replied, "I do not know how to read.
The Prophet added, "The angel caught me (forcefully) and pressed me so hard that I could not bear it any more. He then released me and again asked me to read and I replied, 'I do not know how to read.'
Basically I want to save the paragraphs in database in the same format with carriage returns.
It depends on what you mean by huge and how you're planning on showing the data. The SQLite TEXT field, by default, can store 1 billion bytes.
You could in theory store all of it in a TEXT field in SQLite, then render it in a UIScrollView (or whatever it is you're using to render) and check the performance, memory usage, etc.
If the performance is unacceptable, you can try "chunking" the text into multiple rows and displaying only the records of the text required for the UI.
See the SQLite Limits document:
Maximum length of a string or BLOB
The maximum number of bytes in a string or BLOB in SQLite is defined by
the preprocessor macro
SQLITE_MAX_LENGTH. The default value
of this macro is 1 billion (1 thousand
million or 1,000,000,000). You can
raise or lower this value at
compile-time using a command-line
option like this:
-DSQLITE_MAX_LENGTH=123456789
On the face of it, SQLite doesn't treat newlines any differently than other characters; you can just store the test as-is.
The issue, though, is why are you storing large volumes of raw text in SQLite? If you want to search it or organize it somehow, SQLite (nor Core Data) is probably not the best choice without first massaging the text into some other form. Or, alternatively, you'd want to store the raw text on disk then keep some kind of searchable index in the database.
My suggestion would be if you want to display your text in a webview then add HTML tags to your text.So in that way you can add paragraphs,New lines and many other effects to your text.
Thanks
so do you want to split the text into paragraph and store each in its own row like:
(paragraph_number, text_of_paragraph)
that would be:
create table paragraphs (paragraph_number, text_of_paragraph);
then in what ever language you use split the text into a list of (pn, tp) named l and do like:
executemany("insert into paragraphs values (?, ?)", l)
or do like:
for p in l:
execute("insert into paragraphs values (?, ?)", p)
i would use HTML to represent my paragraphs (i.e)
Saving the Text
<div>
<p>(the mother of the faithful believers) The commencement of the Divine Inspiration to Allah's Apostle was in the form of good dreams which came true like bright day light, and then the love of seclusion was bestowed upon him. He used to go in seclusion in the cave of Hira where he used to worship (Allah alone) continuously for many days before his desire to see his family. He used to take with him the journey food for the stay and then come back to (his wife) Khadija to take his food like-wise again till suddenly the Truth descended upon him while he was in the cave of Hira. The angel came to him and asked him to read. The Prophet replied, "I do not know how to read.</p>
<p>The Prophet added, "The angel caught me (forcefully) and pressed me so hard that I could not bear it any more. He then released me and again asked me to read and I replied, 'I do not know how to read.</p>
</div>
Loading the Paragraphs
I would load them inside a UIWebView as html, you can save the HTML into a file in the app sandbox let's say Paragraph1.HTML load it as the following:
// this is a user defined method
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSURL *url = [NSURL fileURLWithPath:sFilePath];// Path of the HTML File
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[web loadRequest:request];
}
dispose the File after loading it, this will save you much time and space.
Good luck.

iPhone: Reading Text From File and UISegmentedControl

First off, I'm a complete beginner.
That said, I thought an ambitious longer-term project/learning experience would be to create an app that displayed daily quotes, like those cheesy day-by-day calendars our grandmothers have in their bathrooms. I want it to have two per day, each one represented by a tab in a UISegmentedControl. That's the long term. Right now I'd be happy with getting a single day's worth of quotes functioning.
Onto the questions:
How can I get text saved in a .txt or .rtf file to be displayed in a UITextView? Preferably without using 'stringWithContentsOfFile,' since Xcode is telling me that's deprecated.
How can I get content from a different file (or maybe a different portion of the same file...?) to be displayed when the user taps the second segment?
If I can get it running so that those two conditions are met and I understand what's going on, I'll consider the day a success. Thanks!
1.
NSError *error = nil;
NSStringEncoding stringEncoding;
NSString *fileText = [NSString stringWithContentsOfFile:#"/path" usedEncoding:&stringEncoding error:&error];
myTextView.text = fileText;
The error and encoding are optional, and you can pass in nil for both. But if you care about the error, or what encoding the file was in they will have useful info in them after the string is created.
2.
Set the valueChanged outlet in Interface Builder to an IBAction on your controller, such as setSegmentValue:. Then, assuming you have an array of quote strings:
- (IBAction)setSegmentValue:(id)sender {
UISegmentedControl *control = (UISegmentedControl*)sender;
NSString *quote = [quotes objectAtIndex:control.selectedSegmentIndex];
myTextView.text = quote;
}
Even though stringWithContentsOfFile: is deprecated, stringWithContentsOfFile:usedEncoding:error: is not. That is the standard method to use for reading from files.
As for the second question, you simply test the state of the segmented control and perform as action based on it. Admittedly this is a high level answer but should get you going.