What is the problem with the back button in my UIWebView? - iphone

I have an issue in my application that i wrote coding for the back button in my webView setEnabled = NO, but when the application is launched and webViewDidFinishLoad the back button setEnabled = YES. I tried all possibility to set the back button enabled is equal false but it not works.
-(IBAction) backButton : (id) sender{
backTapped = YES;
[webView goBack];
}
-(IBAction) fwdButton : (id) sender{
forwardTapped = YES;
[webView goForward];
}
- (void)webViewDidStartLoad:(UIWebView *)thisWebView{
NSLog(#"webViewDidStartLoad");
[progressWheel startAnimating];
progressWheel.hidden = NO;
if(!backTapped){
back.enabled = NO;
}
if (!forwardTapped) {
forward.enabled = NO;
}
}
- (void)webViewDidFinishLoad:(UIWebView *)thisWebView
{
[progressWheel stopAnimating];
progressWheel.hidden = YES;
if (!backTapped) {
[back setEnabled:thisWebView.canGoBack];
back.showsTouchWhenHighlighted = YES;
}
if (!forwardTapped) {
[forward setEnabled:thisWebView.canGoForward];
forward.showsTouchWhenHighlighted = YES;
}
}

I can't actually quite understand the problem you are having, but I can see two potential issues:
1) You set backTapped and forwardTapped to YES, but never set them to NO anywhere.
2) Perhaps you don't have "back" or "forward" buttons wired in you xib - if they are nil then back.enabled = NO will do nothing.
Edit:
This logic seems backwards:
if (!backTapped)
back.enabled = NO;
In your code you set backTapped to YES, then this code is hit so !backTapped is ! YES which is NO.
Try
if (backTapped)
back.enabled = NO;

Related

YouTube video playback broken on iOS6 (works fine on iOS5)

In my app, I have a button which, when pressed, lets you watch a youtube video (a movie trailer). Within the app, without launching safari. Below you can see a code snippet. This code works pefrectly fine under iOS5. However, in iOS 6, the UIButton in findButtonInView is always nil. Any ideas what might be the reason?
youtubeWebView.delegate = self;
youtubeWebView.backgroundColor = [UIColor clearColor];
NSString* embedHTML = #" <html><head> <style type=\"text/css\"> body {background-color: transparent; color: white; }</style></head><body style=\"margin:0\"><embed id=\"yt\" src=\"%#?version=3&app=youtube_gdata\" type=\"application/x-shockwave-flash\"width=\"%0.0f\" height=\"%0.0f\"></embed></body></html>";
NSURL *movieTrailer;
if(tmdbMovie) movieTrailer = tmdbMovie.trailer;
else movieTrailer = [NSURL URLWithString:movie.trailerURLString];
NSString *html = [NSString stringWithFormat:embedHTML,
movieTrailer,
youtubeWebView.frame.size.width,
youtubeWebView.frame.size.height];
[youtubeWebView loadHTMLString:html baseURL:nil];
[self addSubview:youtubeWebView];
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
isWatchTrailerBusy = NO;
[manager displayNetworkActivityIndicator:NO];
//stop the activity indicator and enable the button
UIButton *b = [self findButtonInView:_webView];
//TODO this returns null in case of iOS 6, redirect to the youtube app
if(b == nil) {
NSURL *movieTrailer;
if(tmdbMovie) {
movieTrailer = tmdbMovie.trailer;
} else {
movieTrailer = [NSURL URLWithString:movie.trailerURLString];
}
[[UIApplication sharedApplication] openURL:movieTrailer];
} else {
[b sendActionsForControlEvents:UIControlEventTouchUpInside];
}
}
- (UIButton *)findButtonInView:(UIView *)view {
UIButton *button = nil;
if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if (view.subviews && [view.subviews count] > 0) {
for (UIView *subview in view.subviews) {
button = [self findButtonInView:subview];
if (button) return button;
}
}
return button;
}
Apple changed how YouTube videos are handled in iOS6. I also was using the findButtonInView method but that no longer works.
I've discovered the following seems to work (untested in iOS < 6):
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.mediaPlaybackRequiresUserAction = NO;
}
- (void)playVideo
{
self.autoPlay = YES;
// Replace #"y8Kyi0WNg40" with your YouTube video id
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#", #"http://www.youtube.com/embed/", #"y8Kyi0WNg40"]]]];
}
// UIWebView delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (self.autoPlay) {
self.autoPlay = NO;
[self clickVideo];
}
}
- (void)clickVideo {
[self.webView stringByEvaluatingJavaScriptFromString:#"\
function pollToPlay() {\
var vph5 = document.getElementById(\"video-player\");\
if (vph5) {\
vph5.playVideo();\
} else {\
setTimeout(pollToPlay, 100);\
}\
}\
pollToPlay();\
"];
}
dharmabruce solution works great on iOS 6, but in order to make it work on iOS 5.1, I had to substitute the javascript click() with playVideo(), and I had to set UIWebView's mediaPlaybackRequiresUserAction to NO
Here's the modified code:
- (void)viewDidLoad
{
[super viewDidLoad];
self.webView.mediaPlaybackRequiresUserAction = NO;
}
- (void)playVideo
{
self.autoPlay = YES;
// Replace #"y8Kyi0WNg40" with your YouTube video id
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#%#", #"http://www.youtube.com/embed/", #"y8Kyi0WNg40"]]]];
}
// UIWebView delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if (self.autoPlay) {
self.autoPlay = NO;
[self clickVideo];
}
}
- (void)clickVideo {
[self.webView stringByEvaluatingJavaScriptFromString:#"\
function pollToPlay() {\
var vph5 = document.getElementById(\"video-player-html5\");\
if (vph5) {\
vph5.playVideo();\
} else {\
setTimeout(pollToPlay, 100);\
}\
}\
pollToPlay();\
"];
}
I have solved the problems with dharmabruce and JimmY2K. solutions, with the fact that it only works the first time a video is played, as mentioned by Dee
Here is the code(including event when a video ends):
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
videoView = [[UIWebView alloc] init];
videoView.frame = frame;
videoView.delegate = self;
[videoView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];// this format: http://www.youtube.com/embed/xxxxxx
[self.view addSubview:videoView];
//https://github.com/nst/iOS-Runtime-Headers/blob/master/Frameworks/MediaPlayer.framework/MPAVController.h
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(playbackDidEnd:)
name:#"MPAVControllerItemPlaybackDidEndNotification"//#"MPAVControllerPlaybackStateChangedNotification"
object:nil];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//http://stackoverflow.com/a/12504918/860488
[videoView stringByEvaluatingJavaScriptFromString:#"\
var intervalId = setInterval(function() { \
var vph5 = document.getElementById(\"video-player\");\
if (vph5) {\
vph5.playVideo();\
clearInterval(intervalId);\
} \
}, 100);"];
}
- (void)playbackDidEnd:(NSNotification *)note
{
[videoView removeFromSuperview];
videoView.delegate = nil;
videoView = nil;
}
Untested and not sure if that is the problem here, becasue I don't know the URL you're using. But I stumbled right about following:
As of iOS 6, embedded YouTube URLs in the form of http://www.youtube.com/watch?v=oHg5SJYRHA0 will no longer work. These URLs are for viewing the video on the YouTube site, not for embedding in web pages. Instead, the format that should be used is described here: https://developers.google.com/youtube/player_parameters.
from http://thetecherra.com/2012/09/12/ios-6-gm-released-full-changelog-inside/
I'm working on this same general problem and I'll share the approach I've come up with. I have some remaining kinks to work out for my particular situation, but the general approach may work for you, depending on your UI requirements.
If you structure your HTML embed code so that the YouTube player covers the entire bounds of the UIWebView, that UIWebView effectively becomes a big play button - a touch anywhere on it's surface will make the video launch into the native media player. To create your button, simply cover the UIWebView with a UIView that has userInteractionEnabled set to NO. Does it matter what the size the UIWebView is in this situation? Not really, since the video will open into the native player anyway.
If you want the user to perceive an area of your UI as being where the video is going to "play", then you can grab the thumbnail for the video from YouTube and position that wherever. If need be, you could put a second UIWebView behind that view, so if the user touches that view, the video would also launch - or just spread the other UIWebView out so that it is "behind" both of these views.
If you post a screenshot of your UI, I can make some more specific suggestions, but the basic idea is to turn the UIWebView into a button by putting a view in front of it that has user interaction disabled.
I've found that the code below works for both iOS5 & iOS6. In my example, I have a text field that contains the url to the video. I first convert the formats to the same starting string 'http://m...". Then I check to see if the the format is the old format with the 'watch?v=' and then replace it with the new format. I've tested this on an iPhone with iOS5 and in the simulators in iOS5 & iOS6:
-(IBAction)loadVideoAction {
[activityIndicator startAnimating];
NSString *urlString = textFieldView.text;
urlString = [urlString stringByReplacingOccurrencesOfString:#"http://www.youtube" withString:#"http://m.youtube"];
urlString = [urlString stringByReplacingOccurrencesOfString:#"http://m.youtube.com/watch?v=" withString:#""];
urlString = [NSString stringWithFormat:#"%#%#", #"http://www.youtube.com/embed/", urlString];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicator stopAnimating];
}
I believe the problem is that when webViewDidFinishLoad was triggered, the UIButton was not added to the view yet. I implemented a small delay to find the button after the callback is returned and it works for me on ios5 and ios6. drawback is that there is no guarantee the UIButton is ready to be found after the delay.
- (void)autoplay {
UIButton *b = [self findButtonInView:webView];
[b sendActionsForControlEvents:UIControlEventTouchUpInside]; }
- (void)webViewDidFinishLoad:(UIWebView *)_webView {
[self performSelector:#selector(autoplay) withObject:nil afterDelay:0.3]; }
I had to use the url http://www.youtube.com/watch?v=videoid this is the only way it will work for me
- (void) performTapInView: (UIView *) view {
BOOL found = NO;
if ([view gestureRecognizers] != nil) {
for (UIGestureRecognizer *gesture in [view gestureRecognizers]) {
if ([gesture isKindOfClass: [UITapGestureRecognizer class]]) {
if ([gesture.view respondsToSelector: #selector(_singleTapRecognized:)]) {
found = YES;
[gesture.view performSelector: #selector(_singleTapRecognized:) withObject: gesture afterDelay: 0.07];
break;
}
}
}
}
if (!found) {
for (UIView *v in view.subviews) {
[self performTapInView: v];
}
}
}
#pragma mark - UIWebViewDelegate methods
- (void) webViewDidFinishLoad: (UIWebView *) webView {
if (findWorking) {
return;
}
findWorking = YES;
[self performTapInView: webView];
}

Unable to keep UIButton in selected state after TouchUpInside Event

I have the need for an UIButton to maintain a pressed state. Basically, if a button is in a normal state, I want to touch the button, it highlight to its standard blue color and then stay blue after lifting my finger.
I made the following UIAction and connected the buttons Touch Up Inside event to it.
-(IBAction) emergencyButtonPress:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
But what happens, after I remove my finger, the button goes back to a white background. For a test I added a UISwitch and have it execute the same code:
-(IBAction) emergencySwitchClick:(id) sender
{
if(emergencyButton.highlighted)
{
emergencyButton.selected = NO;
emergencyButton.highlighted = NO;
}
else
{
emergencyButton.selected = YES;
emergencyButton.highlighted = YES;
}
}
In this case the button toggles to a highlighted and non-highlighted state as I would expect.
Is there another event happening after the Touch Up Inside event that is resetting the state back to 'normal'? How do I maintain the highlighted state?
The highlighted state is applied and removed by iOS when you touch / release the button. So don't depend on it in your action method.
As one of the other answers says, set the highlighted image to be the same as the selected image, and then modify your action method:
-(IBAction) emergencyButtonPress:(id) sender
{
emergencyButton.selected = !emergencyButton.selected;
}
If you want something like a toggle button, customize your "selected" state to be your "pressed" state, and then write something like this:
- (IBAction)buttonTapped:(id)sender {
[(UIButton*)sender setSelected:![sender isSelected]];
}
if you want the above code to work, you should set an image for the state selected and one (even the same) for he state highlighted.
[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateHighlighted];
[emergencyButton setBackgroundImage:buttonHighlightImage forState:UIControlStateSelected];
hope it helps.
I had this same problem, and this is what worked for me:
-(IBAction)buttonPressed:(id)sender {
if (isSelected) {
[_button setSelected:NO];
[_button setImage:[UIImage imageNamed:#"not_selected.png"] forState:UIControlStateSelected];
isSelected=NO;
}
else {
[_button setSelected:YES];
[_button setImage:[UIImage imageNamed:#"selected.png"] forState:UIControlStateSelected];
isSelected=YES;
}
}
This is not possible unfortunately, as soon as you let go Apple changes the state again.
One option I've used before (but not pretty) is that you use a performSelector with delay 0.1 after the button is pressed to put it again in the selected state. This did work in my case.
EDIT: If you don't want to see the blinking effect, just set the delay to 0.0
This worked for me:
- (void)tapButton:(id)sender{
UIButton *button = sender;
if (!button.selected){
[self performSelector:#selector(highlight:) withObject:button afterDelay:0.0];
}else{
[self performSelector:#selector(removeHighlight:) withObject:button afterDelay:0.0];
}
}
- (void)highlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = YES;
}
- (void)removeHighlight:(UIButton *)button{
button.selected = !button.selected;
button.highlighted = NO;
}
Try this simple answer....
- (void)mybutton:(id)sender
{
UIButton *button = (UIButton *)sender;
button.selected = ![button isSelected]; // Important line
if (button.selected)
{
NSLog(#"Selected");
NSLog(#"%i",button.tag);
}
else
{
NSLog(#"Un Selected");
NSLog(#"%i",button.tag);
}
}

i have a problem in UIImageView's animating

-(IBAction) change {
self.imageView.animationImages = myImages;
self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}
hello, i'm studying iOS programming.
but i have a question.
i have a button and when i click the button, then this method will be called.
first i click the button, then this code will start the if statement. that's what i want.
i click the button again, i think that will execute the else statement.
but it always execute the if statement only.
why is that?
i really don't know why is that. please help me
I think setting the properties like animationImages or animationDuration will stop the animation, so that by clicking, you every time stop and then just after (re)start it in the if part. Try setting these two properties outside the action method you wrote, and just let the if/else sequence.
-(IBAction) change {
// set these two anywhere else
//self.imageView.animationImages = myImages;
//self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}

switch.hidden = YES not working, outlets all correctly set up

I am having a problem that is totally confounding me. Please look at the code below, it is from the book "Beginning iPhone 4 Development" chapter 4. I'm new to this :)
- (IBAction)toggleControls:(id)sender
{
if([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
{
NSLog(#"Show switches");
[self.leftSwitch setHidden:NO];
[self.rightSwitch setHidden:NO];
[self.doSomethingButton setHidden:YES];
}
else
{
NSLog(#"Hide switches");
[self.leftSwitch setHidden:YES];
[self.rightSwitch setHidden:YES];
[self.doSomethingButton setHidden:NO];
}
}
The strange thing is that it logs this correctly but the ui controls aren't hiding/showing.
I also tried this (original in book):
- (IBAction)toggleControls:(id)sender
{
if([sender selectedSegmentIndex] == kSwitchesSegmentIndex)
{
NSLog(#"Show switches");
leftSwitch.hidden = NO;
rightSwitch.hidden = NO;
doSomethingButton.hidden = YES;
}
else
{
NSLog(#"Hide switches");
leftSwitch.hidden = YES;
rightSwitch.hidden = YES;
doSomethingButton.hidden = NO;
}
}
It sounds like you may have forgotten to wire up your outlets in Interface Builder. Check the values of leftSwitch and rightSwitch when this method is called by using a break point or an NSLog.

iPhone - How to have a custom browser be able to detect if back/forward buttons need to be enabled/disabled

Hey guys! I have a tumblr app that I am developing, and I have a button that pushes a modal view that contains a webview and a toolbar on the bottom with refresh, back, forward and done buttons. The back forward and refresh buttons work, however I want to be able to tell if the webview can actually go back/forward and if not, disable the button..I have tried the code below, and the image nor the enable/disable changes.
- (IBAction)refreshPage {
[signUpWebView reload];
}
- (IBAction)goBack {
[signUpWebView goBack];
}
- (IBAction)goForward {
[signUpWebView goForward];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
// Enable or disable back
if ([signUpWebView canGoBack]) {
[backOnePage setEnabled:YES];
backOnePage.image = [UIImage imageNamed:#"backButton"];
} else {
[backOnePage setEnabled:NO];
backOnePage.image = [UIImage imageNamed:#"backButtonDisabled"];
}
// Enable or disable forward
if ([signUpWebView canGoForward]) {
[forwardOnePage setEnabled:YES];
forwardOnePage.image = [UIImage imageNamed:#"forwardButton"];
} else {
[forwardOnePage setEnabled:NO];
forwardOnePage.image = [UIImage imageNamed:#"forwardButtonDisabled"];
}
}
Any recommendations would be greatly appreciated!
I recommend binding the forward and backward button directly to the UIWebview.
Automatically enable and disable like this:
- (void)webViewDidStartLoad:(UIWebView *)mwebView {
backButton.enabled = (webView.canGoBack);
forwardButton.enabled = (webView.canGoForward);
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
backButton.enabled = (webView.canGoBack);
forwardButton.enabled = (webView.canGoForward);
}
When you first initialize your webView, try starting it with a request, like this:
[signUpWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.google.com"]]];
So you're initializing the webView with loadRequest: instead of loadHTMLString:, or something similar.