可达性问题

时间:2012-05-03 15:53:23

标签: objective-c ios mpmovieplayercontroller reachability

我在飞机模式下在我的iphone上测试此代码当我按下按钮显示一个消息,但在我连接到互联网的状态,播放按钮不起作用,我的应用程序退出

这是代码:

-(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];
}

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,当您有互联网并希望显示电影时,您的代码会崩溃。在这种情况下,最后一行代码将尝试显示errorView但如果您有互联网则不会分配。

将该show call移动到相同的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

}