Stopping a video stream when UIViewController is closed - ios

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?

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

Related

How to create a button close webview or goback to app in webview objective c

I am very new at Objective-C and iOS programming and I want to create something like X or Back button to close a web view. I am displaying a video from a web URL and on iPhone I have no physical back button like Android devices. So when I show the web view there is nothing to close it and go back to the application, just showing video.
Here is my code
if([_mServiceID isEqual:#"movFilePlay"] == YES) //Stark
{
dispatch_async(dispatch_get_main_queue(), ^{
[_mainView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[dic strValueForKey:#"filePath"]]]];
[self.mainView canGoBack];
});
}
I have no idea where to put a button in this application. There are too many packages and controllers. Please help me, thanks!

Stop webview reload/refresh when app comes to Foreground

I m working on a multiview UIWebView. This webview loaded an external url. This url get reloaded everytime the app comes to the foreground and goes to the first page. This is causing the user to enter any kind of data again and again. Is there a way to stop this url from reloading from webview?
I have already tried using:
1. [self.webView stopLoading];
2. shouldStartLoadingWithRequest making it return NO;
3. UIWebViewNaviagationTypeReload and UIWebViewNaviagationTypeOther
to check if the webviews load status.
But no luck. The Webview keeps reloading?
Can anyone tell me is there any other way to stop the refresh for this url inside webview?

Why is UIWebView canGoBack=NO in iOS7?

I'm embedding this web site into my app like this:
NSString *url = [NSString stringWithFormat:#"https://mobile.twitter.com/search?q=%#", #"#test OR #test"];
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[self.twitterWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
self.twitterWebView.scalesPageToFit = YES;
And I have 2 buttons for going back and forward in this web site. I'm calling
[self.twitterWebView goBack]; and
[self.twitterWebView goForward]; accordingly.
This works fine on iOS 6 but on iOS 7, my web view's canGoBack and canGoForward properties are NO and thus my back and forward buttons do not work.
As a side note, when the app is installed the first time, and the page is loaded the first time, my buttons work. But when I run my app again, and when I tap on a link on the web site, my web view's canGoBack property begins returning always NO.
How can I solve this?
EDIT: I uploaded a mini test app that demonstrates my problem. You can download it from here. Please run the app on an iOS 7 simulator, see that the back button is working on the first installation of the app. Then quit, run the app again and you'll see that it'll stop working.
By the way the problem seems to be about the twitter mobile site. You can try another web site address and see that.
This seems to be related to HTML5's "Application Cache" functionality. On first launch, the site isn't cached and the UIWebView correctly detects if it can go forward or back. As soon as the cache is populated, new UIWebView instances decide that, even if the URL changes (which can be observed in UIWebViewDelegate's webView:shouldStartLoadWithRequest:navigationType:), going forward or back is not possible anymore. canGoForward and canGoBack will return NO and goForward and goBack won't do anything. This persists across restarts of the app, as long as the HTML5 cache for this specific site exists.
Maybe this problem is limited to web apps that modify the URL's Fragment identifier after the hashmark via JavaScript.
And yes, the UIWebView's behavior in this situation DID change between iOS 6 and iOS 7.
I haven't found a solution yet, and we'll probably have to wait for Apple to fix this in iOS 7.1 or so.
Edit
Other people have this problem, too:
If you are using Application Cache and also managing
states through hash or other technique, the history object will not
keep your navigation history, therefore history.back() will never work
and history.length stays in 1 forever.
(from http://www.mobilexweb.com/blog/safari-ios7-html5-problems-apis-review)
Edit 2
This problem exists in Safari 7.0 (9537.71, default in OS X 10.9 Mavericks), too. However, the most recent WebKit nightly build (r158339) seems to work correctly. It's most likely only a matter of time until the fix makes it to an iOS and OS X release.
Edit 3
This problem still exists in iOS 7.1 and and OS X 10.9.2.
Edit 4
This bug has been fixed in iOS 8 and Safari 7.1 (9537.85.10.17.1) for OS X!
Related:
history object doesn't keep navigation history ios7 safari
I had this issue too in iOS 7. What worked for me was moving the "canGoBack" code and "canGoForward" code to shouldStartLoadWithRequest as shown below. Before, I had it in webViewDidFinishLoad, which worked for iOS 6, but did not for iOS 7.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType
{
if ([webView canGoBack])
{
[browserBackItem setEnabled:YES];
}
else
{
[browserBackItem setEnabled:NO];
}
if ([webView canGoForward])
{
[browserForwardItem setEnabled:YES];
}
else
{
[browserForwardItem setEnabled:NO];
}
return YES;
}
I had the same issue. I am able to resolve it with the following changes.
Implemented a new method updateButtons
- (void)updateButtons:(UIWebView*)theWebView {
if ([theWebView canGoBack])
{
self.backButton.enabled = YES;
}
else
{
self.backButton.enabled = NO;
}
if ([theWebView canGoForward])
{
self.forwardButton.enabled = YES;
}
else
{
self.forwardButton.enabled = NO;
}
}
Added Calling the above method in shouldStartLoadWithRequest, webViewDidFinishLoad, didFailLoadWithError events.
Now the tricky part comes. After making above changes, back and forward buttons are working as expected except in one scenario. When we are back to first page by hitting back button, it is not getting disabled. Because it will not fire any of the above events when the page is getting loaded by hitting back/forward button. it just loads from cache.
I have tried many approaches but only one that solved my problem.
Added an observer on WebHistoryItemChangedNotification.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(webViewHistoryDidChange:)
name:#"WebHistoryItemChangedNotification"
object:nil];
Called the same updatebuttons method in webViewHistoryDidChange.
- (void)webViewHistoryDidChange
{
[self updateButtons:self.webView];
}
After changing the property to "strong" reference, my problem disappeared.
before:
#property (nonatomic, weak) IBOutlet UIWebView *webView;
after changing the property to "strong":
#property (nonatomic, strong) IBOutlet UIWebView *webView;

Refresh UIwebview without any shaking?

Currently iam refreshing webview using the following code
[webview reload];
but in this case when i open that viewcontroller i can see a sudden movement(shake or jump) of UIWebview in viewcontroller .Is there any way to to this refresh in background i mean without any change in webview movement?
You van do the same refresh functionality as a background thread,So it will work.

open url with back button

I have one little truble.
I want to open URL in my iOS application.
The way I found on the web was :
[[UIApplication sharedApplication] openURL:url];
and it works fine, but this approach close my app and open safari without back button to return in my app. How to achieve this?
I would like something like
MFMessageComposeViewController * picker = [[MFMessageComposeViewController alloc] init];
[self presentModalViewController:picker animated:YES];
Rather than opening the URL in Safari why not just create a new view controller containing a UIWebView?
Show the view controller, pass in the URL, and load the URL into the UIWebView. Something like this:
MyWebViewController *mwvc = [[MyWebViewController alloc] initWithURL:myURL];
[self.navigationController pushViewController:mwvc animated:YES];
Then in MyWebViewController:initWithURL
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:myURL]];
[myWebView loadRequest:request];
Safari is the application provided by Apple itself, so one cannot make change in behaviour of Sarari.
If you require a back button then create your custom UIViewController using UIWebView and place your back button over there.
For back functionality, you can either use UINavigationController or UIViewAnimation.
To get the control back to main native iOS application, you can URLScheme concept. URLScheme concept gives you unique identifier url of a native iOS application.
Check this http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-working-with-url-schemes/
It gives you fair idea on the procedure to achieve your requirement.

Resources