[TIMOB-25842] iOS: Cannot remove Ti.App.iOS.handleurl event-listener
GitHub Issue | n/a |
---|---|
Type | Bug |
Priority | Critical |
Status | Closed |
Resolution | Fixed |
Resolution Date | 2018-05-22T15:53:50.000+0000 |
Affected Version/s | Release 6.3.0, Release 7.0.2 |
Fix Version/s | Release 7.3.0 |
Components | iOS |
Labels | 6.3.0.GA, ios |
Reporter | Donald Anderson |
Assignee | Hans Knöchel |
Created | 2018-03-06T13:23:30.000+0000 |
Updated | 2018-06-12T17:41:19.000+0000 |
Description
In my app Titanium.App.iOS.addEventListener("handleurl", iosHandleUrl) can successfully register an event listener for the handle url event. Titanium.App.iOS.addEventListener("handleurl", iosHandleUrl) fails to remove the event listener resulting in the iosHandleUrl function being called multiple times in my app, when redirected back to the app from the browser.
To reproduce this I've create a sample project with the following code in index.js
function iosHandleUrl(e) {
console.log("redirected to app from URL");
}
Ti.App.iOS.addEventListener("handleurl",iosHandleUrl);
Ti.App.iOS.removeEventListener("handleurl",iosHandleUrl);
Ti.App.iOS.addEventListener("handleurl",iosHandleUrl);
Ti.App.iOS.removeEventListener("handleurl",iosHandleUrl);
Ti.App.iOS.addEventListener("handleurl",iosHandleUrl);
Ti.UI.createWindow({
backgroundColor: '#fff'
}).open();
And added the following to the plist in tiapp.xml.
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.ideagen.handleurltest</string>
<key>CFBundleURLSchemes</key>
<array>
<string>handleurltest</string>
</array>
</dict>
</array>
When I launch the app then open a browser and open handleurltest:// the expected behaviour is that "redirected to app from URL" should be logged once but the actual behaviour is that it's logged three times.
The app was built against version 6.3.0.GA of the SDK.
This is a valid bug, lets fix this!
PR: https://github.com/appcelerator/titanium_mobile/pull/9910 Test-Case: See above
We need to re-think the app-module architecture, as this is also happening for all other Ti.App.iOS.* events (events that are not linked to an instance but a toplevel namespace), like
shortcutitemclick
. In my opinion, we should only fire one event per unique callback, so thatWould still only trigger once, because the event listener is already added to the "handleURL" method. Is that thought correct? [~donald.anderson] Feedback welcome! If thats the case, I have a fix ready -
notification
,localnotificationaction
andremotenotificationaction
do it the same way btw. Full test:I am using the handleurl event as part of a login flow. I had an issue with repeated logins if the user had navigated away from the login window at any point. I'm debouncing the event listener as a workaround. It sounds like the fix described above would fix my issue. As an aside though, after looking at the test cases in the previous comment i'm a bit uncertain about expected behaviour around around events when a listener is added more that once. For example If I add the following to my test app
"orientationchange" is only logged once each time the device changes orientation. But if I add
then "genericEvent" is logged twice when the app is opened. If a function is repeatedly registered as a listener to an event, when the event fires should the function be run once only or multiple times or does it depend on the event type?
FR Passed, Waiting on Jenkins build to merge.
Closing ticket. Fix can be seen in SDK Version: 7.3.0.v20180607210411 Test and other information can be found at: https://github.com/appcelerator/titanium_mobile/pull/9910