Hide header with google groups inside UIWebView - ios

I'm displaying google groups view inside my UIWebView window but it displays the header with options to other google services. Is there a way to hide the header and just display the google groups content ?
This is the URL I'm using ...
https://groups.google.com/d/forum/MyAppGroupName

Use UIWebViewDelegate method webViewDidFinishLoad and run this JS script in
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *script = [NSString stringWithFormat:#"document.getElementById(\"og_head\").style.display='none';"];
[webView stringByEvaluatingJavaScriptFromString:script];
}

Related

Can i send data into webview?

i have created webview in my app,
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"SimpleCall" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webview loadHTMLString:htmlString baseURL: [[NSBundle mainBundle] bundleURL]];
[self.view addSubview:webview];
now i want to send a data to webview when webview starts load.is there any property for uiwebview?
thanks advance.
you must set the delegate UIWebViewDelegate property on your UIWebView, for example:
webView.delegate = self;
where self conforms to UIWebViewDelegate protocol and implement callback method webViewDidStartLoad (check docs) if you really want to do something when web view starts load. Then, you implement the method:
- (void)webViewDidStartLoad:(UIWebView *)webView {
//you can do something in here, but DON'T call any loaders of webView's - loadHTML or reload, cause this will result in a loop
}
But I doubt that will solve your problem - first of all you probably want to do something when web view finishes. Second - can you elaborate more on what you mean by:
send data to web view
DISCLAIMER: as of iOS 8 and higher you should use WKWebView instead.
EDIT: as clarified in comments you want to update dynamically html contents. In WKWebView docs you can read that there is no option to do that (i.e. dynamically insert options from your NSArray to loaded html string). You have to manually update your html string and then reload WKWebView with new, updated html. Reloading whole html just to update one feature is probably not an option here, so better store the webpage loaded on the server and update dynamically (when web page is updated with new options) by calling:
[webView reload];

UIWebView Source code

I have an IBAction on a button with the following code that I am trying to use to retrieve the source code of a UIWebView:
- (IBAction)loadInAWebView:(id)sender {
[self.webView loadHTMLString:[NSString stringWithFormat:#"Hello: %#", _name.text] baseURL:nil];
NSString *yourHTMLSourceCodeString = [_webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.innerHTML"];
NSLog(#"%#", yourHTMLSourceCodeString);
}
The use of the code above is from the reference I saw at this link: Getting the HTML source code of a loaded UIWebView.
However, the NSLog function always returns the following:
<head></head><body></body>
This obviously does not reflect the actual source code of the UIWebView that in my mind should be:
<head></head><body>Hello, name</body>
Indeed, the UIWebView returns the "Hello, name" content but the related source code does not.
How can I edit the code above to retrieve the real UIWebView source code?
You need implement UIWebViewDelegate methods - (void)webViewDidFinishLoad:(UIWebView *)webView and set self.webView.delegate = self; Put your code NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:#"document.documentElement.innerHTML"]; to this method

Can we use HTML code in SWIFT?

I have some attractive graph which is made in HTML and javascript.
So my questions are:
Can I use this work in my swift app,
Can we use javascript and html code to make graph in app?using webview
Can we pass values from swift/objective c code to javascript/html code and vice-versa?
If yes , How can we pass variable value to html/javascript and how to to accept this value in HTML
html text can be embedded into labels as NSAttributedString instances
HTML content that has scripts and stuff can be put into a web view container: UIWebView or WKWebView
YES. Create a webview then load this html page.
YES. (same as answer 1)
YES .
To send to html page: call [webView stringByEvaluatingJavaScriptFromString:function];
To send to swift/objective-c : handle shouldStartLoadWithRequest of your UIwebview.
-(BOOL) webView : (UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *) request navigationType : (UIWebViewNavigationType)navigationType {
if ([[[request URL] absoluteString] hasPrefix:#"yourPrefixe:"]) {
//do your works
//then cancel current request
return NO;
}
return YES;
}

Can I create a link from within my webview browser that will call a specific function

We are using the native geolocation data in the address string we use when we create a webview component (eg http://example.com?long=36.333&latt=-143.222). The reason we are doing this is that we want to check location but don't want to use the browser location facility which pops up an alert each day asking if we can use your location.
Ideally I want to be able to create a new webview component (with new location data) in response to a click on a link within the webview component. I know we can call the app from a link (eg myappname://) but can I add parameters to this call to trigger certain events such as a function that we reload the webview component?
If it cant be done from a link within the html, I assume I will have to place a native button on the page and call it from there.
You can do. First of all, set link like http://example.com?#SetCustomLocation in your HTML content.
Set WebView Delegate method like bellow :
-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType
{
if ( inType == UIWebViewNavigationTypeLinkClicked )
{
NSString *url = [NSString stringWithFormat:#"%#", [inRequest URL]];
if([url hasSuffix:#"#SetCustomLocation"])
{
NSString *strURL = #"http://example.com?long=36.333&latt=-143.222"; //Set Custom URL with Lat/long Whatever you need
NSURL *cURL = [NSURL URLWithString:[strURL tringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:cURL];
}
}
return YES;
}
Regards.

Problems using UIWebView to implement info page

In my app I have a UIWebView-based view controller showing info and credits about the app itself. As part of the info, the app version is displayed, as retrieved from the infoDictionary of the mainBundle. So this string is not in the original HTML; it is set programmatically, by replacing a placeholder.
Therefore the sequence is:
1) I load the HTML into a NSString
2) I replace the placeholder with the actual version
3) I show the resulting string in UIWebView by invoking method loadHTMLString:baseURL:
The HTML also has hyperlinks to some web pages in the internet.
Everything is fine, but for this problem:
If I touch a hyperlink, and therefore navigate to the corresponding web page, I will not be able to go back to my original info page (canGoBack returns NO, goBack does nothing).
Any suggestion?
Thanks in advance.
In the end, I have found a satisfactory solution, that I would like to share.
1) I changed the HTML code of my info page, by inserting the following code where I want to show the app version:
<p>App version:
<script type="text/javascript">
var args = document.location.search.substring(1).split("&");
for (var i in args) {
var nameValue = args[i].split("=");
if (nameValue[0] == "version")
var version = nameValue[1];
}
if (version)
document.write(version);
</script>
</p>
This piece of code will parse the query part of the URL, looking for a "version=<version>" argument, and use its value to show the app version.
2) When preparing the URL to be used by the UIWebView, I retrieve the app version from the main bundle:
NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:#"CFBundleVersion"];
Then I append it at the end of my URL string:
NSString* urlString = [NSString stringWithFormat:#"<my_info_page>.html?version=%#", version];
3) Finally, I use the UIWebView to show the page:
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSURLRequest* request [[NSURLRequest alloc] initWithUrl:url];
[mWebView loadRequest:urlRequest];
[urlRequest release];
[url release];
where mWebView is an IBOutlet, of type UIWebView, properly connected to the web view in the XIB file.
It works correctly on my iPhone 4, including the back/forward functions of the web view when following hyperlinks, while keeping the user inside the app.
I would break out of the app and load Safari in this case:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
The only downside is that the user comes out of your app. You could put an alertview in that method to warn them first...
Alternatively, you'll have to code your own back button.

Resources