getting errors and freezing when deploying UIWebView with youtube page..

Discussion in 'iPhone/iPad Programming' started by primedime, Apr 17, 2011.

  1. macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #1
    I have two applications in which I use a UIwebView in order to show the mobile youtube page.... I am not sure how to really explain the actual error because this is the first time I am seeing this type of problem... The thing is that it only happens if I try to open that webview multiple times in one session. If I just open it one time it works fine, the second I launch it again it freezes and crashes.... it says Thread1: Program received signal "EXC_BAD_Access".

    Here is the code, any help is much appreciated:

    Code:
    #import "YoutubeViewController.h"
    #import "MainViewController.h"
    
    @implementation YoutubeViewController
    
    @synthesize myWebView;
    @synthesize toolbar;
    
    - (void)loadWebPageWithString:(NSString *)urlString
    {
    	NSURL *url = [NSURL URLWithString:urlString];
    	NSURLRequest *request = [NSURLRequest requestWithURL:url];
    	[myWebView loadRequest:request];
    }
    
    -(IBAction)back
    {
    	[self.parentViewController dismissModalViewControllerAnimated:YES];
    }
    
    -(IBAction)goHome 
    {
        NSString *urlAddress = @"http://m.youtube.com/primedime21";
        
        NSURL *address = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:address];
        [myWebView loadRequest:requestObj];
    }
    
    -(IBAction)reload
    {
    	NSLog(@"reload");
    }
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
    	[myWebView stopLoading];
    }
    
    - (void)dealloc
    {
        [myWebView release];
        [super dealloc];
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	
    	[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.youtube.com/primedime21"]]];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	
    	return YES;
    }
    
    @end
    
     
  2. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #2
    I have had the same problem before with putting youtube user pages in an app. I think the only solution is to work around the problem... embed the videos you want to play or have the app navigate to the youtube app for playback.

    Nick
     
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    EXC_BAD_ACCESS is caused by over-released or under-retained objects. You should probably post the code where you present your YoutubeViewController, since that's where your issue seems to be popping up.
     
  4. thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #4
    Here is my main view:

    Code:
    #import "MainViewController.h"
    
    @implementation MainViewController
    
    /*
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }
    */
    
    -(IBAction)switchView{
       
        YoutubeViewController *youtube = [[YoutubeViewController alloc] 
                                          initWithNibName:@"YoutubeViewController" bundle:nil];
        youtube.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentModalViewController:youtube animated:YES];
        [youtube release];
        
        FacebookViewController *facebook = [[FacebookViewController alloc]
                                            initWithNibName:@"FacebookViewController" bundle:nil];
        facebook.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
        [self presentModalViewController:facebook animated:YES];
        [facebook release];
        
    }
    
    
    // set up email button
    - (IBAction)email:(id)sender;
    {
        MFMailComposeViewController *mailComposer = 
        [[MFMailComposeViewController alloc] init];
        mailComposer.mailComposeDelegate = self;
        
        if ([MFMailComposeViewController canSendMail]) {
            [mailComposer setToRecipients:[NSArray 
                                           arrayWithObjects:@"rjossfolk@gmail.com", nil]];
            [mailComposer setSubject:@"Inquery from mobile app"];
            [mailComposer setMessageBody:nil isHTML:NO];
            [self presentModalViewController:mailComposer animated:YES];
        }
    }
    
    - (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
    {
        [self dismissModalViewControllerAnimated:YES];
        
        if (result == MFMailComposeResultFailed) {
            UIAlertView *alert = [[UIAlertView alloc] 
                                  initWithTitle:@"Unable to send mail" 
                                  message:@"Please,check your internet connection and try again"
                                  delegate:self 
                                  cancelButtonTitle:@"OK" 
                                  otherButtonTitles:nil];
            [alert show];
            [alert release];
        }
    }
    
    
    //Flip side
    - (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    
    - (IBAction)showInfo:(id)sender
    {    
        FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
        controller.delegate = self;
        
        controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:controller animated:YES];
        
        [controller release];
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        // Return YES for supported orientations.
        return YES;
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc. that aren't in use.
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
    
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (void)dealloc
    {
        [super dealloc];
    }
    
    @end
    
    Here is the YoutubeViewController:

    Code:
    #import "YoutubeViewController.h"
    #import "MainViewController.h"
    
    @implementation YoutubeViewController
    
    @synthesize myWebView;
    @synthesize toolbar;
    
    - (void)loadWebPageWithString:(NSString *)urlString
    {
    	NSURL *url = [NSURL URLWithString:urlString];
    	NSURLRequest *request = [NSURLRequest requestWithURL:url];
    	[myWebView loadRequest:request];
    }
    
    -(IBAction)back
    {
    	[self.parentViewController dismissModalViewControllerAnimated:YES];
    }
    
    -(IBAction)goHome 
    {
        NSString *urlAddress = @"http://m.youtube.com/primedime21";
        
        NSURL *address = [NSURL URLWithString:urlAddress];
        NSURLRequest *requestObj = [NSURLRequest requestWithURL:address];
        [myWebView loadRequest:requestObj];
    }
    
    -(IBAction)reload
    {
    	NSLog(@"reload");
    }
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
    	[myWebView stopLoading];
    }
    
    - (void)dealloc
    {
        [myWebView release];
        [super dealloc];
    }
    
    - (void)didReceiveMemoryWarning
    {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
        
        // Release any cached data, images, etc that aren't in use.
    }
    
    #pragma mark - View lifecycle
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	
    	[self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://m.youtube.com/primedime21"]]];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    	
    	return YES;
    }
    
    @end
    

    Let me know if you need to see anything else.... Thanks a lot!
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Explain switchView. Why are you presenting a YoutubeViewController and a FacebookViewController?
     
  6. thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #6
    If you take a look at that image I have 4 buttons at the bottom not including the info button. for the youtube, facebook and linkedin I want to open them in a UIWebView and the Twitter button will open a twitter feed I have made in my previous application.... The switchView is the event I am using to switch from the main view to the views for youtube and facebook... I think i am missing an IF statement in there which I am looking up currently but none the less my youtubeviewcontroller crashes if I open it more than once in a session.... Am I clear enough in this description?


    [​IMG]
    Screen shot 2011-04-17 at 12.13.38 PM by primedime, on Flickr
     
  7. macrumors 603

    Joined:
    Aug 9, 2009
    #7
    Have you run your code using Instruments, with zombies enabled?

    That should almost always be the first step when getting this kind of error.
     
  8. thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #8
    To be totally honest with you I have never used instruments before, I am pretty new to xcode and still learning. I have tried to open it before but didn't know what to do next.... I have troubles reading the resources as well since I have never done any coding before I started a few months ago but I am getting better at understanding the terminology.... I plan on looking in to instruments though because I have an app ready for the store but when I put it on my phone it has a few crashes here and there.... This is my second app now so hopefully soon I can use instruments...
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    Also, might want to try Build and Analyze (Xcode 3) or Project > Analyze (Xcode 4).
     
  10. thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #10
    Ok, I used the Analyze thing you mentioned and the only thing that came up is this:

    [​IMG]
    Screen shot 2011-04-17 at 3.46.44 PM by primedime, on Flickr


    This has to do with the mail button I have but that is working perfectly fine on the simulator and on my iphone....
     
  11. macrumors 603

    Joined:
    Aug 9, 2009
    #11
    It's not overly difficult.
    http://meandmark.com/blog/2010/09/using-nszombie-with-instruments/

    http://developer.apple.com/library/...mentsUserGuide/AboutTracing/AboutTracing.html
     
  12. thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #12
    Cool, I am going to run it right now and see what's up... I already added all the other views and attached them to all the other buttons... The only view giving me trouble is the YoutubeViewController.....
     
  13. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #13
    The error shown by Build and Analyze is correct. Your code shows a leak of the mailcomposer. There should be a release for that view controller at the end of the method. However that memory leak shouldn't cause a crash.

    What line in your code is causing the bad access crash?
     
  14. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #14
    Try changing the url to anything other than youtube and you will see that everything is working fine. The Youtube user page is your problem. Do a google search of your topic and you will find that this a common issue with youtube. You can however have the youtube button open up the youtube app to play videos from there. http://forums.macrumors.com/showthread.php?t=1093715&highlight=
     
  15. thread starter macrumors member

    Joined:
    Mar 21, 2011
    Location:
    Ft. Lauderdale, FL
    #15
    I added [mailComposer release]; to it and it still shows a leak. Any ideas on what exactly I am missing?

    And the error is in the main.m file

    Code:
    #import <UIKit/UIKit.h>
    
    int main(int argc, char *argv[])
    {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        int retVal = UIApplicationMain(argc, argv, nil, nil); <-- here is the error
        [pool release];
        return retVal;
    }
    

    Also, how would I go about having it open the youtube app to my youtube channel? Thanks guys!
     
  16. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    Added it to what? Where?
     
  17. macrumors regular

    nickculbertson

    Joined:
    Nov 19, 2010
    Location:
    Nashville, TN
    #17
    perhaps you could load the youtube page in the mainview's viewdidload method and make it hidden until the youtube button is pushed (without opening a second xib). This way the page is only loaded once with the viewdid of your mainview. I've never tried it but its worth a shot.

    Nick
     

Share This Page