UIWebView Loads in simulator but not in device - ios

My UIWebView is working perfectly in simulator, but not on all my test devices, the UIWebView remains blank. Any suggestions to edit this code?
- (void)awakeFromNib
{
[worship loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.website.com/current.pdf"] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60.0]];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:#selector(tick) userInfo:nil repeats:YES];
}

Try moving your code into:
- (void)viewDidLoad {
//Here
}
Your webview is probably = nil in
-(void)awakeFromNib {
}
Edit: You can also add NSLog(#"%#", self.webview); into both awakeFromNib, and viewDidLoad if you want to see if your web view is nil or not.

During -awakeFromNib, your UIWebview will not be initialized yet. Move your UIWebView code to -viewDidLoad (if you only want it to (re)load once) or -viewDidAppear: (if you want it to reload every time the view becomes active.

Related

How to appear/ disappear UITableview in ios

I am working video player.. I am using MPMediaplayer framework…
In video player below i am displaying one UIView, in this view i am displaying current video details like Video name and video description and video starting time and ending time… Those are details are displayed in UITableview.
The tableview adding to this uiview..Those are all things are done.
The uiview beside i put one button.. if i click the button then automatically appear uiview. if i click again the uiview disappear..this is also done.
now i am integrating animation for uiview like fade out(means if i click button appear uiview, after 10 seconds automatically disappear the uiview… )..that is my main requirement.. This is also done..
But problem is for example user scroll the uiview (video player details in tableview to scroll the tableview up and down) after 10 seconds automatically disappear. if user scroll the video player details i don’t want to disappear my tableview… but my problem if user scroll the video player details it is automatically disappear after 10 seconds..
so plz help me any body..How to handle this thing…My requirement is if user scroll the video player details i don’t want to disappear my tableview…
- (IBAction)InfoVisibleAction:(id)sender
{
if(UIView.hidden==NO)
{
UIView.hidden=YES;
ScheduleImageView.hidden=YES;
tableView.hidden=YES;
timeLabel1.hidden=YES;
}
else
{
UIView.hidden=NO;
ScheduleImageView.hidden=NO;
tableView.hidden=NO;
timeLabel1.hidden=NO;
[self performSelector:#selector(infoHiddenAction:) withObject:nil afterDelay:10];
}
}
- (IBAction)infoHiddenAction:(id)sender
{
if(UIView.hidden==NO)
{
UIView.hidden=YES;
ScheduleImageView.hidden=YES;
tableView.hidden=YES;
timeLabel1.hidden=YES;
}
}
- (IBAction)infoHiddenAction:(id)sender
{
if(UIView.hidden==NO)
{
SupportedView.hidden=YES;
ScheduleImageView.hidden=YES;
tableView.hidden=YES;
timeLabel1.hidden=YES;
}
}
-(void)viewDidLoad
{
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 550, 130)];
tableView.delegate=self;
tableView.dataSource=self;
tableView.backgroundColor=[UIColor clearColor];
tableView.scrollEnabled=YES;
tableView.separatorColor=[UIColor clearColor];
[UIView addSubview:tableView];
}
The following should help
In Your class:
#implementation <YourClass> {
NSTimer* hideTimer;
}
Inside (IBAction)InfoVisibleAction:(id)sender
Instead of
[self performSelector:#selector(infoHiddenAction:) withObject:nil afterDelay:10];
Use
[hideTimer invalidate], hideTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(infoHiddenAction:) userInfo:nil repeats:NO];
Implement scrollViewDidScroll
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
[hideTimer invalidate], hideTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:#selector(infoHiddenAction:) userInfo:nil repeats:NO];
}
Inside (IBAction)infoHiddenAction:(id)sender
Add
[hideTimer invalidate];

Crash while popping view controller

I have one view controller (let's call it ViewController) with timer within its code, and have another view controller (let's call it ContentViewController) with webView within it which shows content depending on which button is clicked.
I have buttons on ViewController which, when clicked, pushes ContentViewController which loads a html file into its webView.
Timers are used to close ContentViewController if it is pushed.
Here's how I create timers:
-(void)createTimer :(NSNumber*)index
{
if (_show)
{
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:index, #"parameter1", nil];
switch ([index intValue])
{
case 1001:
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:YES];
break;
case 1002:
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:YES];
break;
case 1003:
[NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:YES];
break;
case 1004:
[NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:YES];
break;
}
}
}
Here's the code in ViewController which closes ContentViewController when timer fired:
-(void)updateUi :(NSTimer *)timer
{
int index = [[timer.userInfo objectForKey:#"parameter1"] intValue];
if([self.navigationController.visibleViewController isKindOfClass:[ContentViewController class]])
{
if ([[[NSUserDefaults standardUserDefaults]valueForKey:#"CurrentString"] intValue]==index )
{
[self.navigationController popViewControllerAnimated:YES];
}
}
}
And on web page which is shown on ContentViewController there's a button,when user clicks it ContentView must go back to ViewController. Here's how I do this:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *URL = [request URL];
if ([[URL scheme] isEqualToString:#"coffee-drink"])
{
NSString *urlString = [[request URL] absoluteString];
NSArray *urlParts = [urlString componentsSeparatedByString:#":"];
if (urlParts.count > 1) ///------------поменял
{
[self.navigationController popViewControllerAnimated:YES];
}
}
return YES;
}
All of above works fine but sometimes it crashes with the following error:
[ContentViewController respondsToSelector:]: message sent to deallocated instance
And also, I have other errors like these:
nested push animation can result in corrupted navigation bar
Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.
and
delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds
The last one appears very rarely.
I don't know what am I doing wrong.Can someone help me)?If needed any more information I will provide it!Thanks in advance!
Issue
You are popping your view without leaving ARC to deallocate all objects and delegates, by keeping them alive.
My Solution
For the NSTimer
At #implementation create NSTimer *timer and use it when you want to initialize it. To dealloc it correctly when you pop back, at viewWillDisappear set [timer invalidate] and timer = nil.
For delegates
Set you specific delegates to nil. For example self.delegate = nil
#Yuandra Ismiraldi is right. Your NSTimer will call your updateUI method every 20/10 seconds repeatedly untill it receives [timer invalidate]; instruction. You are getting this error messages as you navigate through your view controller tree. Sometimes your NavigationViewController will need more memory and will release some of un-used objects. In your case some of the previously shown view controllers that still runs the updateUI method every 20/10 seconds. Once this view controller has been released and your selector is called you will receive this crash.
I hope this explains why sometimes you get this crash, and sometimes not.
Your timer code
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:YES]
the repeat parameter is set to YES, this make your timer always repeat, thus repeating the sending of the message updateUI after the timer has run for the first time and the view has popped, thus resulting in your error. To make sure your timer only run once, set the repeat to NO
[NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(updateUi:) userInfo:dictionary repeats:NO]

UIWebView still active after pressing back button

I'm using a Navigationcontroller which pushes a new Viewcontroller containing a Webview every time a link is clicked. One of the URLs has a javascript function, which calls another function every 60 seconds. It works fine, but when I hit the back button once after being on that web view (containing the javascript), the webview stays active and keeps doing the javascript calls, while going back to the previous webview flawlessly. I can see it doing things through NSLog. Why is that and how can I avoid this?
EDIT:
I have declared my subclass of UIWebView in my Viewcontroller like this:
#property (strong, nonatomic) AFXTWebView *wv;
and this is how I instantiate it (in ViewDidLoad):
[self setWv:[[AFXTWebView alloc] initWithVC:self andURLString:[self.pageConfig objectForKey:xAFXTPageLink]]];
[self.view addSubview:self.wv];
[self.wv loadWebView];
I have also tried setting the webView to nil in viewWillDisappear:
-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
self.wv = nil;
}
You have to use the viewWillAppear & viewWillDisappear methods
-(void)viewWillAppear:(BOOL)animated
{
_webView=[[UIWebView alloc]init];
......//do your stuff
}
-(void)viewWillDisappear:(BOOL)animated
{
[_webView removeFromSuperView];
_webView=nil;
}
In your viewWillDisappear load a blank page into the web view.
This sounds like your web view and view controller are not being released, i.e. you have a leak. You should check your code for any unreleased retained/strong references to your view controller or web view.
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"about:blank"]]];
}

Another (!) Activity Indicator Issue

Well, after reading a bunch of SO posts on this issue, I still can't fix problems with my activity indicator. This indicator is in a view in a tab under the control of its own view controller. It has a view with a UIWebView which loads a local html page just fine. The view is loaded with initWithNibName and then awakeFromNib. Here's the part I think is relevant:
#implementation HelpViewController
#synthesize webView = _webView;
#synthesize back = _back;
#synthesize forward = _forward;
#synthesize aI = _aI;
- (void) viewDidLoad {
_aI = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_aI stopAnimating];
// Now add the web view
NSString *filePath = [[NSBundle mainBundle]
pathForResource:#"Help"
ofType:#"html"];
[self.view addSubview:_webView];
_webView.delegate = self;
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
[_webView loadRequest:request];
[super viewDidLoad];
}
-(void) webViewDidStartLoad:(UIWebView *)webView {
[_aI startAnimating];
_back.enabled = NO;
_forward.enabled = NO;
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
[_aI stopAnimating];
if (webView.canGoBack) {
_back.enabled = YES;
_back.highlighted = YES;
}
if (webView.canGoForward) {
_forward.enabled = YES;
_forward.highlighted = YES;
}
}
The navigation buttons work fine. The activity indicator was placed in the nib, but in the main view, not on/over/under the webView. In the attributes, I have Hides When Stopped checked. If I check Animating the indicator is always visible and animated, no matter how I navigate through the UIWebView.. If I uncheck Animating, Hidden is automatically checked for me. In this case, the indicator never shows up. So it's either always on or always off.
I've read quite a bit about cases where you need to have the indicator on a different thread. I'm not sure, but I don't think that applies here (I load a local html page but allow users to navigate off and then back to the local page). But, I seem to have some disconnect; perhaps it is the fact that the indicator is in the main view but the pages are in the webView? Or I'm not calling things in the right methods. Or who knows... Thanks for any help!
You overwrite _aI in your viewDidLoad and never place it in your view hierarchy, so the object you are sending the messages to is never visible and the activity indicator placed in interface build will never change its state, so thats why its either always animating or hidden.

UINavigationcontroller popViewcontroller does not work at second try

I encountered a little problem with my app. It is a simple problem but i can not find out what is the cause of crash.
I have a simple viewcontroller with only a webview (created and linked in storyboard) (ARC enabled):
#implementation BPActivateController
#synthesize mainWebView;
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"%#/test.php?code=%#", BASE_URL, [[NSUserDefaults standardUserDefaults] objectForKey:#"uniqueIdentifier"]]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[mainWebView loadRequest:requestObj];
[super viewDidLoad];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *html = [webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
if ([html rangeOfString:#"<h1>Not Found</h1>"].location != NSNotFound)
{
[self.navigationController popViewControllerAnimated:YES];
NSLog(#"pop the view controller");
}
}
- (void)viewDidUnload
{
[self setMainWebView:nil];
[super viewDidUnload];
}
The first time the view opens the popViewControllerAnimated is called and the user is redirected back to the last viewcontroller. But when the view is opened again, gives me a EXEC_BAD_ACCESS after the popViewControllerAnimated is called. When i enable the Zombie code gives me the following trace:
2012-06-04 11:51:12.717 actusmedicus[410:707] pop the view controller
2012-06-04 11:51:12.720 actusmedicus[410:707] *** -[BPActivateController respondsToSelector:]: message sent to deallocated instance 0xc6d95b0
I have tried several things but still is is not clear what selector is being called. I suspect that the webview is still busy..
Anyone has an idea that gets me back on track?
[EDIT]
The EXEC_BAD_ACCESS is fixed by setting the delegate of the webview to nil.
But the real problem is still there, the first time the view is pushed on the UINavigation stack, the web view does load and after a 404 the popViewControllerAnimated is successfully executed. But the second time we push the same view on the UINavigation stack (the same way we did the first time) The popViewControllerAnimated does not do anything. I have checked if it is nil but that does not seem to be the problem..
My first idea was that it runs in another thread and the UINavigationcontroller does not exist there. I ruled out that possibility with the help of performSelectorOnMainThread.
Finally i added a button to call popViewControllerAnimated manually and that works every time, so why doesn't it work when i call it programmatically?
I suspect that the webview is still busy..
If so then in your dealloc call the stopLoading method.
You should also set the delegate to nil in the dealloc.
Make sure if you are not segueing twice to the destination view controller. In my case, my code fired the segue twice. So I had to click back button twice to come back to my main controller.

Resources