IOS Memory buildup when changing IFRAME src with angular/Embedding youtube in cordova? - ios

I'm afraid im having the same probem as this unasnwered question.
I have a blank IFRAME In my cordova ionic IOS App for embedded YouTube videos.
I change the NG-SRC="" of iframe by clicking Next Video. But upon testing it appears that the phone/cordova/webview is caching the old content of the iframes. I can only get through 20 videos or so before crashing out of memory.
I have tried using angular.element.remove() to remove the iframe as well as setting the iframe src to blank first, and neither seemed to affect how much memory is in use, per Xcode. I've also tried the cordova plugin ClearCache and that didnt clear any memory either.
Please help! Is there a better way to embed youtube in a cordova app?
I have spent weeks working on this all to have it crashing down around me (no pun intended)
My Video view is like:
<ion-view view-title="Random Video">
<iframe id="youtube" sandbox="allow-scripts allow-same-origin" ng-src="{{video.url | trustAsResourceUrl}}" frameborder="0" allowfullscreen></iframe>
<h4>{{video.title}}</h4>
<button ng-click="nextVideo()">
</ion-view>
My controller is like:
angular.module('starter.controllers')
.controller('VideoCtrl', function(VideoService) {
$scope.video = {};
$scope.nextVideo = function() {
$scope.video = null; //doesnt seem to help
//$scope.$destroy(); //doesn't help
//angular.element(document.querySelector( '#youtube' )).attr("src", " ");
//angular.element(document.querySelector( '#youtube' )).remove();
//neither of the above 2 remove any memory
VideoService.getVideo().then(function(response){
$scope.video = response.data;
});
}
$scope.nextVideo();
});
Note, when I load my app onto a website instead, and load in chrome, I can cycle through videos without seeing the memory usage go up (looking at taskmgr.exe at least)

It might seem like setting the iframe to an empty string should be enough, but for some browsers and some situations it isn't. It might be necessary to recursively delete event listeners and elements one by one. Maybe surprisingly, the recursive method (1) below is faster than just setting to an empty string (2):
1.Recursive
while (box.lastChild) {
box.removeChild(box.lastChild);
}
2. Setting empty string
myNode.innerHTML = '';
See https://stackoverflow.com/a/3955238/1158376 for reference.
Additionally, in the recursive approach, one might need to apply special treatment to some items, for example first remove event listeners, nullify functions (http://javascript.crockford.com/memory/leak.html), and use dedicated cleanup methods, like with jQuery (http://javascript.info/tutorial/memory-leaks).
Another strategy you could try is to load a new web page with a fresh iframe for every video you play. Loading a new page should enable the browser to release the previously claimed memory.

Related

Print styles overriding screen styles after AirPrint in iOS webview

I am working on a hybrid HTML5/iOS app that uses the Safari Webview. We are using AirPrint to allow the user print the contents of the webview. The problem I am having is that after the print dialog is opened, the print styles are taking affect on the screen, and even after printing is complete or canceled do not go away. This does not happen in our Windows or Android versions of the app, which use CEF and Android System Webview respectively. Print styles in those versions of the application are only applied to the print out, as expected.
Anyone have any experience using AirPrint with Safari Webview that could shed some light on a solution? I have considered just adding/removing the link tag containing the CSS with javascript before and after printing, but that feels hacky, and doesn't answer the curious question of why print styles are being applied to the screen.
Any help appreciated! Sorry there is no real way to attach code to this!
Yes, this is indeed a not expected behaviour. However, we can try to solve this using JavaScript.
Theory: When the print is done, let's reload the stylesheets. The browser will paint the page again and hopefully using screen definitions.
Practice: As we don't have a JavaScript callback after printing, you could try reload your stylesheets using the window.onfocus event, as follows:
function updateStylesheets(){
var links = document.getElementsByTagName("link");
for (var x in links) {
var link = links[x];
if (link.getAttribute("type").indexOf("css") > -1) {
link.href = link.href + "?id=" + new Date().getMilliseconds();
}
}
}
window.onfocus = updateStylesheets;
In detail, it grabs all <link> tags and appends a random number after, forcing a reload on the stylesheets.
Please let me know if that worked, I'd be glad to help.

How does phoneGap (Cordova) work internally, iOS specific

I have started developing html applications for mutliple platforms. I recently heard about Cordova 2.0(PhoneGap) and ever since I have been curious to know how the bridge works.
After lot of code walking, i saw that the Exec.js is the code where call from JS -> Native happens
execXhr = execXhr || new XMLHttpRequest();
// Changeing this to a GET will make the XHR reach the URIProtocol on 4.2.
// For some reason it still doesn't work though...
execXhr.open('HEAD', "file:///!gap_exec", true);
execXhr.setRequestHeader('vc', cordova.iOSVCAddr);
if (shouldBundleCommandJson()) {
execXhr.setRequestHeader('cmds', nativecomm());
}
execXhr.send(null);
} else {
execIframe = execIframe || createExecIframe();
execIframe.src = "gap://ready";
But want to understand how that works, what is the concept here, what does file:///!gap_exec or gap://ready do? and how does the call propgate to the lower layers (native code layers)
thanks a bunch in advance.
The trick is easy:
There is a webview. This displays your app. The webview will handle all navigation events.
If the browser navigates to:
file:///!gap_exec
or
gap://
the webview will cancel the navigation. Everything behind these strings is re-used as an identifier, to get the concrete plugin/plugin-method and parameter:
pseudo-url example:
gap://echoplugin/echothistext?Hello World
This will cause phonegap to look for an echoplugin and call the echothistext method to send the text "Hello World" to the (native) plugin.
update
The way back from native to javascript is (or may be) loading a javascript: url into the webview.
The concrete implementation is a little bit more complex, because the javascript has to send a callback-id to native code. There could be more than one native call are running at the same time. But in fact this is no magic at all. Just a number to get the correct JSON to the right javascript-callback.
There are different ways to communicate between the platform and javascript. For Android there are three or four different bridges.
I am trying to figure this out in more detail, too. Basically there are 2 Methods on the iOS side that can help ...
- webView:shouldStartLoadWithRequest:navigationType: and
- stringByEvaluatingJavaScriptFromString:script
From the sources it seems cordova sends a "READY" message using webView:shouldStartLoadWithRequest:... and then picks up results with the second message, but I am not sure.
Cordova Sources iOSExec
There is much to learn there.

Play multiple html5 video on single page (not simultaneously) using backbone.js and jw player

I am using backbone.js to replace content on a page. After backbone places the new content I call a JW Player script to create a video (Flash normal web, HTML5 video tag on iOS).
When I play one video, stop that video, use backbone to replace the video, click to play the new video... the new video just has a spinner and won't play. (this only happens in iOS devices)
Has anyone had this issue that knows what might be happening and how to fix it?
This content is getting updated via backbone.
<div class="media-wrapper">
<div class="media-holder"><div id="media"></div></div>
</div>
This function is being called after backbone and creates the video on the div above.
function create_video(url,w,h,thumb)
{
jwplayer('media').setup({
flashplayer: '/api/js/jwp/player.swf',
skin: '/api/js/jwp/video.zip',
wmode: 'opaque',
image: thumb,
file: url,
height: h,
width: w
});
}
I am pretty sure it is not JW Player, but something to do with iOS and how it handles html5 videos.
This is a known bug[1] with using the same div and calling setup multiple times. The forum post linked to from the bug has the simple workaround of incrementing your div id each time you call setup.
[1] http://developer.longtailvideo.com/trac/ticket/1615

Backbone on BlackBerry OS5 - is it possible?

I'm developing a mobile application using Backbone, jQueryMobile and Phonegap. The app works great on Android, iOS and BB >= 6, but on BB5 as expected there are countless issues coming up.
I'm now facing problems with Backbone itself. I'm debugging it and looks like the problem is in the routes definition. The application crashes on start time due to something related to it (still investigating, but debugging is painful for BB5...).
Also, I read that BB5 won't play nice with hash listening, which Backbone relies on to do the navigation, so I am wondering if somebody has been able to create a backbone app on OS5, or is it simply not possible?
I'm updating this question just in case someone faces the same issue:
Short story: it's not possible to run Backbone on OS5. I debugged into backbone and some instructions with regular expressions were causing a crash. Even if these are fixed in the future, we determined that the js support was simply not good enough and finally discarded the OS5 version.
It is probably not worth it in most cases but this is doable.
I managed to get an app running after quite a bit of work - the javascript support is really not great in OS 5.0 and debugging is very very slow as suggested in bfcapell's answer.
To get backbone to work you need to comment out the code that uses the hashchange event to handle url changes (this is assuming that the router is being used). THere is a fallback in backbone which uses setinterval to poll for changes.
// Depending on whether we're using pushState or hashes, and whether
// 'onhashchange' is supported, determine how we check the URL state.
/*if (this._hasPushState)
{
alert('pushstate');
$(window).bind('popstate', this.checkUrl);
} else if (this._wantsHashChange && ('onhashchange' in window) && !oldIE)
{
alert('hashchange');
$(window).bind('hashchange', this.checkUrl);
} else if (this._wantsHashChange)
{*/
this._checkUrlInterval = setInterval(this.checkUrl, this.interval);
//}
The foreach method in underscore also needs to be modified to not use the native foreach method. This is needed for collections to be rendered correctly.
var each = _.each = _.forEach = function (obj, iterator, context)
{
if (obj == null) return;
/*if (nativeForEach && obj.forEach === nativeForEach)
{
obj.forEach(iterator, context);
}
else*/
if (obj.length === +obj.length)
The above should get at least backbone mostly working. (I say mostly because I have a completely working app but I suspect to find a couple more OS5 specific issues in time).

UIWebView cache in iOS

I'm hosting UIWebView in my app. it looks like UIWebView caches images and data itself.
I want to clear its cache on app startup.
Cleaning up Safari's cache doesn't help. the only way I found to flush UIWebView's cache is to power off my iPhone and turn it again. Closing the app doesn't help either.
Apple documentation says nothing about it...or I'm missing something. Just in case, app is created with monotouch.
If you want to obliterate all cached responses, something like this looks like the way to go:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
There is a view cache that shows a bitmap of the last page used (like we see inside Safari) but that does not looks like what you're seeing (since it requires to reboot the device).
I've not noticed this behaviour before (never looked for it either ;-) but the following answer looks promising.
FWIW this is not something that would be specific to MonoTouch.
Ive tried all the suggestions on stack overflow and none of them work. The only way I got it to work and feel that its a reliable solution is to create new html files on a temp directory (with a different directory name - a Guid works best) every time, and copy all the related images, scripts, css, every time to that temp directory.
Then open it using an NSUrlRequest object
string tempdir = Path.Combine(UIController.Common.DataFolder,System.Guid.NewGuid().ToString ());
Directory.CreateDirectory (tempdir);
//-- create your html on the tempdirectory here
//-- copy all the images, and all the css, and js files
UIWebView wv = new UIWebView(new RectangleF(30,30,480,680));
NSUrlRequest req = new NSUrlRequest(new NSUrl (Path.Combine (tempdir,"default.html"), false),NSUrlRequestCachePolicy.ReloadRevalidatingCacheData,10);
wv.LoadFinished += delegate(object sender1, EventArgs e1)
{
//delete the tempdirectory
Directory.Delete(tempdir);
};

Resources