remove uiwebview href grey selection - iphone

i have searched the net, but the given solutions doesn't fit my needs..
So i have a webview, and my webview loads data like this:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[_webView loadHTMLString:htmString baseURL:baseURL];
That html string has anchor tags with href e.g - < a href=http://url.com/ ... /> and i trigger some events on shouldStartLoadWithRequest.
Now the problem is i don't know how to remove the grey selection when this attribute is pressed. I saw people are doing this:
<a href=http://yourlink.com/ style = "-webkit-tap-highlight-color:rgba(0,0,0,0);">
and some similar things, but i want to do it other way. By other way i'm talking that i don't want to change my htmlString, because it may have a lot of them, and injecting style in every seems not the perfect solution..
So i tried doing like this:
[_webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.style.webkitTapHighlightColor = \"rgba(0,0,0,0);\""];
after i load html string, but it doesn't work.
So anybody has some suggestions, or knows what am i doing wrong?
EDIT:
Or maybe a simple injection of javascrypt for the behavior i want?
Thanks in advance!!!

This code should do the trick:
- (void)webViewDidFinishLoad:(UIWebView *)webView
NSString *js = #"var styleNode = document.createElement('style');\n"
"styleNode.type = 'text/css';\n"
"var styleText = document.createTextNode('a {-webkit-tap-highlight-color:rgba(0,0,0,0)}');\n"
"styleNode.appendChild(styleText);\n"
"document.getElementsByTagName('head')[0].appendChild(styleNode);\n";
[_webView stringByEvaluatingJavaScriptFromString:js];
}

Related

"Inject" Objective-C data into a UIWebView that loads a local HTML file?

I am trying to load a UIWebView with local HTML/CSS that is build to look like a nutrition label. The problem is, the data for the food lies inside of my iPhone app. Do I have to put all of my HTML into one enormous NSString object and concatenate my data into it, or is there a way to load the HTML from a local .html file, but somehow "inject" the data that is stored within Objective-C into it?
If the data to be injected is "safe", you could construct your "enormous NSString object" as a format string, sprinkled with %# markers, and use stringWithFormat: to perform the injection in a single move. This is how I construct the pages in the TidBITS News app, using pieces that all come from RSS. It's really quite painless.
You can load basic html using NSData's method dataWithContentsOfFile and then use javascript to modify html in the way you need.
Code would look something like this (using this example):
NSString *path = [[NSBundle mainBundle] pathForResource:#"food" ofType:#"html"];
NSData *data = [NSData dataWithContentsOfFile:path];
if (data) {
[webView loadData:data MIMEType:#"text/html" textEncodingName:#"UTF-8"];
}
[webView stringByEvaluatingJavaScriptFromString:#"var script = document.createElement('script');"
"script.type = 'text/javascript';"
"script.text = \"function myFunction() { "
"var field = document.getElementById('field_3');"
"field.value='Calling function - OK';"
"}\";"
"document.getElementsByTagName('head')[0].appendChild(script);"];
[webView stringByEvaluatingJavaScriptFromString:#"myFunction();"];
I would do a hybrid of both- have an HTML file in the app that you load, then replace certain strings in that before giving it to the UIWebView. So for example, you could have a file like this
<html>
<head>
<title><!--foodName--></title>
</head>
<body>
<h1><!--foodName--></h1>
<p>Calories / 100g: <!--foodCalories--></p>
</body>
</html>
You'd load that into Cocoa, then replace your special placeholder comments with the actual values you want.
NSDictionary *substitutions = [NSDictionary dictionaryWithObjectsAndKeys:
#"Carrots", #"foodName",
[NSNumber numberWithInt:20], #"foodCalories",
// add more as needed
nil];
NSMutableString *html = [NSMutableString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"foodCard" ofType:#"html"]
encoding:NSUTF8StringEncoding
error:nil];
for(NSString *substitutionKey in substitutions)
{
NSString *substitution = [[substitution objectForKey:substitutionKey] description];
NSString *searchTerm = [NSString stringWithFormat:#"<!--%#-->", substitutionKey];
[html replaceOccurrencesOfString:searchTerm withString:substitution options:0 range:NSMakeRange(0, [html length])];
}
[webView loadHTMLString:html baseURL:[[NSBundle mainBundle] resourceURL]];
Since iOS 2 you can use - (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script within a UIWebView subclass to execute JS scripts in your webview. This is the best way to inject data from the "Objective-C part" of your application.
Cf: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIWebView_Class/#//apple_ref/occ/instm/UIWebView/stringByEvaluatingJavaScriptFromString:

UitextField and UIWebView interaction?

I put some text into the textField like: "Peter.at", and the UIwebview shows all correct (Mag. pharm. Peter MÜLLER)!
The Problem is when I type in the UItextField: "Püter.at", the UIwebView does nothing!
What is wrong? I think it is the "ü" but I don't know how to fix it!
Yep. I see what the problem is.
Your code should look like this:
NSString *preURL = #"";
NSString *middleURL = #".at";
NSString *fullURLString =
[[NSString stringWithFormat:#"%#%#%#",preURL,1.text,middleURL] stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSURL * preparedURL = [[NSURL URLWithString:fullURLString];
[uiwebnamesearch loadRequest: [NSURLRequest requestWithURL: preparedURL]];
[uiwebnamesearch reloadInputViews];
The important change here is that you are "percent escaping" the string, to make it possible to use in a URL.
Here is the documentation for that [NSString stringByAddingPercentEscapesUsingEncoding:] method.

Loading webpage into UIwebview but images from local

I have web page having 1000's of images and values. Whenever I load the page to UIWebView it gets loaded with values but as there are so many images it takes time to download.
So is there any way, I can download the page from web but images from local.
Any help is really appreciated.
Thank you,
Ankita
If you have your HTML from webserver and images at local
yourBaseURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:#"YourHTML" baseURL:yourBaseURL]
and your HTML contains name of local image stored like
Image Tag like --> imgage src='fig1.jpg width=150 height=150 align="left"
Than this should work.
Not exactly as you required though this way you can load local images in webView.
Basically:
Download HTML from server.
NSURL *URL = [[NSURL alloc] initWithString:#"http://www.example.com/page.php"];
NSError *error = nil;
NSStringEncoding encoding;
NSString *theSource = [[NSString alloc] initWithContentsOfURL:URL usedEncoding:&encoding error:&error];
Replace the file references to load images locally.
Note the double slashes, this seems important.
// Replace:
<img src="File.png">
// By something like:
<img src="file://Path//To//Resources//File.png">
Detailed information how to do this (check post by Joe D'Andrea):
Link to resources inside WebView - iPhone
Finally make the UIWebView load the HTML:
[webView loadHTMLString:htmlString baseURL:baseURL];
Now it should load the 'fresh' HTML from the server with local images.
This works quite well for me.
NSURL *myBaseURL = [[NSURL alloc] initFileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *myString = [[managedObject valueForKey:#"long_presentation"] stringByReplacingOccurrencesOfString:#"/FooFolder/BarFolder" withString:[NSString stringWithFormat:#"%#/FooFolder/BarFolder", [[NSBundle mainBundle] bundlePath] ] ];
[attractionWebView loadHTMLString:myString baseURL:myBaseURL];
I had to include a replacement for the image source
img src="FooFolder/BarFolder/my_image.jpg" ...
to get the right path.

Xcode: How to add an integer to a NSURL

im kind of a noob on objective c and still are having trouble with some basics, in this case
I'm developing a pdf viewer on xcode, but im having a problem adding a integer to my NSUrl variable.
I have several pdf's on my site:
"http://mysite.com/1.pdf" "2.pdf" etc.
but the number of pdf files allways change so in order to avoid using a NSMutableArray with all the url sites what im trying to make is a NSURL variable like this:
int numpag = 1;
NSString *urlAddress = #"http://mysite.com/%d.pdf", numpag;
[pdfView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlAdress]]];
is there a correct way to do this?
Thanks in advance
Try;
NSString *urlAddress = [NSString stringWithFormat:#"http://mysite.com/%d.pdf",numpag];
int numpag = 1;
NSString *baseURLStr = #"http://mysite.com/";
NSURL *url = [NSURL URLWithString:[baseURLStr stringByAppendingFormat:#"%d.pdf", numpag]];

Remap UIWebView root URL to [[NSBundle mainBundle] bundleURL]

I've got some HTML and some images in my iPhone app, arranged something like:
html/
foo.html
images/
bar.png
I can get bar.png to appear in my UIWebView a couple of different ways -- either loading foo.html from an NSUrl, and walking back up the directory tree from the html directory:
<img src="../images/bar.png"/>
or by loading foo.html into a string, using loadHtmlString, and using [[NSBundle mainBundle] bundleURL] as the baseURL:
<img src="images/bar.png"/>
Both of these are kind of clumsy, though -- in the first case, if I move HTML files around I have to rejigger all the relative paths, and in the second case, I have to ignore the actual path structure of the HTML files.
What I'd like to make work is this --
<img src="/images/bar.png"/>
-- treating the bundleURL as the root of the "site". Is there any way to make this work, or am I doomed to have that translated into file:///images/bar.png and have the file not found?
Only way I can see for you to do this would be to embed a web server in your app. Matt Gallagher has a blog post on this you could start from. Alternatively, CocoaHTTPServer and Mongoose could be dropped into your project.
If I'm not mistaken, you have some files in your project bundle that you want to load in your web view. You can do it simply with these few lines of code:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"bar" ofType:#"png"];
NSURL *imageURL = [NSURL fileURLWithPath:imagePath];
I'm assuming that you have a text/html file containing the pattern for your web view. You'll need to add the image as an object there (src="%#"...) and then add the imageURL to the pattern:
NSString *path = [[NSString alloc]initWithString:[[NSBundle mainBundle]pathForResource:#"htmlPattern" ofType:#"html"]];
NSError *error;
NSString *pattern = [[NSString alloc]initWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:&error];
htmlPage = [[NSString alloc]initWithFormat:pattern,
imageURL;
webView = [[UIWebView alloc] initWithFrame:WEBVIEW_FRAME];
[webView loadHTMLString:htmlPage baseURL:[NSURL URLWithString:path]];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pattern]]];