editing occurrences is not working - ios

i am trying to get the path to a certain file
and then open it in a webview. so i need to replace each space by '%20'
NSString *test=#"filename";
NSString *finalPath12 = [test stringByAppendingString:#".pdf"];
NSString *path1 = [[NSBundle mainBundle] bundlePath];
NSString *finalPath1 = [path1 stringByAppendingPathComponent:finalPath12];
NSString *file =#"file://";
NSString *htmlfilename1 = [file stringByAppendingString:finalPath1];
NSString *pathtofile = [htmlfilename1 stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
any string is working except #"%20".
this works perfectly for exemple:
NSString *pathtofile = [htmlfilename1 stringByReplacingOccurrencesOfString:#" " withString:#"string"];
but i need the #"%20". What am i missing ? Thanks

There is already [NSString stringByAddingPercentEscapesUsingEncoding:] (reference) for that very purpose.
However in your case, as you want a URL, you can replace all the lines in your question with:
NSURL *url = [[NSBundle mainBundle] urlForResource:#"filename" withExtension:#"pdf"];

You need to use #"%%20".
As first % is treated as escape/wild character.
Or use
stringByAddingPercentEscapesUsingEncoding:

Related

how to replace space with dash in objective-c?

Hey folkks I'm working with an api that search about movies now the problem is when i type single word in UISearchBar it works but when I type space for another word it dose'nt work.Near query=%# when i type single word it works but when I type another word with space it won't
NSString *movieName=searchBar.text;
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
You don't need to replace Space with Dash you new to URL Encode your string
Below is an example of encoding your string to URL Encode
NSString *movieName=searchBar.text;
movieName = [movieName stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLUserAllowedCharacterSet]];
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
Use
formattedString = [originalString stringByReplacingOccurrencesOfString:#" " withString:#"-"];
Please refer this code.
NSString *movieName=searchBar.text;
NSString* replacedMovieName = [movieName stringByReplacingOccurrencesOfString:#" " withString:#"_"];
Hope this helps.
No need replace space with dash, need to add Percent Escapes in string before creating NSURL.
Use NSString class method:
-(NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding
Like,
NSString *movieName=searchBar.text;
//This is what you need
movieName=[movieName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
OR
Alternately you can do following(Not best approach):
NSString *movieName=searchBar.text;
//This is what you need
movieName = [movieName stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];
NSString *movieName=searchBar.text;
movieName = [movieName stringByReplacingOccurrencesOfString:#" " withString:#"-"];//use vice-versa
NSString *movieString = [NSString stringWithFormat:#"https://api.themoviedb.org/3/search/movie?query=%#&api_key=c4bd81709e87b1209433c49",movieName];
NSURL *url=[NSURL URLWithString:movieString];

How to read a text file ,turn it into a string, then using arguments on it

Say If I had a string = "There were %# apples,I ate %#. Now I have %# apples" in text file "Apples.txt". This is how I will extract the text from it.
NSString *path = [[NSBundle mainBundle] pathForResource:#"Apples" ofType:#"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
Now how do I pass the arguments to it so it looks like this:
"There were %# apples,I ate %#. Now I have %# apples", x, y, x-y
I am sure there is a way around this using NString with arguments? using
NSString with local argument functions? otherwise I will have to type all of my text in the file.m
This very crucial for my app.
You are probably looking for [NSString stringWithFormat:...].
Your complete code goes like this :
NSString *path = [[NSBundle mainBundle] pathForResource:#"Apples" ofType:#"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSNumber *total = #100;
NSNumber *ate = #34;
NSString *fullString = [NSString stringWithFormat:content,total,ate, #([total integerValue] - [ate integerValue])];
NSLog(#"%#", fullString);
Output:
"There were 100 apples,I ate 34. Now I have 66 apples".
Use + stringWithFormat:
Here's the Apple reference

How to set image into uiwebview?

I have created a html code like see above and the code uploaded to http://www.ged.Ic.com site.
Added an image name “1.jpg” into Xcode project.
When open iPad app loading http://www.ged.Ic.com/page/ from webview.
How to get to set image from into Xcode project to img src="image path from xcode"
NSString *imageFileName = #"image";
NSString *imageFileExtension = #"png";
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageFileName ofType:imageFileExtension];
NSString *imgHTMLTag = [NSString stringWithFormat:#"<img src=\"file://%#\" />", imagePath];
NSString *imageUrlString = #"http://www.gstatic.com/translate/intl/ru/logo.png";
NSString *yourText = #"Some text here";
NSString *htmlString = [NSString stringWithFormat:#"<img src='%#'/><br><b>%#</b>", imageUrlString, yourText];
[myWebView loadHTMLString:htmlString baseURL:nil];

How to read last n lines in a Text file in Objective C

I have a Text file and it has lot of lines How can i get last 'n' number of lines from the text file? and Can we give Numbers in the text file for each file How can we get it.
You could use NSFileHandle, seekToEndOfFile and then work backwards from the offsetInFile using seekToFileOffset: and readDataOfLength: scanning the data read each time for carriage returns and counting them until you get to the required number. As you go you can build up the text after each scan.
One way is putting \n to separate your different lines in the text file. Then
NSString* path = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"txt"];
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:NULL];
NSArray* lines = [content componentsSeparatedByString: #"\n"];
Then you can take the last few elements in the array.
Hope this helps..
Try to use this one:
NSString* textFile = [[NSBundle mainBundle]
pathForResource:#"fileName" ofType:#"txt"];
NSString* fileContents = [NSString stringWithContentsOfFile: textFile
encoding: NSUTF8StringEncoding error: nil];
Separate by new line
NSArray* allLinedStrings =
[fileContents componentsSeparatedByCharactersInSet:
[NSCharacterSet newlineCharacterSet]];
Here you can get your last 100 objects
NSString* oneLineStr;
for (int i = allLinedStrings.count - 100; i < allLinedStrings.count; i++)
{
oneLineStr = [allLinedStrings objectAtIndex: i];
NSLog#("New Line %#", oneLineStr);
}

UIWebview - Loading PNG image - got error

I am trying to load the image(png) in UIWebview. But I got the following error
""ImageIO: PNGinvalid literal/lengths set"
I will get the "png" from the web service. I will store it in the following path. "Users/XXX/Library/Application Support/iPhone Simulator/4.3.2/Applications/E4DE3097-EA84-492F-A2A7-5882202F3B00/Documents/attachement.png "
When I reading the file from the path, I got the error. I am using the following code
NSBundle *bundle = [NSBundle mainBundle];
NSString *path = [bundle bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
NSString *commentHtml = [NSString stringWithFormat:#"<img src=\"%#\"/>", documentEntity.path];
NSString *html3 = [NSString stringWithFormat:#"<html><head/><body><div><b>COMMENTS:</b></div>%#</body></html>", commentHtml];
[self.documentView loadHTMLString:html3 baseURL:baseURL];
Please help me.
THanks
Girija
If you have saved the Image in Documents Folder, why are you fetching from MainBundle.
Fetching Image from the Documents Directory would be :
NSString *aDocumentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *aFilePath = [NSString stringWithFormat:#"%#/attachement.png", aDocumentsDirectory];
UIImage *anImage = [UIImage imageWithContentsOfFile:aFilePath];

Resources