UIApplication OpenUrl double-escaping my URLs - iphone

Basically, i'm trying to program a "tweet this" button from inside my application. Depending on their spot in the application, they can click the tweet button and it'll shoot them out to Safari with a tweet message that changes depending on where they are.
In order to create URLs, I have to escape the query string that I want to put in the NSUrl object. So, I do this:
NSString* escapedTweet = [#"Some String With Spaces" stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
After concatenating the base, my url comes out "http://www.twitter.com/home/?status=Some&20String%20With%20Spaces" - looked at it in the debugger and this is definitely the value (as expected). Now, I create my URL and launch safari:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escapedUrlString]];
But here's where the issue comes up: OpenUrl appears to be escaping my percent signs, so the actual URL that Safari goes to is "http://www.twitter.com/home/?status=Some%2520String%2520With%2520Spaces", which is obviously a problem since twitter creates the status message as "Some%20String%20With%20Spaces".
NSUrl will NOT allow me to create a URL with spaces in it, so i'm completely lost as to how to get my URLs to just include a %20. Has anyone else run into this issue or found a workaround?
I'm running on an iPad with an up to date OS, not sure if it's the same issue on the iPhone.
Edit: In a nutshell, how do I get openUrl to open http://www.twitter.com/home/?status=Some%20Url%20With%20Spaces without escaping my percent signs and creating a URL like http://www.twitter.com/home/?status=Some%2520Url%2520With%2520Spaces?

I am assuming that escapedUrlString is declared as NSURL *escapedUrlString = [NSURL URLWithString:escapedTweet];. Then your problem probably lies in openURL:[NSURL URLWithString:escapedUrlString]];, because you’re taking an URL, passing it into another URL and then opening it. Fix by passing in escapedURLString (which should be named ‘escapedURL’) instead of URLWithString:….

Related

How to make link open "Maps" window when clicked

I would like to have a link in a web page in such a way that when the link is clicked it opens the standard "Maps" view in iPhone.
If such a thing is possible, what tag format do I need to use with the link?
It's pretty simple; you don't even need to use a specific scheme identifier. Any Google Maps URL will be opened with the Maps app automatically, as long as all the parameters are supported.
So links like these:
http://maps.google.com/maps?q=cupertino
http://maps.google.com/maps?daddr=San+Francisco,+CA&saddr=cupertino
Would automatically be opened in Maps. To find out more about what works and what doesn't see the Map Links page from the Apple URL Scheme Reference.
I used this code which works passing longitude lattitude:
NSString *url = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%#",cur_lat, cur_lon,[loc stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"current %f %f",cur_lat,cur_lon);
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:url]];

Opening tel: links from UIWebView

I've searched and searched, but I can't seem to find a fix for this problem.
For some reason, I can not get 'tel:' links to work in a UIWebView. When the links are clicked, the message "The URL can't be shown" appears. Clicking on the same link in Safari works perfectly and dials the number.
This problem started with iOS 5. These links worked perfectly in iOS 4.2 and 4.3.
I'm not sure what other information might be useful, so please let me know if I need to clarify.
Thanks!
EDIT:
Here is the actual code in use...
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = request.URL;
if ([url.scheme isEqualToString:#"tel"]) {
return YES;
}
if (![url.scheme isEqualToString:#"http"] && ![url.scheme isEqualToString:#"https"]) {
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
return NO; // Let OS handle this url
}
}
[NSThread detachNewThreadSelector:#selector(startBusy) toTarget:self withObject:nil];
return YES;
}
If I take out the first if statement, the number gets dialed immediately with no confirmation. I'd really like it to function the way it used to by giving an alert, giving you the option to hit either 'Call' or 'Cancel' before dialing the number.
If launching as an HTML link, the tel URL scheme will be opened if they appear as:
1-408-555-5555
If you are launching from a native URL string (meaning you coded this in Objective-C and are not serving it via a WebView), your URL string should look like this:
tel:1-408-555-5555
Note: This only works with iOS devices that have the Phone app installed (that means iPhone only). iPad & iPod Touch devices will display a warning message.
Note 2: Ensure the phone numbers you are passing do not contain spaces or other special characters (such as * and #).
Code Feedback
Based on your code, things are a bit clearer now. You comment about how nothing happens when you leave the first if statement in the shouldStartLoadWithRequest method (where you return YES). This is exactly the behavior you should see because your app is not the Phone app. Only the Phone app can handle the tel: URL scheme. By returning YES, you are telling the OS that your app will handle the phone call, but it cannot. You get the call when that conditional is removed because the next block, which checks if ([[UIApplication sharedApplication] canOpenURL:url]) allows the sharedApplication (which, in this case, is the Phone app) to launch the call.
How Things Work & What You Want
The OS is not going to handle showing the Call/Cancel alert dialog for you. That is up to you. It shows up in Safari because the Safari app's shouldStartLoadWithRequest method undoubtedly responds to the tel: scheme by showing a UIAlertView. Your conditional for if ([url.scheme isEqualToString:#"tel"]) should, when YES, trigger a UIAlertView with a Call and Cancel button. On Call, you will tell the sharedApplication to openURL; on Cancel, you will not issue the call & you will also want to return NO so your app does not attempt to loadWithRequest.
Self-Correcting Edit
To be fair about errors in my own thought process, I'm leaving my responses above.
I believe the Call/Cancel dialog is, in fact, a feature of the OS. Apologies for the inaccuracy.
I'd also erroneously glanced over your code's passing off URL handling to sharedApplication only occurring when the scheme was http or https.
After another look at the code, I wonder if, by any chance you have debug options on in Safari? I believe this prevents the alert from popping up. Also--just to double-check the obvious--you aren't trying this inside the simulator, correct? What happens if you remove the conditional check for http/https and just use the canOpenURL check?
However, aside from the error in my comments on the conditional & dialog itself, you still should not be returning YES. To make a phone call, you should only be able to pull that off by passing it to sharedApplication:openURL and ensuring you return NO because your app is not the Phone app. The only reason you'd want to return YES in this method is if your app is going to handle a tel: link in a special way that doesn't involve sending it to the Phone app.
If you created the UIWebView in a .xib, select the UIWebView and check its attributes in the Attribute Inspector. The first heading should be 'Web View', and under that it provides a list of checkboxes marked 'Detection'. Ensure that 'Phone Numbers' is checked.

how to place url string

hi i am new to iphone. what i need is i have to place a url as a string eg:http://stackoverflow.com by selecting the string it self open in a web brosewr how can i done this. post some code or link thank u.
Add a textView control onto your NIB file. Put whatever content (you mention 5 lines of text) you want in here. In the properties for this control, under the "Detection" section, click "Links". Then, any URL links in this text will be automatically highlighted in blue, and when your users tap on it they will be taken to that URL in the browser.
Your question could have been a little clearer - I only got what you were after because of your previous comment on puckipedia's answer...
To open A url use (in Objective-C)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:URLHere]];

Mapping data from RSS feed with no latitude/longitude using MapKit

Having follow the tutorial at http://mithin.in/2009/06/22/using-iphone-sdk-mapkit-framework-a-tutorial/ to integrate the MapKit into my application. However, I want to be able to display an annotation for a dynamic "incident" on the map. Here's where I'm having problems.
Basically my apps an RSS reader, and it downloads everything and saves each item of the feed (as in Story 1, Story 2) as currentItem. The data I want to grab and use to map the annotation can be found under currentItem.title - but I can't seem to get it to work in this tutorial's code or find the right place to put it.
The RSS feed I'm using doesn't have any latitude/longitude information either - making it even harder.
Any help would be great - I've been working on this for a couple of days now with still no luck.
UPDATE: I'm even considering launching the Google Maps application instead showing the annotation. But this in itself is raising issues. Below is the code I'm using but its throwing errors everywhere.
NSString *title = currentItem.title;
int zoom = 13;
NSString *stringURL = [NSString stringWithFormat:#"http://maps.google.com/maps?q=%##%1.6f,%1.6f&z=%d", title, zoom];
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
In the end I just mapped the title using reverse lookup, and added the country afterwards so results would only be in the country where the address is. Not ideal, but it seems to work OK.

problem using tel: URL to initiate a call

I'm trying to initiate a call from within an iPhone app.
This related code works and opens Safari as expected:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.apple.com"]];
But, when I replace the http URL with a tel URL the resulting code does not invoke the phone app:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:3035551212"]];
No exceptions or alerts are generated (in the simulator or on a device).
Any idea what the problem might be with my invocation?
Thanks.
The iphone will dial a number using either of the formats listed below. But, it will do nothing if you are in the simulator. It took me 30 minutes of banging my head to figure this out.
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel://15415551234"]];
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel:15415551234"]];
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"tel:1-541-555-1234"]];
Link for Apple documentation on the tel: url scheme
Link for openURL documentation
(and )are actually no problem if you use stringByAddingPercentEscapesUsingEncoding as suggested by samiq. Same goes for +, / and spaces. It's a URL, so escaping seems natural.
If I'm guessing right, Apple's regular phone number recognition will be used in the tel: scheme handler, if you escape correctly.
(missing reputation to make it a comment)
As #bentford said one might get miscarried because the simulator does show an alert when you try to click on a phone on the contacts app, this is just an alert that gets generated because the app checks whether or not the tel: protocol is supported on the device or not.
Adding to what he writes you might want to also add support to escape any special characters or spaces as in:
NSString *phoneStr = [NSString stringWithFormat:#"tel:%#",[self.contactDetails objectForKey:#"phone"]];
NSString *escaped = [phoneStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:escaped]];
Hope it helps.
Cheers.
If your phone number has ( or ) in it, the Phone app will not launch.
Haven't had any problems with it using tel:{phone-number} and invoking it the same way you are. Only works on the iPhone device, though.
One thing I did have to do was strip out extraneous characters (like all parentheses, spaces, dashes and dots) out of the phone string. Some of the examples above (but not the original post) have dashes. That might be the problem.
I found that the issue was due to characters that where not allowed for tel:// URI - the following code solved my problem:
NSMutableCharacterSet *charSet = [NSMutableCharacterSet new];
[charSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]];
[charSet formUnionWithCharacterSet:[NSCharacterSet punctuationCharacterSet]];
[charSet formUnionWithCharacterSet:[NSCharacterSet symbolCharacterSet]];
NSArray *arrayWithNumbers = [str componentsSeparatedByCharactersInSet:charSet];
NSString *numberStr = [arrayWithNumbers componentsJoinedByString:#""];
I just ran into this when trying to add a "Call" button to a UIAlertView. I had the following code to handle the call:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != 0)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:1-602-555-1212"]];
}
}
It wouldn't open anything, just like you. I even tried a regular http URL. It turned out I had forgotten to set the delegate to self. That's probably your problem also.
some additional info about making phone calls via url scheme (as I think someone may find it useful)
How to use tel: with * (star, asterisk) or # (hash, pound) on iOs?
Here is an update on how to derive a phone number in 2018 (where iOS 11 and 12 are most common):
NSString *phoneNumberURLString = [#"tel://" stringByAppendingString:[result.phoneNumber
stringByAddingPercentEncodingWithAllowedCharacters:
[NSCharacterSet URLPathAllowedCharacterSet]]];
NSURL *phoneNumberURL = [NSURL URLWithString:phoneNumberURLString];
samiq's answer was what I used initially until I found that my app would crash due to a nil NSURL being created (and later being passed into a collection object) due to failing to properly escape a phone number formatted like so: (###) ###-####. The aforementioned code resolves this.
I got the same problem and at last I found the reason was that there was a space in the phone number (for better formatting). After removing the space it worked fine.
The URL should be tel://3035551212 and not tel:3035551212...
Add that // and it should work.