Unable to change colour in UIWebView - html

I'm using the following code it is working fine if I want to make a HTML page but it is not working on UIWebView. it looks like don't know why web view is not rendering the HTML tags. Is there any way I can fix it?
NSString *htmlString = [NSString stringWithFormat:#"<html><head><style type='text/css'>body { color:#FFFFFF; background-color: #000000; }</style></head><body>%#</body></html>",string];
[webView loadHTMLString:htmlString baseURL:nil];

This code works for me:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *string = [[NSString alloc] initWithFormat:#"Helooooooo"];
NSString *htmlString = [[NSString alloc] initWithFormat:#"<html><head><style type='text/css'>body { color:#FFFFFF; background-color: #000000; }</style></head><body>%#</body></html>", string];
[webView loadHTMLString:htmlString baseURL:nil];
}
Here's a screenshot:

Related

How do you log in to a website from an iOS Webview?

I'm working on an iOS app for my school that will allow the user to access school documents like schedules, calendars, and grade/tuition sites. I've been doing research on how to save credentials for some of the sites that require logins, and my ultimate goal is not only save credentials, but automatically log in the user to the websites when the webview is loaded as well. So far my code looks like this:
#import "PJP Webview.h"
#implementation PJP_Webview
#synthesize webView, loginView, username, password, body, urlToLink,usernameField,passwordField, firstTime, loginNeeded, callerCell;
-(void) viewDidLoad {
[super viewDidLoad];
loginView.hidden = true;
if (firstTime && loginNeeded) {
loginView.hidden = false;
[self.view bringSubviewToFront:loginView];
}
else {
loginView.hidden = true;
[self.view addSubview:self.webView];
[self.view bringSubviewToFront:webView];
if (loginNeeded) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
self.username = [userDefaults objectForKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
self.password = [userDefaults objectForKey:passwordSave];
NSURL *url = [NSURL URLWithString: urlToLink];
NSString *bodyForUse = [NSString stringWithFormat: body, username, password];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [bodyForUse dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
}
else {
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
}
}
- (IBAction)acceptInput:(id)sender {
self.username = usernameField.text;
self.password = passwordField.text;
loginView.hidden = true;
[self.view addSubview:self.webView];
[self.view bringSubviewToFront:webView];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
[userDefaults setObject:username forKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
[userDefaults setObject:password forKey:passwordSave];
[userDefaults synchronize];
NSURL *url = [NSURL URLWithString: urlToLink];
NSString *bodyForUse = [NSString stringWithFormat: body, username, password];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [bodyForUse dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
}
#end
I modeled some of my code after another stack overflow article found here Loading a webpage through UIWebView with POST parameters
An example of one of the websites I am trying to integrate is https://adphila.gradeconnect.com/front/slogin.php
So far my code has not been successful at logging me into the website and taking me to the next page, so I'm not sure if maybe the body I am using for the parameters is wrong, if my code is faulty, or some other issue is present. Also, if you have any suggestions for how to make my previous code more efficient I welcome that too!
Edit: The school also uses gmail for student emails, so is it possible to embed a uiwebview of email content as well? I know Google has their own APIs but I have never used non-Apple libraries before.
Edit 2:
#import "PJP Webview.h"
#implementation PJP_Webview
#synthesize webViewCurrent, loginView, username, password, bodyUsername, bodyPassword, urlToLink,usernameField,passwordField, firstTime, loginNeeded, callerCell, bodyButton;
-(void) viewDidLoad {
[super viewDidLoad];
[webViewCurrent setDelegate:self];
loginView.hidden = true;
if (firstTime && loginNeeded) {
loginView.hidden = false;
[self.view bringSubviewToFront:loginView];
}
else {
loginView.hidden = true;
if (loginNeeded) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
self.username = [userDefaults objectForKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
self.password = [userDefaults objectForKey:passwordSave];
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webViewCurrent loadRequest:request];
}
else {
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webViewCurrent loadRequest:request];
}
[self.view addSubview:self.webViewCurrent];
[self.view bringSubviewToFront:webViewCurrent];
}
}
- (IBAction)acceptInput:(id)sender {
self.username = usernameField.text;
self.password = passwordField.text;
loginView.hidden = true;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
[userDefaults setObject:username forKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
[userDefaults setObject:password forKey:passwordSave];
[userDefaults synchronize];
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webViewCurrent loadRequest:request];
[self.view addSubview:self.webViewCurrent];
[self.view bringSubviewToFront:webViewCurrent];
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
NSString *fillData = [NSString stringWithFormat:#"document.getElementById('%#').value = '%#';document.getElementById('%#').value = '%#';", bodyUsername, username, bodyPassword, password];
[webView stringByEvaluatingJavaScriptFromString:fillData];
NSString *buttonData = [NSString stringWithFormat:#"document.getElementById('%#').click()", bodyButton];
[webView stringByEvaluatingJavaScriptFromString:buttonData];
}
#end
There is a new error though, void SendDelegateMessage(NSInvocation *): delegate (webView:didFinishLoadForFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
For the website I am using, the bodyUsername is lusername, the bodyPassword lpassword, and the bodyButton login. The page loads but is not executing the javascript.
You can do this by Autopopulating the username and password field of that webpage. First get the name of the username and password field from the webpage. right click on the webpage.
Click on inspect element and then You van easily find it.
Then write the WebView Delegate Method.
// ViewController.m
// WebDemo
//
// Created by Nilesh on 7/31/16.
// Copyright © 2016 Nilesh. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()<UIWebViewDelegate>
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlToLink = #"https://adphila.gradeconnect.com/front/slogin.php";
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
NSString* userId = #"userName" ;//Your userName
NSString* password = #"password";// Your Password
NSString* jScriptString1 = [NSString stringWithFormat:#"document.forms[0]['lusername'].value='%#'", userId];
NSString* jScriptString2 = [NSString stringWithFormat:#"document.forms[0]['lpassword'].value='%#'", password];
[webView stringByEvaluatingJavaScriptFromString:jScriptString1];
[webView stringByEvaluatingJavaScriptFromString:jScriptString2];
NSString *jsStat = #"document.forms[0]['login'].click()";
[webView stringByEvaluatingJavaScriptFromString:jsStat];
}
#end

iOS, show specific html content from site on web view

I would like to retrieve the data from a specific part of the html of a site, and I wrote the following code. Unfortunately, the code is not working. How can I fix this?
the html class name is "topic_content", and every article in applicable site contains it (see the comments in code).
#implementation DetailViewController
....
UIActivityIndicatorView *activityIndicator;
- (void)viewDidLoad {
.....
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator stopAnimating];
....
[self callDetailNews];
}
-(void)callDetailNews{
....
if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]!=NotReachable)
{
// article link :: http://el3en.com/?articles=topic&topic=20840 (token_ID)
// <td class="topic_content" colspan='6' itemprop="articleBody" style="text-align:justify; word-wrap: break-word; width:100%">
// NSString *someHTML = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('box')[0].innerHTML;"];
NSString *articleString = [NSString stringWithFormat:#"http://el3en.com/?articles=topic&topic=%#",token_ID];
NSURL *articleURL = [NSURL URLWithString:articleString];
NSError *error;
NSString *articlePage = [NSString stringWithContentsOfURL:articleURL
encoding:NSASCIIStringEncoding
error:&error];
[_webView loadHTMLString:articlePage baseURL:nil];
//NSString *someHTML = [_webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('topic_content')[0].innerHTML;"];
//NSLog(#"Content : %#",someHTML);
}
.....
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
self.webView.hidden=true;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('topic_content')[0].sinnerHTML;"];
[activityIndicator stopAnimating];
self.webView.hidden=false;
}
Thanks
You should use innerHtml instead sinnerHtml, besides this you need to get the string and load this string into another webview. Pls see example
#import "ViewController.h"
#interface ViewController ()
#property (strong, nonatomic) IBOutlet UIWebView *fakeView;
#end
#implementation ViewController
UIActivityIndicatorView *activityIndicator;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicator stopAnimating];
[self callDetailNews];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)callDetailNews{
if ([[Reachability reachabilityForInternetConnection]currentReachabilityStatus]!=NotReachable)
{
// article link :: http://el3en.com/?articles=topic&topic=20840 (token_ID)
// <td class="topic_content" colspan='6' itemprop="articleBody" style="text-align:justify; word-wrap: break-word; width:100%">
// NSString *someHTML = [webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('box')[0].innerHTML;"];
NSString *articleString = [NSString stringWithFormat:#"http://el3en.com/?articles=topic&topic=%#",#"20840"];
NSURL *articleURL = [NSURL URLWithString:articleString];
NSError *error;
NSString *articlePage = [NSString stringWithContentsOfURL:articleURL
encoding:NSASCIIStringEncoding
error:&error];
NSLog(#"Request %#",articleURL);
self.fakeView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
self.fakeView.delegate = self;
[self.fakeView loadRequest:[NSURLRequest requestWithURL:articleURL]];
//NSString *someHTML = [_webView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('topic_content')[0].innerHTML;"];
//NSLog(#"Content : %#",someHTML);
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[activityIndicator startAnimating];
self.webView.hidden=true;
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(#"error %#" ,[error description]);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if(webView == self.fakeView) {
NSString* javascriptString = [self.fakeView stringByEvaluatingJavaScriptFromString:#"document.getElementsByClassName('topic_content')[0].innerHTML"];
NSLog(#"%#",javascriptString);
[self.webView loadHTMLString:javascriptString baseURL:[NSURL URLWithString:#""]];
self.fakeView = nil;
}else{
[activityIndicator stopAnimating];
self.webView.hidden=false;
}
}
#end

viewDidLoad not being called with UIViewController is pushed

I'm trying to launch a UIViewController when a table line is clicked...
The code is main view controller is ...
- (void)showDetail:(NSString *)detail
{
DetailViewController *secondViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:secondViewController animated:YES];
[DetailViewController release];
}
I filled in the viewDidLoad method in my "DetailViewController" class that is attached to the NIB.
- (void)viewDidLoad:(BOOL)animated;
{
NSURL *url = [NSURL URLWithString:#"www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
When I hit the pushViewController method, the NIB appears to load (can see a test label on it) but the viewDidLoad method is not called. In that method, I'm trying to load a test HTML into a UIWebView.
Any ideas?
There is no event viewDidLoad: change to the bellow it should work.
- (void)viewDidLoad{
NSURL *url = [NSURL URLWithString:#"www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}

Three20 display local html file

I'm trying to display a local html file from a three20 table controller. The table shows up correctly, but when I select the item, all I get is a blank screen. If I use an external URL, the page shows correctly. I haven't tried using UIWebView, but I was wondering if there was an easy way using the Three20 framework.
Thanks.
EDIT: I forgot that has changed recently...
Assuming you have registered a TTWebController in your navigator that way :
TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
[map from:#"*" toViewController:[TTWebController class]];
You can simply open a local html file that way :
NSString *creditsPath = [[NSBundle mainBundle] pathForResource:#"credits" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
TTOpenURL([url description]);
Then all you have to to is to add TTTableLinkedItem or subclass (almost all of TT*Cell are) to your dataSource with a local file URL like that :
NSString *creditsPath = [[NSBundle mainBundle] pathForResource:#"credits" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
TTTableTextItem *item = [TTTableTextItem itemWithText:#"foobar" URL:[url description]];
Much simpler that what I wrote before...
ENDOFEDIT
Forget whats under, unless you're interested in others solutions... (more complex... or not, see last one)
Basically here is how you load content in a UIWebView :
NSString *creditsPath = [[NSBundle mainBundle] pathForResource:#"credits" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
The three20 TTWebController can take a request in a query :
- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
if (self = [self initWithNibName:nil bundle:nil]) {
NSURLRequest* request = [query objectForKey:#"request"];
if (nil != request) {
[self openRequest:request];
} else {
[self openURL:URL];
}
}
return self;
}
So to open a local html file in a TTWebController you could do this :
NSString *creditsPath = [[NSBundle mainBundle] pathForResource:#"credits" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[[TTNavigator navigator] openURLAction:
[[[TTURLAction actionWithURLPath:#"url://to/your/ttwebcontroller"]
applyQuery:[NSDictionary dictionaryWithObject:ulrRequest
forKey:#"request"]]
applyAnimated:YES]];
Now the last part, to trigger this from a TTTableViewController...
You have in your ViewController to implement :
- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
// there you retrieve a *TTTableItem* corresponding to your row
// and call previous snippet from URL...
// TTTableItem class has userInfo property you could use:
TTTableItem *item = (TTTableItem *)object;
NSString *htmlFile = item.userInfo.
[self openLocalHtmlFile:htmlFile];
}
Hope that helps!
Nothing tested, at all, should not compile or whatsoever, but that is a solution.
That's a solution using basic three20 + iOS features. You could also write a TTCustomWebController inheriting from TTWebController that would take urls like #"myapp://openLocalHtml?file=credits" that's not that hard to do...
...That here is a draft :
#interface TTCustomWebController : TTWebController {}
#end
#implementation TTCustomWebController
- (id)initWithFile:(NSString *)name {
self = [super init];
if (self) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:name ofType:#"html"];
NSURL *URL = [NSURL fileURLWithPath:filePath];
[self openURL:URL];
}
return self;
}
#end
Then you could simply use TTTableLinkedItem in your datasource pointing to these #"myapp://openLocalHtml?file=credits"...
Good luck! :)

Using images and css file for a generated HTML UIWebView

I'm generating an HTML file for an app, and in this HTML file there's a link to a stylesheet and one to an image.
Here's what I tried so far:
NSMutableString *toReturn = [NSMutableString stringWithCapacity:100];
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"etapes.css" ofType:nil];
[toReturn appendFormat:#"<html><head><title></title><link href=\"%#\" media=\"all\" rel=\"stylesheet\" type=\"text/css\" /></head><body>", cssPath];
It generates the right full path to the right file, this is okay if I want to access it on my mac, but on the simulator or my iPhone it doesn't point to the right place at all...
Do you have any idea of how could I make this?
Thanks
I found this tutorial a while back. I ended up just using a <style> tag in the end though.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
Another way of doing this is like so:
-(NSString *)urlForFileNamed:(NSString *) name ofType:(NSString *) type {
NSString *filePath = [[NSBundle mainBundle] pathForResource: name ofType: type];
if (!filePath) { NSLog(#"No path found for file %#.%#", name, type); }
return filePath;
}
-(void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] bundlePath];
baseUrl = [NSURL fileURLWithPath: path];
[view loadHTMLString: [self html] baseURL: [NSURL fileURLWithPath: path]];
}
-(NSString *)html {
// Obviously the below's just a stub for proper HTML
return [NSString stringWithFormat: #"<img src=\"#\" />", [self urlForFileNamed: #"foo" ofType: #"png"]];
}
You may try to edit the line
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"etapes.css" ofType:nil];
to
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"etapes" ofType:#"css"];