UIWebview Loading same html page again and again? - html

I have a UIWebView in my viewcontroller, and when I tried to open an epub file it shows correctly but every epub thereafter shows the same cover page (first page) and its contents are different.
Why is this happeneing? Is there cache inside the codes?
In chapter class
- (void) loadChapterWithWindowSize:(CGRect)theWindowSize fontPercentSize:(int) theFontPercentSize
{
// [[NSURLCache sharedURLCache] removeAllCachedResponses];
fontPercentSize = theFontPercentSize;
windowSize =theWindowSize;
// NSLog(#"webviewSize: %f * %f, fontPercentSize: %d", theWindowSize.size.width, theWindowSize.size.height,theFontPercentSize);
UIWebView* webView = [[UIWebView alloc] initWithFrame:theWindowSize];
[webView setDelegate:self];
NSURLRequest* urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:spinePath]];
//CODE FOR IGNORING CACHE
//NSURLRequest* urlRequest ;
//urlRequest=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:spinePath] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10000];
//[webView loadRequest:urlRequest];
NSLog(#"%#",spinePath);//html pahe path
[[UIApplication sharedApplication] openURL:[NSURL fileURLWithPath:spinePath]];
};
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
[webView release];
}
- (void) webViewDidFinishLoad:(UIWebView*)webView
{
[webView stringByEvaluatingJavaScriptFromString:#"document.open();document.close()"];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
sharedManager=[Mymanager sharedManager];
NSString *varMySheet = #"var mySheet = document.styleSheets[0];";
NSString *addCSSRule = #"function addCSSRule(selector, newRule) {"
"if (mySheet.addRule) {"
"mySheet.addRule(selector, newRule);" // For Internet Explorer
"} else {"
"ruleIndex = mySheet.cssRules.length;"
"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);" // For Firefox, Chrome, etc.
"}"
"}";
// NSLog(#"w:%f h:%f", webView.bounds.size.width, webView.bounds.size.height);
NSString *insertRule1 = [NSString stringWithFormat:#"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
NSString *insertRule2 = [NSString stringWithFormat:#"addCSSRule('p', 'text-align: justify;')"];
NSString *setTextSizeRule = [NSString stringWithFormat:#"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')",fontPercentSize];
[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];
[webView stringByEvaluatingJavaScriptFromString:insertRule1];
[webView stringByEvaluatingJavaScriptFromString:insertRule2];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
int totalWidth = [[webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollWidth"] intValue];
pageCount = (int)((float)totalWidth/webView.bounds.size.width);
//NSLog(#"Chapter %d: %# -> %d pages", chapterIndex, title, pageCount);
[webView dealloc];
[delegate chapterDidFinishLoad:self];
}
in my viewocntroller class
- (void) chapterDidFinishLoad:(Chapter *)chapter{
i=i+1;
NSLog(#"iteration%d",i);
NSLog(#"%d",[loadedEpub.spineArray count]);
loadingProgressBar=loadingProgressBar+1;
sharedManager=[Mymanager sharedManager];
NSLog(#"%d",sharedManager.coverPageloadCount);
//sharedManager.coverPageloadCount++;
totalPagesCount+=chapter.pageCount;
sharedManager.TotalPageNumber=totalPagesCount;
if(chapter.chapterIndex + 1 < [loadedEpub.spineArray count]){
[[loadedEpub.spineArray objectAtIndex:chapter.chapterIndex+1] setDelegate:self];
[[loadedEpub.spineArray objectAtIndex:chapter.chapterIndex+1] loadChapterWithWindowSize:webView.bounds fontPercentSize:currentTextSize];
[currentPageLabel setText:[NSString stringWithFormat:#"?/%d", totalPagesCount]];
}
else {
[currentPageLabel setText:[NSString stringWithFormat:#"%d/%d",[self getGlobalPageCount], totalPagesCount]];
[pageSlider setValue:(float)100*(float)[self getGlobalPageCount]/(float)totalPagesCount animated:YES];
paginating = NO;
//NSLog(#"Pagination Ended!");
}
- (void)webViewDidFinishLoad:(UIWebView *)thewebView{
// [self TextureModes];
NSUserDefaults *menuUserDefaults = [NSUserDefaults standardUserDefaults];
if([menuUserDefaults boolForKey:#"btnM1"]){
[webView setOpaque:NO];
[webView setBackgroundColor:[UIColor whiteColor]];
NSString *jsString2 = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
[webView stringByEvaluatingJavaScriptFromString:jsString2];
}
else{
NSUserDefaults *userDefaults2 = [NSUserDefaults standardUserDefaults];
[userDefaults2 setBool:NO forKey:#"btnM1"];
[userDefaults2 synchronize];
[webView setOpaque:NO];
[webView setBackgroundColor:[UIColor blackColor]];
NSString *jsString = [[NSString alloc] initWithFormat:#"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
[webView stringByEvaluatingJavaScriptFromString:jsString];
}
if(sharedManager.textureFlag==1)
{
webView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"darkWoodP.png"]];
}else if(sharedManager.textureFlag==2)
{
webView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"paperP.png"]];
}
NSString *varMySheet = #"var mySheet = document.styleSheets[0];";
NSString *addCSSRule = #"function addCSSRule(selector, newRule) {"
"if (mySheet.addRule) {"
"mySheet.addRule(selector, newRule);" // For Internet Explorer
"} else {"
"ruleIndex = mySheet.cssRules.length;"
"mySheet.insertRule(selector + '{' + newRule + ';}', ruleIndex);" // For Firefox, Chrome, etc.
"}"
"}";
NSString *insertRule1 = [NSString stringWithFormat:#"addCSSRule('html', 'padding: 0px; height: %fpx; -webkit-column-gap: 0px; -webkit-column-width: %fpx;')", webView.frame.size.height, webView.frame.size.width];
NSString *insertRule2 = [NSString stringWithFormat:#"addCSSRule('p', 'text-align: justify;')"];
NSString *setTextSizeRule = [NSString stringWithFormat:#"addCSSRule('body', '-webkit-text-size-adjust: %d%%;')", currentTextSize];
NSString *setHighlightColorRule = [NSString stringWithFormat:#"addCSSRule('highlight', 'background-color: yellow;')"];
[webView stringByEvaluatingJavaScriptFromString:varMySheet];
[webView stringByEvaluatingJavaScriptFromString:addCSSRule];
[webView stringByEvaluatingJavaScriptFromString:insertRule1];
[webView stringByEvaluatingJavaScriptFromString:insertRule2];
[webView stringByEvaluatingJavaScriptFromString:setTextSizeRule];
[webView stringByEvaluatingJavaScriptFromString:setHighlightColorRule];
NSString *padding = #"document.body.style.padding='30px 30px 30px 30px';";
NSString *paddingtop = #"document.body.style.padding-top='85px';";
NSString *paddingbottom = #"document.body.style.margin-bottom='85px';";
[webView stringByEvaluatingJavaScriptFromString:padding];
[webView stringByEvaluatingJavaScriptFromString:paddingtop];
[webView stringByEvaluatingJavaScriptFromString:paddingbottom];
if(currentSearchResult!=nil){
// NSLog(#"Highlighting %#", currentSearchResult.originatingQuery);
[webView highlightAllOccurencesOfString:currentSearchResult.originatingQuery];
}
totalWidth = [[webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.scrollWidth"] intValue];
// NSLog(#"%d %f",totalWidth,webView.bounds.size.width);
pagesInCurrentSpineCount = (int)((float)totalWidth/webView.bounds.size.width);
[self gotoPageInCurrentSpine:currentPageInSpineIndex];
}

Try an experiment: instead of reloading the web view, release it and replace it with a new one (properly niling and setting delegates). If the new web view shows the same content it must be the files. If that fixes things you have now one way to fix it.

Related

show route in newly launch google map app

I'm looking for way to redirect user from our app to google map app to show route between two location. currently apple map is supporting route in united state only but i want to publish my app for other regions.
before few days apple launches google map app for ios, i want to redirect my user inside it.
right now i'm using following code to redirect user in apple maps :
NSString * myLocString = [NSString stringWithFormat:#"40.737893,-73.990173"];
NSString * destString = [NSString stringWithFormat:#"40.733893,-73.993173"];
NSString* queryString = [NSString stringWithFormat:#"saddr=%#&daddr=%#",myLocString,destString];
NSString* searchQuery = nil;
searchQuery = [queryString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* urlString = [NSString stringWithFormat:#"http://maps.apple.com/maps?%#",searchQuery];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
Simple way to achieve this :
BOOL openurlflag=[[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:#"comgooglemaps://"]];
if (openurlflag==0)// open apple maps {
NSString * myLocString = [NSString stringWithFormat:#"40.737893,-73.990173"];
NSString * destString = [NSString stringWithFormat:#"40.733893,-73.993173"];
NSString* queryString = [NSString stringWithFormat:#"saddr=%#&daddr=%#",myLocString,destString];
NSString* searchQuery = nil;
searchQuery = [queryString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* urlString = [NSString stringWithFormat:#"http://maps.apple.com/maps?%#",searchQuery];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
} else if (openurlflag==1) // open google maps {
NSString * myLocString = [NSString stringWithFormat:#"40.737893,-73.990173"];
NSString * destString = [NSString stringWithFormat:#"40.733893,-73.993173"];
NSString* queryString = [NSString stringWithFormat:#"saddr=%#&daddr=%#&directionsmode=transit",myLocString,destString];
NSString* searchQuery = nil;
searchQuery = [queryString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString* urlString = [NSString stringWithFormat:#"comgooglemaps://?%#",searchQuery];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]]; }
finally i got one link where you can do it by changing URLScheme.
https://developers.google.com/maps/documentation/ios/urlscheme

DTAttributedTextCell with clickable link

I'm trying to reuse DTAttributedTextCell with clickable link. I could get hold of the attributedstring but I'm not sure how i can get the frame for the text such that I can create a DTLinkButton.
Here's the sample code from the Demoapp:
- (void)configureCell:(DTAttributedTextCell *)cell forIndexPath:(NSIndexPath *)indexPath
{
NSDictionary *snippet = [_snippets objectAtIndex:indexPath.row];
NSString *title = [snippet objectForKey:#"Title"];
NSString *description = [snippet objectForKey:#"Description"];
NSString *html = [NSString stringWithFormat:#"<h3>%#</h3><p>%#</font>Test</p>", title, description];
[cell setHTMLString:html];
cell.attributedTextContextView.shouldDrawImages = YES;
}
Any pointers will be awesome.

Three20 Navigation: View don't show

I rework my app to use Three20 and I wanna use Three20 navigation now.
Here is my code, that works perfectly before:
ENSListViewController *vc = [ENSListViewController alloc];
NSArray *ensArray;
NSDictionary *dic;
NSInteger folder_id;
NSString* folder_type;
NSString* barTitle;
NSString* folderName;
if (indexPath.section == 0)
{
ensArray = [ensFolderList objectForKey:#"an"];
dic = [ensArray objectAtIndex:indexPath.row];
folder_type = #"an";
barTitle = [NSString stringWithFormat:#"%#", [dic objectForKey:#"name"]];
folder_id = [[dic objectForKey:#"ordner_id"] intValue];
folderName = [dic objectForKey:#"name"];
}
else
{
ensArray = [ensFolderList objectForKey:#"von"];
dic = [ensArray objectAtIndex:indexPath.row];
folder_type = #"von";
barTitle = [NSString stringWithFormat:#"%#", [dic objectForKey:#"name"]];
folder_id = [[dic objectForKey:#"ordner_id"] intValue];
folderName = [dic objectForKey:#"name"];
}
vc.folder_id = folder_id;
vc.folder_type = folder_type;
vc.barTitle = barTitle;
vc.folderName = folderName;
[vc initWithNibName:#"ENSListViewController" bundle:nil];
[self.view addSubview:vc.view];
It works perfectly.
It allocs a ViewController, sets a lot of data in the ViewController (Properties) and then show the view.
Here is my code now:
NSArray *ensArray;
NSDictionary *dic;
NSInteger folder_id;
NSString* folder_type;
NSString* barTitle;
NSString* folderName;
if (indexPath.section == 0)
{
ensArray = [ensFolderList objectForKey:#"an"];
dic = [ensArray objectAtIndex:indexPath.row];
folder_type = #"an";
barTitle = [NSString stringWithFormat:#"%#", [dic objectForKey:#"name"]];
folder_id = [[dic objectForKey:#"ordner_id"] intValue];
folderName = [dic objectForKey:#"name"];
}
else
{
ensArray = [ensFolderList objectForKey:#"von"];
dic = [ensArray objectAtIndex:indexPath.row];
folder_type = #"von";
barTitle = [NSString stringWithFormat:#"%#", [dic objectForKey:#"name"]];
folder_id = [[dic objectForKey:#"ordner_id"] intValue];
folderName = [dic objectForKey:#"name"];
}
/*
vc.folder_id = folder_id;
vc.folder_type = folder_type;
vc.barTitle = barTitle;
vc.folderName = folderName;
[vc initWithNibName:#"ENSListViewController" bundle:nil];
//[self.view addSubview:vc.view];
*/
NSString *url = [NSString stringWithFormat:#"tt://ensList/%#/%#/%d/%#/%#/%#", #"ENSListViewController", nil, folder_id, folder_type, barTitle, folderName];
TTURLAction *action = [TTURLAction actionWithURLPath:url];
[[TTNavigator navigator] openURLAction:action];
Here is my Navigator:
navigator = [TTNavigator navigator]; // create the navigator
navigator.persistenceMode = TTNavigatorPersistenceModeAll; // and he will save the data :)
TTURLMap* map = navigator.URLMap;
[map from: #"tt://ens"
toSharedViewController: [ENSOverviewViewController class]];
[map from: #"tt://ensList/(initWithNibName:)/(bundle:)/(folderId:)/(folderType:)/(barTitle:)/(folderName:)" toViewController:[ENSListViewController class]
transition:3];
And here is my new Constructor method:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self LoadENSList];
}
return self;
}
- (void) initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil folderId:(NSInteger)folder_id2 folderType:(NSString*)folder_type2 barTitle:(NSString*)barTitle2 folderName:(NSString*)folderName2
{
self.folder_id = folder_id2;
self.folder_type = folder_type2;
self.barTitle = barTitle2;
self.folderName = folderName2;
[self initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}
So, if you read it up to here: big thanks!
Now my problem is: The view doesn't open. Nothing happens.
I think there is a mistake in my self made constructor, the order of calling my constructor or something like this. Im on it since 2 hours but can't find the error.
I know Three20 is much undocumented and I am not expacting a fast answer, but if anyone have an idea: please comment or answer.
Found the solution:
1) I forget the return-value in my constructur.
After adding this (changing (void) to (id) and add "return self") it goes on...
2) After he changes in 1) the system crashes because initWithNibName throws an NSInvalidArgument error.
After changing this to init, it works perfectly

Searching for keywords in HTML

The iOS app I'm writing displays an HTML page, and I would like to add a search feature where the user can search for instances of a keyword and highlight them.
What's the best way to do this?
NSString *filePath = PATH_OF_HTML_FILE;
NSError *err = nil;
NSString *pageHTML = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&err];
if(err)
{
pageHTML = [NSString stringWithContentsOfFile:filePath encoding:NSASCIIStringEncoding error:&err];
}
if([searchTxtField.text length])
{
NSRange range1 = [pageHTML rangeOfString:searchTxtField.text options:NSCaseInsensitiveSearch];
if(range1.location != NSNotFound)
{
NSString *highlightedString = [pageHTML substringWithRange:range1];
pageHTML = [pageHTML stringByReplacingOccurrencesOfString:highlightedString withString:[NSString stringWithFormat:#"<span style=\"background-color:yellow; color:red;\">%#</span>",highlightedString] options:NSCaseInsensitiveSearch range:NSMakeRange(0, [pageHTML length]) ];
[webView loadHTMLString:pageHTML baseURL:[NSURL fileURLWithPath:filePath]];
}
}

MFMailComposeViewController csv attachment not being attached, but showing inline instead

I am having a problem with sending csv attachments via MFMailComposeViewController.
Sometimes they come through just fine, but for other users they don't come through as attachments, but rather as text inline in the email (with <br/> instead of line returns.) It's very strange. Anybody know what I'm doing wrong?
Here is a snippet of my code:
MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
mailComposeViewController.mailComposeDelegate = self;
NSString *csv = #"foo,bar,blah,hello";
NSData *csvData = [csv dataUsingEncoding:NSUTF8StringEncoding];
[mailComposeViewController addAttachmentData:csvData mimeType:#"text/csv" fileName:#"testing.csv"];
[mailComposeViewController setSubject:#"testing sending csv attachment"];
[mailComposeViewController setMessageBody:#"csv file should be attached" isHTML:NO];
[self presentModalViewController:mailComposeViewController animated:YES];
-(IBAction)btnPressed:(id)sender {
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString *Path = [docDir stringByAppendingString:#"/CSVFile.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:Path];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"For csv file..."];
[controller setMessageBody:#"...csv file is hear.." isHTML:NO];
[controller addAttachmentData:csvData mimeType:#"text/csv" fileName:#"CSVFile.csv"];
[self presentModalViewController:controller animated:YES];
[controller release];
}
Hi I put sample code for Creating CSV file and attach it with mail but make sure you have to add MessageUI.Framework and import its related header "MessageUI/MessageUI.h"
"MessageUI/MFMailComposeViewController.h" and deligate "MFMailComposeViewControllerDelegate"...I hope this wl useful for others
- (void)viewDidLoad {
arrCsv=[[NSArray alloc]initWithObjects:#"Hello",#"Hi",#"traun",#"fine",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:#"%#/try.csv", documentsDirectory];
[[arrCsv componentsJoinedByString:#","] writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL];
}
-(ibAction)btnMail {
NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString *Path = [docDir stringByAppendingString:#"/CSVFile.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:Path];
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:#"For csv file..."];
[controller setMessageBody:#"...csv file is hear.." isHTML:NO];
[controller addAttachmentData:csvData mimeType:#"text/csv" fileName:#"CSVFile.csv"];
[self presentModalViewController:controller animated:YES];
[controller release];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{ message.hidden = NO;
switch (result)
{
case MFMailComposeResultCancelled:
message.text = #"Result: canceled";
break;
case MFMailComposeResultSaved:
message.text = #"Result: saved";
break;
case MFMailComposeResultSent:
message.text = #"Result: sent";
break;
case MFMailComposeResultFailed:
message.text = #"Result: failed";
break;
default:
message.text = #"Result: not sent";
break;
}
[self dismissModalViewControllerAnimated:YES];
}
set the mime type as "application/octet-stream" and that should do the trick to remove inline attachments (I still named the extension of my file i.e. pdf)
I believe the second parameter to setMessageBody:isHTML: must be YES for attachments to not show up inline.
Even if you set isHTML param to YES, your message body can be sent as plain/text if the message body can be represented as such. And attachments in plain/text messages are not always recognized correctly by some email clients (Outlook).
In my case adding a link in the message body helped. Formatting text as bold with HTML tags works too. Tricky!
Tested on iPod 1G 3.1.3.
This may not be the case here, but one thing to watch out for is that:
[NSString dataUsingEncoding:]
returns a valid but empty NSData object if the conversion to the specified encoding is not possible. Better to use the full version:
[NSString dataUsingEncoding: s allowLossyConversion: YES]
Or check the length of the returned data. It appears that zero-length data attachments are trimmed somewhere in the mail process.