UIWebview does not show up into UINavigationController - iphone

I have a tabBar application. In the first tab, I have a navigation bar. In this navigation bar I have a table view. When the user clicks a cell of the table, it goes to another table view, and when the user clicks a cell in that table, it goes to a webview to open a web page.
Basically, it goes fine until opening the webview. In the webview, viewDidLoad is called and seems to work properly. However, webViewDidStartLoad is never called and the web page never shows up.
I am not using IB. I build the UITabBarController in the AppDelegate, where I also assign an instance of my UINavigationController for each tab.
I call the webview from the second UITableViewController as follows:
rssWebViewController = [[webViews objectAtIndex: indexPath.row] objectForKey:#"controller"];
[[self navigationController] pushViewController:rssWebViewController animated:YES];
I have checked that the navigationController is there and it seems just fine.
The viewDidload of my webview is as follows:
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlAddress = self.storyUrl;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[[self rssWebView] setDelegate: self];
[[self view] addSubview:[self rssWebView]];
[rssWebView loadRequest:requestObj];
self.rssWebView.scalesPageToFit = YES;
self.rssWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
}
The web view controller interface is defined as follows:
#interface RSSWebViewController : UIViewController <UIWebViewDelegate>{
IBOutlet UIWebView *rssWebView;
IBOutlet NSString *storyUrl;
IBOutlet NSString *feedName;
}
#property (nonatomic, retain) IBOutlet UIWebView *rssWebView;
#property (nonatomic, retain) IBOutlet NSString *storyUrl;
#property (nonatomic, retain) IBOutlet NSString *feedName;
#end
Any help is greatly appreciated.

ok, I found out, I had to initiate my webview with a frame in viewDidLoad:
rssWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];

Related

Load Webview in different view in iOS Programming

I have been programming an Web browser app for iOS and stumbled upon an issue. At first my search text field and web view where in the same view (Everything was fine :). When I put the web view in an different view the web view wouldn't load up the page and stay blank. So the issue is the web view will not load in a different view (will work if text field and web view are in the same view).
My Code:
#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>
#interface ViewController ()
#end
#implementation ViewController
#synthesize searchButton;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
}
-(IBAction)SearchButton:(id)sender {
NSString *query = [searchField.text stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.com/search?q=%#", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
ViewController *WebView = [self.storyboard instantiateViewControllerWithIdentifier:#"WebView"];
[self presentViewController:WebView animated:YES completion:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
The SearchButton method refers to a webView in the current class, but it's the new ViewController (you call it WebView) which is about to be presented and that should contain a UIWebView. So the UIWebView on the presented view controller is never getting to loadRequest.
Create a subclass of UIViewController to contain your web view. (call it something like MyWebViewController) It should have a property called urlString. Make sure to change the class of the view controller you currently have painted in storyboard to MyWebViewController.
The SearchButton method should look like this:
// renamed to follow convention
- (IBAction)pressedSearchButton:(id)sender {
NSString *query = [searchField.text stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSString *urlString = [NSString stringWithFormat:#"http://www.google.com/search?q=%#", query];
// remember to change the view controller class in storyboard
MyWebViewController *webViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"WebView"];
// urlString is a public property on MyWebViewController
webViewController.urlString = urlString;
[self presentViewController:webViewController animated:YES completion:nil];
}
The new class can form the request from the urlString...
// MyWebViewController.h
#property (nonatomic, strong) NSString *urlString;
#property (nonatomic, weak) IBOutlet UIWebView *webView; // be sure to attach in storyboard
// MyWebViewController.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSURL *url = [NSURL URLWithString:self.urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}

Calling method from another class

To begin, I would like to apologize for my English :)
I have FirstViewController, which contains scrollView. This is scrolView with enabled paging, and have 2 pages with 2 different view controllers. From one of the view controllers by touching the button the third view controller appears like a modal view. I call a method in FirstViewController that must disable scrolling and hide two labels which is not contained in the scrollView.
Method is executing, but UI not changes, scrolling still enabled and labels still visible.
Now a bit of code:
This is a piece of the FirstViewController.h (not the whole code):
#interface FirstViewController : UIViewController <UIScrollViewDelegate> {
IBOutlet UIScrollView *scrollView;
IBOutlet UILabel *label1;
IBOutlet UILabel *label2;
}
#property (nonatomic, retain) UILabel *label1;
#property (nonatomic, retain) UILabel *label2;
#property (nonatomic, retain) UIScrollView *scrollView;
-(void)prepareToModal;
#end
Now it's -(void)prepareToModal; implementation:
-(void)prepareToModal {
[label1 setHidden:YES];
[label2 setHidden:YES];
scrollView.scrollEnabled = NO;
}
So, from the one of the view controllers, which contained in scrollView, I call prepareToModal
Previously:
#import "FirstViewController.h"
Next:
FirstViewController *vc = [[FirstViewController alloc] init];
[vc prepareToModal];
[vc release];
So, that's all. I put a breakpoint in prepareToModal, and it stopped executing. The method is called, but nothing changes on screen...
What am I doing wrong?
How to do this correctly?
Update:
I solved this problem.
When I present this modal view, I wrote this:
ThirdViewController *tvc = [[ThirdViewControler alloc] init];
tvc.delegate = self;
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:tvc];
[self presentModalViewController:nc animated:YES];
[tvc release];
[nc release];
Now, insted of [self presentModalViewController:nc animated:YES]; I write this:
[[[[UIApplication sharedApplication].windows objectAtIndex:0] rootViewController] presentModalViewController:nc animated:YES];
And It's working very well, I don't need a method -(void)prepareToModal;
Thanks a lot :)
Make sure you have hooked up your IBOutlets in Interface Builder.

Open Link in UIWebView in Safari

I am working on an app that receives a feed that I display in a UIWebView. The feed has embedded links that I want to open in Safari instead of thew WebView. I have looked over several other questions that are posted here. I am not sure what I am missing, but I think it is something simple. Here are my .h and .m files
#import
#class BlogRss;
#interface EnduranceDailyWorkoutViewController : UIViewController {
IBOutlet UIWebView * descriptionTextView;
BlogRss * currentlySelectedBlogItem;
}
#property (nonatomic, retain) UIWebView * descriptionTextView;
#property (readwrite, retain) BlogRss * currentlySelectedBlogItem;
#end
#import "EnduranceDailyWorkoutViewController.h"
#import "BlogRss.h"
#implementation EnduranceDailyWorkoutViewController
#synthesize descriptionTextView = descriptionTextView;
#synthesize currentlySelectedBlogItem = currentlySelectedBlogItem;
- (void)viewDidLoad {
[super viewDidLoad];
NSString *html = [NSString stringWithFormat:#"%# %#",currentlySelectedBlogItem.title, currentlySelectedBlogItem.contentEncoded];
[descriptionTextView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];
}
-(BOOL)webView:(UIWebView *)descriptionTextView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
if (UIWebViewNavigationTypeLinkClicked == navigationType) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
}
return YES;
}
Using Interface Builder I have linked the IBOutlet and the UIWebView. Please let me know what I am missing. I have put break points in the webView section but the code never hits there so it is almost like something is not linked correctly in IB.
You need to ensure that the UIWebView's delegate is set to your controller. You can do this in interface builder, or you can modify your viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
// add this line to set the delegate of the UIWebView
descriptionTextView.delegate = self;
NSString *html = [NSString stringWithFormat:#"%# %#",currentlySelectedBlogItem.title, currentlySelectedBlogItem.contentEncoded];
[descriptionTextView loadHTMLString:html baseURL:[NSURL URLWithString:nil]];
}

iOS using External Object in Nib

I am writing an application that needs to interfaces with different backend systems. I decided to use a Protocol in order to abstract my backend Classes. I created a nib called LoginViewController that contains an "External Object" reference of type "NSObject", and wired it to the systemDelegate outlet in my LoginViewController.
#interface LoginViewController : UIViewController {
}
#property (nonatomic, retain) IBOutlet UITextField *usernameTextView;
#property (nonatomic, retain) IBOutlet UIImageView *captchaImageView;
#property (nonatomic, retain) IBOutlet UITextField *captchaTextView;
#property (nonatomic, retain) IBOutlet NSObject <BackEndSystemDelegate> *systemDelegate;
- (IBAction) submitCaptcha:(id) sender;
- (IBAction)dismissKeyboard: (id)sender;
- (IBAction) animateViewUp: (id) sender;
- (IBAction) animateViewDown: (id) sender;
- (void) animateViewOnYAxis: (int) offset;
- (void) loadCaptchaImage;
#end
I instanciate the LoginViewController in my application delegate, then try to load the nib with the external object reference. My code calls the loadNibNamed and crashes without a stack trace. I do not reach the NSLog statements after the invocation:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSObject <BackEndSystemDelegate> *systemDelegate = [[ACMEBackEndSystemDelegate alloc] init];
// Init LoginView, and load nib with systemDelegate
self.viewController = [[LoginViewController alloc] init];
NSDictionary *proxies = [NSDictionary dictionaryWithObject:systemDelegate forKey:#"systemDelegate"];
NSDictionary *options = [NSDictionary dictionaryWithObject:proxies forKey:UINibExternalObjects];
NSArray *toplevelobjects = [[NSBundle mainBundle] loadNibNamed:#"LoginViewController"
owner:self.viewController
options:options];
if (toplevelobjects) {
NSLog(#"toplevelobjects is nil");
} else {
NSLog(#"toplevelobjects count %d", [toplevelobjects count]);
}
NSLog(#"Controller: %#, View: %#", viewController, viewController.view);
// Add the view controller's view to the window and display.
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
I am at a loss trying to figure this out. Any help would be appreciated.
Thanks,
J Garcia
A few comments:
Generally you refer to a delegate object using the pattern id<SomeProtocolName> and not NSObject<SomeProtocolName>
You alloc your systemDelegate variable but never release it. This is a memory leak.
If the ACMEBackEndSystemDelegate class implements the BackEndSystemDelegate protocol, it is sufficient to allocate it like ACMEBackEndSystemDelegate* systemDelegate = [[ACMEBackEndSystemDelegate alloc] init];
Now, as for your crash, you said there's a crash on this line:
NSArray *toplevelobjects = [[NSBundle mainBundle] loadNibNamed:#"LoginViewController"
owner:self.viewController
options:options];
I assume you have a .xib called LoginViewController.xib. Open it up. That is the class type set for "File's Owner"? Is it LoginViewController? If not, set it. Now check the view outlet property. Is it set to a UIView in the .xib (perhaps a top-level UIView object)? If not, set it.

how to disable horizontal scroll for a uiwebview?

I created a sample in which i imported a word(.docx)on to the uiwebview and displayed.I got the output but i don't want to display the horizontal scroll bar and the vertical scroll also working not properly i can see the black space behind the webview when i scroll it vertically.
Here is my code:
#import <UIKit/UIKit.h>
#interface userguideLinesNewViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic,retain) UIWebView *webView;
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webViews;
#end
#import "userguideLinesNewViewController.h"
#implementation userguideLinesNewViewController
#synthesize webView;
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webViews
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"BTBP CLARITY SKIN ADVISORuser.docx" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webViews loadRequest:request];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self loadDocument:#"BTBP CLARITY SKIN ADVISORuser.docx" inView:self.webView];
}
You could check others topics about this : Stop UIWebView from "bouncing" vertically? :-)
There isn't a method like in UIScrollView. But there is some kind of way to achieve your goal ;-)
Here is my way.
- (void)disableBounce {
for (id subview in self.subviews)
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).bounces = NO;
}
Enjoy...