RGriffiths
RGriffiths

Reputation: 5970

Stopping a video stream when UIViewController is closed

I have a UIViewController with a UIWebView with the URL pointing to a video on my website which loads and streams fine when the page is opened.

I then have a Back button on the same page to take me back using a segue. However if I use this button to return the video continues to play in the background. I presume the segue does not actually close the view but just pushes the new view in front.

Can anyone advise me as to what I ought to do to either close the view or to stop the video downloading when the button is pressed?

Upvotes: 1

Views: 625

Answers (1)

jszumski
jszumski

Reputation: 7416

In viewWillDisappear: you can call stopLoading on the web view, which will stop any requests that are still loading, followed by loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]] to remove any loaded content. You can also send nil to loadRequest: (which as of iOS 6 has the same effect as the about:blank request) but that behavior isn't documented and could change in future releases.

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    [self.webView stopLoading];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"about:blank"]]];
}

Upvotes: 2

Related Questions