0

I test this code on my iphone on airplane mode when I clik on the button displays a msg, but on the state where I connect to the internet, to play the button does not function and my application exit

This is the code:

-(void)playMovie {
    NSURL *url = [NSURL URLWithString:@"http://www.tvlaayoune.com/iphone/jt.mp4"];
    UIAlertView *errorView;
    if ([[Reachability sharedReachability]
            internetConnectionStatus] == NotReachable) {
        errorView = [[UIAlertView alloc]
                        initWithTitle: @"Unable To Connect To Server" 
                              message: @"Check your network connection and try again."
                             delegate: self
                        cancelButtonTitle: @"OK"
                        otherButtonTitles: nil];
    } else {
        moviePlayer = [[MPMoviePlayerController alloc]
                          initWithContentURL:url];
        [[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(moviePlayBackDidFinish:)
                   name:MPMoviePlayerPlaybackDidFinishNotification
    object:moviePlayer];
        moviePlayer.controlStyle = MPMovieControlStyleDefault;
        moviePlayer.shouldAutoplay = YES;
        [self.view addSubview:moviePlayer.view];
        [moviePlayer setFullscreen:YES animated:YES];
    } [errorView show];
}

What can be the problem ?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
  • The problem is that you do not quote the issue that is displayed in your log once the application exits. Also you do not quote the stack trace that should be visible within the debugger. – Till May 03 '12 at 20:13

1 Answers1

0

If I understand correctly, your code crashes when you have internet and want to display the movie. In that case, the last line of code will try and display errorView but it is not allocated if you have internet.

Move that show call in the same If :

-(void)playMovie {
    NSURL *url = [NSURL URLWithString:@"http://www.tvlaayoune.com/iphone/jt.mp4"];
    UIAlertView *errorView;
    if ([[Reachability sharedReachability]
            internetConnectionStatus] == NotReachable) {
        errorView = [[UIAlertView alloc]
                        initWithTitle: @"Unable To Connect To Server" 
                              message: @"Check your network connection and try again."
                             delegate: self
                        cancelButtonTitle: @"OK"
                        otherButtonTitles: nil];



         // Notice this line here:
         [errorView show];


     } else {
        moviePlayer = [[MPMoviePlayerController alloc]
                          initWithContentURL:url];
        [[NSNotificationCenter defaultCenter]
            addObserver:self
               selector:@selector(moviePlayBackDidFinish:)
                   name:MPMoviePlayerPlaybackDidFinishNotification
    object:moviePlayer];
        moviePlayer.controlStyle = MPMovieControlStyleDefault;
        moviePlayer.shouldAutoplay = YES;
        [self.view addSubview:moviePlayer.view];
        [moviePlayer setFullscreen:YES animated:YES];
    } 


    // Removed the show call from here

}
Andrei Stanescu
  • 6,353
  • 4
  • 35
  • 64