I want to initialize a UIWebview object inside of my WebViewController. I'm having a bit of trouble initializing it, and directing it to the URL that is sent to it from the previous ViewController.
In the viewDidLoad of my WebViewController, I attempt to initialize it with the following:
UIWebView *tempWebview = [[UIWebView alloc]initWithFrame:theFrame];
NSURL *url = [NSURL URLWithString:_itemURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
self.myWebView = tempWebview;
[tempWebview loadRequest:urlRequest];
_myWebView.delegate=self;
[WebViewController loadRequest:urlRequest];
However, this gives me errors stating:
use of undeclared identifier'theFrame', and no known class method for selector loadRequest.
Full code is below:
WebViewController.h:
#import <UIKit/UIKit.h>
#import "MatchCenterViewController.h"
#interface WebViewController : UIViewController
#property (strong, nonatomic) NSURL *itemURL;
#property (weak, nonatomic) IBOutlet UIWebView *myWebView;
#end
WebViewController.m:
#import "WebViewController.h"
#interface WebViewController ()
#end
#implementation WebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *tempWebview = [[UIWebView alloc]initWithFrame:theFrame];
NSURL *url = [NSURL URLWithString:_itemURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
self.myWebView = tempWebview;
[tempWebview loadRequest:urlRequest];
_myWebView.delegate=self;
[WebViewController loadRequest:urlRequest];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
First you need to define that you are going to use the UIWebView Delegate in your .h file as:
#interface WebViewController : UIViewController <UIWebViewDelegate>
When you already have a webView defined in your Xib file, why are you creating another UIWebView and assigning it to the IBOutlet's webView? You could simply do:
self.myWebView.delegate = self; //Note that I have set the delegate first before calling LoadRequest
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView setScalesPageToFit:YES];
[self.myWebView loadRequest:request];
Related
I watched this video on YouTube :
Xcode How to Create a Webview for iOS Applications
I followed video but if I run my app on simulator, only I see white screen.
This ViewController.h
//
// ViewController.h
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIWebView *myWebView;
}
#end
//
// ViewController.m
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *myURL = [NSURL URLWithString:#"http://website.com"]; NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL]; myWebView.scalesPageToFit = YES; [myWebView loadRequest:myRequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Can you get me fix this error?
tHis ViewController.m
//
// ViewController.m
//
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *myURL = [NSURL URLWithString:#"http://website.com"]; NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL]; myWebView.scalesPageToFit = YES; [myWebView loadRequest:myRequest];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You have to actually load the request after you create it!
NSURL *url = [NSURL URLWithString:#"http://yoururl.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
I'm building a basic app for my school and can't figure out how to change the URL to be presented based on the button pressed. I have four buttons and currently have them all linked to temporary URLs, but when I run my app, the webview appears blank and my NSLog returns null for the URL. What am I doing wrong? Here is my code:
TWViewController.h:
#import <UIKit/UIKit.h>
#interface TWViewController : UIViewController
- (IBAction)gradesPressed:(id)sender;
- (IBAction)facPressed:(id)sender;
- (IBAction)newsPressed:(id)sender;
- (IBAction)sportsPressed:(id)sender;
#property (strong, nonatomic) NSURL *url;
#end
TWViewController.m:
#import "TWViewController.h"
#import "TWWebViewController.h"
#interface TWViewController ()
#end
#implementation TWViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)gradesPressed:(id)sender{
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://google.com"];
}
- (IBAction)facPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://yahoo.com"];
}
- (IBAction)newsPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://facebook.com"];
}
- (IBAction)sportsPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:#"http://apple.com"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//
// TWWebViewController *wbc = (TWWebViewController *)segue.destinationViewController;
//}
#end
TWWebViewController.h:
#import <UIKit/UIKit.h>
#interface TWWebViewController : UIViewController
{
UIWebView *webView;
NSString *website;
}
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (strong, nonatomic) NSURL *website;
#end
TWWebViewController.m:
#import "TWWebViewController.h"
#import "TWWebViewController.h"
#interface TWWebViewController ()
#end
#implementation TWWebViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.website];
[self.webView loadRequest:urlRequest];
NSLog(#"%#", website);
// Do any additional setup after loading the view.
}
I'm fairly new to iOS Development, so I'm not quite sure if I'm even on the right track. Any help would be appreciated, thanks!
You need to push your controller after initializing it: Also, your NSString is not an NSURL object so do this also in your TWWebViewController: NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.website]];
TWWebViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:
[NSURL URLWithString:self.website]];
[self.webView loadRequest:urlRequest];
}
TWViewController.m:
- (IBAction)sportsPressed:(id)sender
{
TWWebViewController *webView = [self.storyboard
instantiateViewControllerWithIdentifier:#"WebView"];
webView.website = #"http://apple.com";
[self.navigationController pushViewController:webView animated:YES];
}
Also don't forget to do this in your storyboard file:
I'm getting one error, Ive made a UIPageController for the web but I cant seem to find the problem with it, there is only one error, please help. code bellow - more code on demand.
#interface ContentViewController ()
#end
#implementation ContentViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_webView loadHTMLString: _dataObject baseURL:[NSURL URLWithString:#"http://"]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
[_webView loadHTMLString: _dataObject baseURL:[NSURL URLWithString:#"http://"]];
My "Error is above"
Please help
#import <UIKit/UIKit.h>
#interface ContentViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIView *webView;
#property (strong, nonatomic) id dataObject;
#end
Change the .h file to this:
#import <UIKit/UIKit.h>
#interface ContentViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *webView;
#property (strong, nonatomic) id dataObject;
#end
Beside that if you want to load a webpage in UIWebView from a url then you can do like this:
self.webView.delegate = self;
self.webView.scalesPageToFit =YES; // This will fit the page within the screen
NSURL *Url = [NSURL URLWithString:#"http://facebook.com/xxx/xxx"];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:Url];
[self.webView loadRequest:req];
If you want to load a HTML file from document then do:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"Test" ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];
Or if you just want to load a string then do:
NSString* htmlString = #"";
[webView loadHTMLString:htmlString baseURL:nil];
Hope this helps .. :)
Hey guys, i'm trying to make the buttons on my project to open a different webview url. I'm new to iOS programming, but i've used andriod programming. Is this possible? I've a ready created another webview view that sits in the supporting files folder.
Here is my code below
Viewcontroller.m
#import "ViewController.h"
#implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
-(void)viewDidLoad
{
NSString *urlString = #"http://www.athletic-profile.com/Application";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Requst Object
NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:webRequest];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#synthesize webView;
#end
Add a button to your View then give a connection to it. In the action method just write your code
-(IBAction*)yourButtonActionMethod:(id)sender
{
[UIWebView *aWebView =[[UIWebView alloc] initWithFrame:CGRectMake(x,y,width,height)];
aWebView.delegate=nil;
NSString *urlString = #"http://www.athletic-profile.com/Application";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Requst Object
NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[aWebView loadRequest:webRequest];
[self.view addSubView:aWebView];
}
Firstly you can drag & drop UIButton in First Class & create action of that Button then create a new class for UIWebView in which you want to open you url named your choice which is subclass of UIViewController then into your .Xib or Storyboard you just drag & drop the UIWebView then create outlet of UIWebView after you write this code in first class of that button's action :-
- (IBAction *) firstButton:(id) sender
{
NSString *urlString = #"http://www.athletic-profile.com/Application";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlString];
//URL Request Object
NSURLRequest *webRequest = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:webRequest];
}
I am building a tab bar application with a different pdf in each tab. I have two sets of view controllers. Each have this relative code
#import "SecondViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:#"Winter_Newsletter"
ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webview setScalesPageToFit:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I keep receiving an error "undeclared identifier 'web view'" What am I doing Wrong?
You are showing in the code two versions of webView(capital V) and webview(small v)
[webView loadRequest:request];
[webview setScalesPageToFit:YES];
And if you are using auto-synthesize then it sould be
self.webView or _webView
And also, don't forget to make a property of the web view and control drag to hook it up!