There is a list of videos in main view. In that main view, i have added a UIWebView as subview to play videos.
I suppose to do, when i shake iPhone it should play the next video in UIWebView.
It is working fine for the very first time but as come back to main view and again try to perform the same thing, it is not reacting.
Any solution please?
Make sure when the view (re)appears that you set it as the first responder.
-(void)viewDidAppear:(BOOL)animated
{
[self becomeFirstResponder];
}
It needs to be in viewDidAppear and not viewWillAppear
Related
I am updating my project from UIWebView to WKWebView. In existing UIWebView approach where UIWebView does't have any parent until it runs javascript and once it has content, it adds a parent to the webview. Content renders fine using UIWebView. I am using the same approach for WKWebView, however WKWebview stuck at loading. Is there any way we can execute javascript on wkwebview without adding parent to the webview. Another interesting thing is it works on simulator not on device. Using localHTTPServer to serve the html, css and images.
Any Suggestions or Tips to solve the above issue.
I have observed similar behavior in my app running socket.io in a headless WKWebView. In my case, the web socket would disconnect all the time and not reconnect.
It seems that since WKWebView runs javascript off-process that it will pause any javascript that is considered idle to save resources (<-IMO). Idle includes not having a parent or the app/device is inactive. So, yes, it seems like you are correct in that it needs a parent view in most cases. I was able to work around this using the following code:
WKWebViewConfiguration* webViewConfig = // set up config
// provide empty frame rect
WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:webViewConfig];
// add to keyWindow to ensure it is 'active'
[UIApplication.sharedApplication.keyWindow addSubview:webView];
This approach at least ensures the javascript runs while the app is active and doesn't effect the UI. If you need to display it later you can remove the webView from the keyWindow after render and reset the frame. And, yes, I also observed that in the simulator this is not an issue (javascript ALWAYS runs, regardless of parent or app state)
Hope this helps!
The accepted solution worked for me some of the time. I tried something else which appears to work for me consistently:
self.keepWebViewActiveTimer = [NSTimer scheduledTimerWithTimeInterval:0.2
target:self
selector:#selector(_keepWKWebViewActive:)
userInfo:nil
repeats:YES];
Where _keepWKWebViewActive is:
-(void) _keepWKWebViewActive:(NSTimer*) timer{
if(self.webView) {
[self.webView evaluateJavaScript:#"1+1" completionHandler:nil];
}
}
I have a uiviewcontroller which loads a uiwebview. When the uiviewcontroller loads the uiwebview everything is done correctly. But when one navigates away and returns back and the uiwebview is not loaded, it is displayed differently. The uiwebview scrollview's contentsize height changes by 64px, it gets higher.
When it is loaded for the first it looks like this
When one returns back to the view, this is what happens
I hope I was able to make my problem understandable. If more details are necessary, I am able to give more. Thank you
I had a similar problem, try this code in your viewDidLoad method.
if([self respondsToSelector:#selector(setAutomaticallyAdjustsScrollViewInsets:)])
self.automaticallyAdjustsScrollViewInsets = NO;
You can do like this.
- (void) viewDidLoad
{
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
}
I'm making a tab bar app and one of the tabs is a youtube channel. It works fine and is perfect but the one problem is when i click on the video it plays but only in the standard view and not in Landscape. Any ideas on how to fix. Im sure its really simple but i've been having no luck.
This question probably answers what you are trying to do:
Webview resizes automatically to portrait and landscape view in iphone
The key thing to take away from it is that you need your view controller to tell the app that it supports the other orientations, not just portait.
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) orientation
{
return YES;
}
I have a ViewController with a UIWebView, in a Objective-c project for iPad.
When I add html (with loadHtmlString), I try to keep the scroll position it had before the loadHtml, but I cannot help and it returns to the top.
Here are the lines I use:
scrollPosition = [[webView stringByEvaluatingJavaScriptFromString: #"scrollY"] intValue];
[webView loadHTMLString:html baseURL:nil];
[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0, %d);",scrollPosition]];
I tested the value 'scrollY' at different locations in my code
and it stays at the value scrollPosition that I want, but when it ends up showing on the screen the WebView is always at the top (with the scrollY value then to 0).
Is there someone who could help me figure out why this happens?
I checked for hours the answers online, also on StackOverflow.
I tried different solutions (like adding in my webview the JavaScript "onclick="scroll(); return false;", but to no avail.
I tried to self-delegate the webView and update the scrollTo in webViewDidFinishLoad, but it seems that webViewDidFinishLoad is not called (the breakpoint is not reached) although I did add in .h in the interface and in the .m in ViewDidLoad the line: webView.delegate=self;
Thank you in advance for any help.
I realized that after recompiling, webViewDidFinishLoad was called after the html was updated, and that I could then scroll to the correct position.
For the self delegation I used in viewDidLoad, it looks so:
webview.delegate = self;
Before updating the html in the webView I calculate the scrolling position with the code:
scrollPosition = [[wbView stringByEvaluatingJavaScriptFromString: #"scrollY"] intValue];
Then, for moving back to this position once the webView has an updated html, I then use:
-(void) webViewDidFinishLoad:(UIWebView *)wbView {
[wbView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:#"window.scrollTo(0, %d);",scrollPosition]];
}
I found part of the solution through the post below, with the answer of Pawel:
How can I scroll programmatically to the bottom in a uiwebview
Does any one have an idea how to access the UIScroller class , which is the default subview of UIWebView ?
I want to handle the touches, zooming , panning and scrolling features inside the webview .
Thanks..
I know this thread is old but if anyone comes across it there's a new way.
As of iOS 5 UIWebView now has a property called scrollView which you can't replace but you can set the properties of it. Most people just want to disable zooming/bouncing/scrolling all together which can be done by setting the properties of the scrollView for example if webview is a UIWebView:
webview.scrollView.bounces = NO; //Disables webview from bouncing
webview.scrollView.minimumZoomScale = webview.scrollView.maximumZoomScale = 1.0; //Forces zoom to be at 1 (can be whatever you fancy) and disables zooming
webview.scrollView.bouncesZoom = NO; //Disables bouncing when zooming exceeds minimum or maximum zoom
I suppose you could set the delegate for the scrollView if you want more control, though to be on the safe side you might want to store the original delegate and call its methods appropriately in your custom delegate.
Handling the touches would be more difficult since you can't replace the scrollView to provide your own handlers. Best you could do is add some gesture recognizers as part of the UIView and try to handle them there, but I think UIWebView will still receive the events. Alternatively in iOS 5 they let you access the gesture recognizers directly on UIScrollView.
You can find this by going like this:
[webview objectAtIndex:0]
That should be where it is. If not, put this in your code somewhere and run the program to search for the index of the UIScroller, and replace the 0 above with that index:-
for (UIView *subview in [webView subviews]){
NSLog(#"subviews of webView : %#", [[subview class] description]);
}