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:
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];
In my app I've a view controller in which there are a table view, when I tap on one of table view row it should dismiss the actual view controller an pass back an url to load it in a web view. I thought the right way to do that it's to implement a delegate, so I did it but when I try to run the app when I tap on the row of table view it doesn't call the delegate method.
I add here some code to understand what's wrong:
HistoryNotificationViewController.h
#import <UIKit/UIKit.h>
#protocol HistoryNotificationDelegate;
#interface HistoryNotificationViewController : UIViewController
#property(nonatomic,weak) id <HistoryNotificationDelegate> delegate;
#end
#protocol HistoryNotificationDelegate <NSObject>
- (void) updateWebViewWithURL:(NSString*)url;
#end
HistoryNotificationViewController.m (I post only the method in which I call the delegate method)
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.delegate updateWebViewWithURL:[sortedArray[indexPath.row] objectForKey:#"url"]];
[self dismissViewControllerAnimated:YES completion:nil];
}
HomeViewController.m (view controller that should implement the delegate method of HistoryNotificationViewController.m)
#import "HomeViewController.h"
#import "SSKeychain.h"
#import "SSKeychainQuery.h"
#import "MBProgressHUD.h"
#import "HistoryNotificationViewController.h"
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
#interface HomeViewController () <UIWebViewDelegate, HistoryNotificationDelegate>
#property (weak, nonatomic) IBOutlet UIWebView *webView;
- (IBAction)buttonForward:(UIButton *)sender;
- (IBAction)buttonBack:(UIButton *)sender;
#property(nonatomic,strong)MBProgressHUD *hud;
#end
#implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.hud = [[MBProgressHUD alloc]init];
self.hud.labelText = #"Loading";
self.hud.mode = MBProgressHUDModeIndeterminate;
[self.view addSubview:self.hud];
NSURL *url = [[NSURL alloc]init];
NSURLRequest *request = [[NSURLRequest alloc]init];
if (!self.website) {
url = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:#"website" ]];
request = [NSURLRequest requestWithURL:url];
} else {
[[NSUserDefaults standardUserDefaults] setObject:self.website forKey:#"website"];
[[NSUserDefaults standardUserDefaults] synchronize];
url = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:#"website"]];
request = [NSURLRequest requestWithURL:url];
}
[self.webView loadRequest:request];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
HistoryNotificationViewController *historyNotificationViewController = [[HistoryNotificationViewController alloc]init];
[historyNotificationViewController setDelegate:self];
}
#pragma mark HistoryNotificationDelegate
- (void)updateWebViewWithURL:(NSString *)url {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[self.webView loadRequest:request];
}
#pragma mark UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self.hud show:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.hud hide:YES];
}
- (IBAction)buttonForward:(UIButton *)sender {
[self.webView goForward];
}
- (IBAction)buttonBack:(UIButton *)sender {
[self.webView goBack];
}
#end
What's wrong in my classes? Why it doesn't execute the delegate method?
Try this,
Make a property for HistoryNotificationViewController by
#property (nonatomic, strong) HistoryNotificationViewController *historyNotificationViewController;
and in prepareForSegue :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
UIViewController *destinationVC = [segue destinationViewController];
if ([destinationVC isKindOfClass:[HistoryNotificationViewController class]]) {
self.historyNotificationViewController = destinationVC;
[self.historyNotificationViewController setDelegate:self];
}
}
You need to get HistoryNotificationViewControllers in prepareForSegue from destinationViewController instead of allocating a new object...
#import "HomeViewController.h"
#import "SSKeychain.h"
#import "SSKeychainQuery.h"
#import "MBProgressHUD.h"
#import "HistoryNotificationViewController.h"
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
#interface HomeViewController () <UIWebViewDelegate, HistoryNotificationDelegate>{
}
#property (weak, nonatomic) IBOutlet UIWebView *webView;
- (IBAction)buttonForward:(UIButton *)sender;
- (IBAction)buttonBack:(UIButton *)sender;
#property(nonatomic,strong)MBProgressHUD *hud;
#end
#implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.hud = [[MBProgressHUD alloc]init];
self.hud.labelText = #"Loading";
self.hud.mode = MBProgressHUDModeIndeterminate;
[self.view addSubview:self.hud];
NSURL *url = [[NSURL alloc]init];
NSURLRequest *request = [[NSURLRequest alloc]init];
if (!self.website) {
url = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:#"website" ]];
request = [NSURLRequest requestWithURL:url];
} else {
[[NSUserDefaults standardUserDefaults] setObject:self.website forKey:#"website"];
[[NSUserDefaults standardUserDefaults] synchronize];
url = [NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:#"website"]];
request = [NSURLRequest requestWithURL:url];
}
[self.webView loadRequest:request];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"YOUR_SEGUE_NAME_HERE_FOR_HISTORY_NOTIFICATION_VIEWCONTROLLER"])
{
// Get reference to the destination view controller
HistoryNotificationViewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setDelegate:self];
}
}
#pragma mark HistoryNotificationDelegate
- (void)updateWebViewWithURL:(NSString *)url {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[self.webView loadRequest:request];
}
#pragma mark UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self.hud show:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self.hud hide:YES];
}
- (IBAction)buttonForward:(UIButton *)sender {
[self.webView goForward];
}
- (IBAction)buttonBack:(UIButton *)sender {
[self.webView goBack];
}
You do not need and should not use delegate at all in this case. Otherwise you're making thing complicated.
Just define a public method in your HomeViewController,
- (void)updateWebViewWithURL:(NSString *)url
{
}
Same method name as the one you've defined should be ok, but not as a delegate method!
Then, in your HistoryNotificationViewController.m, you'll be able to get a reference of HomeViewController easily. Something like this will do,
NSArray *viewControllers = self.navigationController.viewControllers;
HomeViewController *homeViewController = [viewControllers objectAtIndex:0];
[homeViewController updateWebViewWithURL:url];
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];
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 .. :)
I want to open links of uiwebview in to the safari browser my code is working perfectly if I implement shouldStartLoadWithRequest method in viewController but when I implement shouldStartLoadWithRequest in same class and set UIWebView's delegate to self it doesn't work it get halt in between and shows assembly level code with error EXC_BAD_ACCESS(code=2, address=0x9) my files are as follows
//content of ShowView.h file
#import <UIKit/UIKit.h>
#interface ShowView : UIView <UIWebViewDelegate> {
}
- (void) showViewFunction;
#property (nonatomic, assign) UIViewController *mainViewContObj;
#end
//content of ShowView.m file is :
#import "ShowView.h"
#implementation ShowView
- (void) showViewFunction {
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 150)];
aWebView.autoresizesSubviews = YES;
aWebView.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
[aWebView setDelegate:self];
NSString *urlAddress = #"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
aWebView.delegate = self;
[aWebView loadRequest:requestObj];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[[[self mainViewContObj] view] addSubview:aWebView];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"In shouldStartLoadWithRequest method");
if ([[[request URL] absoluteString] isEqual:#"http://localhost/test/index.php"])
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
#end
// Content of ViewController.h
#import "ViewController.h"
#import "ShowView.h"
#interface mnetViewController ()
#end
#implementation mnetViewController
- (void)viewDidLoad {
[super viewDidLoad];
MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
bannerObj.mainViewContObj = self;
[bannerObj showAd];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Some times html page shows but when I click on link it opens in same UIWebview window, and not even going into the shouldStartLoadWithRequest method, am I doing anything wrong?
Your code isn't clear, might need more info's, but from what i see, the ShowView class is never instanciated, so it shouldn't even show.
you should make something like this i guess :
//mnetViewController.m
#import "mnetViewController.h"
#import "ShowView.h"
#interface mnetViewController ()
#end
#implementation mnetViewController
- (void)viewDidLoad {
[super viewDidLoad];
ShowView* theShowView = [[ShowView alloc] initWithFrame:CGRectMake(insert the frame you want your webview to have)];
theShowView.autoresizesSubviews = YES;
[self.view addSubview:theShowView];
[theShowView release];
MNETMobAd *bannerObj = [[MNETMobAd alloc] init];
bannerObj.mainViewContObj = self;
[bannerObj showAd];
}
now for the ShowView class, try something like this :
//ShowView.h
#import <UIKit/UIKit.h>
#interface ShowView : UIView <UIWebViewDelegate> {
}
#end
//ShowView.m
#import "ShowView.h"
#implementation ShowView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
aWebView.scalesPageToFit = YES;
[aWebView setDelegate:self];
[self addSubview:aWebView];
NSString *urlAddress = #"http://localhost/test/index.php";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[aWebView loadRequest:requestObj];
[aWebView release];
}
return self;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"In shouldStartLoadWithRequest method for URL : %#",[request [URL absolutString]]);
if ([[[request URL] absoluteString] isEqual:#"http://localhost/test/index.php"])
return YES;
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
This should work, i didn't try it, i'll comeback tomorrow to try it if necessary.