Titanium.Platform.openURL not working with a variable
I've got the following in an attempt open a url in Safari.
//open in safari button
var safariBtn = Titanium.UI.createButton({
title:"open in safari"
});
safariBtn.addEventListener('click', function() {
Titanium.Platform.openURL(rowLink);
});
The button lives in the rightNavButton placement in the window bar. The variable 'rowLink' is a legit, working variable as it is also being used to load the webview below. Right now the button does nothing, but if I were to replace the variable with a string like "http://www.google.com" it functions just fine. I'm banging my head against a wall trying to figure out why a variable that works with a webview refuses to work as the value in openURL. Anyone see anything missing?
5 Answers
-
rowLink needs to be defined out of the click function to be available
try this:var rowLink = "http://google.com"; safariBtn.addEventListener('click', function() { Titanium.Platform.openURL(rowLink); });
-
Yeah the variable is defined outside the click function. The value of rowLink is being extracted from twitter via a regex operation. I changed that regex value to a simple string and the whole thing worked so it looks like that regex is doing something that the webview.url method doesn't much care about but openURL does. I've looked at the str and it seems ok to me so now I guess I have to deal with regex, which isn't going to be fun :-(
-
put a
Titanium.API.info( result_of_regex )
to see what is there
-
rowLink must contain a valid URL.
-
Titanium.API.info( result_of_regex ) comes back with what looks like a properly formatted string and further upstream in this app I'm already removing any null values. Even weirder if I cut and paste that value from the log window and use it as a static variable (instead of my google.com example) it again works. I'll be honest, regex gives me a headache so I'm using the following that I found online:
//link extractor function getLinks(text) { var exp = /(b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|])/ig; return text.match(exp); }
If anyone can see a flaw there that might be causing this issue I'd be super appreciative. If i can't sort this out I'm just going to have to figure out another way of letting the user open safari.