I am trying to make my application open the apple maps application and have the address be pulled up. I tried this :
- (IBAction)openInMaps:(id)sender {
NSString *addressString = #"http://maps.apple.com/?q=1 Infinite Loop, Cupertino, CA";
NSURL *url = [NSURL URLWithString:addressString];
[[UIApplication sharedApplication] openURL:url];
}
and this :
- (IBAction)openInMaps:(id)sender {
NSString *addressString = #"http://maps.apple.com/?q=1_Infinite_Loop,_Cupertino,_CA";
NSURL *url = [NSURL URLWithString:addressString];
[[UIApplication sharedApplication] openURL:url];
}
But the button just acts like its hooked to nothing. But this does work :
- (IBAction)openInMaps:(id)sender {
NSString *addressString = #"http://maps.apple.com/?q=Cupertino,CA";
NSURL *url = [NSURL URLWithString:addressString];
[[UIApplication sharedApplication] openURL:url];
}
So, whenever their is a space it doesn't work. How can I open this address?
You need to properly escape the spaces in the URL:
NSString *addressString = #"http://maps.apple.com/?q=1%20Infinite%20Loop,%20Cupertino,%20CA";
Edit - it seems using + instead of %20 for the spaces solves the problem.
So to get it to work properly you must use this :
NSString *addressString = #"http://maps.apple.com/?q=1+Infinite+Loop,+Cupertino,+CA";
Swift 2 version that is formatted nicely, safely handles optionals, and won't crash your app:
let baseUrl: String = "http://maps.apple.com/?q="
let encodedName = "address".stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet()) ?? ""
let finalUrl = baseUrl + encodedName
if let url = NSURL(string: finalUrl) {
UIApplication.sharedApplication().openURL(url)
}
This is how I do it in Objective C...I always try the Waze Map first, because to be honest, it's awesome sauce, I added the PLIST file permissions at the end:
- (IBAction)getDirectionsAction:(id)sender {
NSURL *googleURL = [[NSURL alloc]
initWithString:[NSString stringWithFormat:#"comgooglemaps://?daddr=%#", #"44.294349,-70.326973"]];
NSURL *googleWebURL =
[[NSURL alloc] initWithString:[NSString stringWithFormat:#"http://www.maps.google.com/maps?daddr=%#",
#"44.294349,-70.326973"]];
NSURL *appleURL = [NSURL URLWithString:#"http://maps.apple.com/?daddr=311+East+Buckfield+Road+Buckfield+Maine"];
NSURL *wazeURL = [NSURL URLWithString:#"waze://?ll=44.294349,-70.326973&navigate=yes"];
// Lets try the Waze app first, cuz we like that one the most
if ([[UIApplication sharedApplication] canOpenURL:wazeURL]) {
[[UIApplication sharedApplication] openURL:wazeURL
options:#{}
completionHandler:^(BOOL success){
}];
return;
}
// Lets try the Apple Maps app second (great success rate)
if ([[UIApplication sharedApplication] canOpenURL:appleURL]) {
[[UIApplication sharedApplication] openURL:appleURL
options:#{}
completionHandler:^(BOOL success){
}];
return;
}
// If those 2 are unsuccessful, let's try the Google Maps app
if ([[UIApplication sharedApplication] canOpenURL:googleURL]) {
[[UIApplication sharedApplication] openURL:googleURL
options:#{}
completionHandler:^(BOOL success){
}];
return;
}
// Uh, oh...Well, then lets launch it from the web then.
else {
[[UIApplication sharedApplication] openURL:googleWebURL
options:#{}
completionHandler:^(BOOL success){
}];
}
}
FOR PLIST FILE
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>http://maps.apple.com</key>
<dict>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSIncludesSubdomains</key>
<true/>
</dict>
<key>http://maps.google.com/</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
</dict>
</dict>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>waze</string>
<string>comgooglemaps</string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
<string>For Use for directions</string>
Swift 3 version:
let baseUrl: String = "http://maps.apple.com/?q="
let encodedName = "yourAddress".addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let finalUrl = baseUrl + encodedName
if let url = URL(string: finalUrl)
{
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Swift 2
let baseUrl : String = "http://maps.google.com/?q="
let name : String = tableCellData[indexPath.row]
let encodedName = "address of yours"
name.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
let finalUrl = baseUrl + encodedName!
let url = NSURL(string: finalUrl)!
UIApplication.sharedApplication().openURL(url)
Related
I am trying to open the instagram app from my application and implemented the following code, but it does not do anything. I have instagram app installed on my iphone.
NSURL *instagramURL = [NSURL URLWithString:#"instagram://app"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
Update info.plist for Key LSApplicationQueriesSchemes
<key>LSApplicationQueriesSchemes</key>
<array>
<string>instagram</string>
</array>
Code:
NSURL *instagramURL = [NSURL URLWithString:#"instagram://location?id=1"];
if ([[UIApplication sharedApplication] canOpenURL:instagramURL]) {
[[UIApplication sharedApplication] openURL:instagramURL];
}
Refer instagram docs
In the following code:
// NSURL *videoURL1 = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"]];
NSURL *videoURL2 = [NSURL URLWithString:[NSString stringWithFormat:#"https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"]];
AVPlayer *playerV = [AVPlayer playerWithURL:videoURL];
AVPlayerViewController *playerViewController = [AVPlayerViewController new];
playerViewController.player = playerV;
[self presentViewController:playerViewController animated:YES completion:^{
[playerV play];
}];
VideoURL1 is working but videoURL2 is not working and the screen is like this:
I added keys and values in info.plist:
Reason behind not working you second url is that you are whitelisting only "http://www.ebookfrenzy.com" which does not support support secure connection. You should also whitelist "clips.vorwaerts-gmbh.de" in the same manner you have whitelisted "http://www.ebookfrenzy.com" . If you are going to access video from many site dynamically which does not support secure connection(https) then and the below line in your plist.
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
For more detail have a look on following thread-
How do I load an HTTP URL with App Transport Security enabled in iOS 9?
With Xcode7, I upgrade Facebook SDK to pod FBSDKShareKit (4.6.0). And I have added Facebook scheme to WhiteList as below.
reference: https://developers.facebook.com/docs/ios/ios9
<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fb-messenger-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
However, the following code only show iOS default social dialog on iOS9. The same code with the same binary on iOS8 can open Facebook app and show the Sharing Dialog properly.
FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.example.com"]];
content.contentDescription = #"Test";
[FBSDKShareDialog showFromViewController:self withContent:content delegate:nil];
I guess Facebook app is not found on iOS9 and then show the default social dialog. Even no error message showed.
Do I miss anything? Or, it's an iOS9 bug?
I'm guessing Facebook changed the behaviour because iOS 9 now pops up a dialog asking if you would like to "Open Facebook?" when doing app-switching. Even for FBSDKLoginManager, the app-switching (native) method seems to be less preferred than a modal UIWebView.
However, you can still force the share dialog to switch to the Facebook app (assuming you have your application plist setup as described in https://developers.facebook.com/docs/ios/ios9) by using this method:
FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"fbauth2://"]]){
dialog.mode = FBSDKShareDialogModeNative;
}
else {
dialog.mode = FBSDKShareDialogModeBrowser; //or FBSDKShareDialogModeAutomatic
}
dialog.shareContent = content;
dialog.delegate = self;
dialog.fromViewController = self;
[dialog show];
In iOS 9 below is the only solution that worked for me to detect if facebook app is installed in the device or not:
NSString *urlString = #"fbapi://";
NSURL *url1 = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url1]) {
[[UIApplication sharedApplication] openURL:url1];
}
else {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"itunes link for download app"]];
}
I'm running iOS 9b5.
In my app, if a device can make a phone call, I want to color the text blue so it looks tappable. If not, I leave it black.
In order to determine the device capabilities, I use:
[[UIApplcation sharedApplication] canOpenURL:#"telprompt://5555555555"]
As we all know, iOS 9 requires we whitelist any URL schemes we'll be using in our app as a privacy measure.
I have this in my Info.plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>telprompt</string>
</array>
No matter what I do, I still get canOpenURL: failed for URL: "telprompt://" - error: "(null)". I've tried tel:// and sms:// and I can't seem to avoid that syslog warning.
Does anybody know of a way to detect whether or not a device can make a phone call wtihout triggering these warnings?
What I discovered so far is, that if the console logs -canOpenURL: failed for URL: "xxx://" - error: "(null)", it actually works. As soon as there is any other error than null, it may not work. If the error is "This app is not allowed to query for scheme xxx", then you have to add this scheme to your app's .plist:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>xxx</string>
</array>
Strange behavior that the console output looks like an error although there is none, indeed.
I think you might need to try this on an actual device, or just try it again. I just got this working on my iPhone 5, it looks like you don't even need to add it to the LSApplicationQueriesSchemes. If the app is built with Xcode 7 Beta 6 and you use canOpenURL or openURL like below it seems to work just fine on device.
[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"tel:555-555-5555"]]
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:555-555-5555"]]
On the iOS sim I still get the error:
LaunchServices: ERROR: There is no registered handler for URL scheme tel
-canOpenURL: failed for URL: "tel:555-555-5555" - error: "This app is not allowed to query for scheme tel"
I got the same error in IOS9 devices. So I have used below code snippet to avoid this error.
NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
As iOS9 deprecates stringByAddingPercentEscapesUsingEncoding, the following can be used to clean the telprompt: URL.
NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
//NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *phoneURLString = [NSString stringWithFormat:#"telprompt:%#", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
In iOS9 I'm using this code and it works:
NSString *assistanceNumber = [[NSUserDefaults standardUserDefaults] objectForKey:#"AssistanceCallMISDN"];
assistanceNumber= [[assistanceNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
assistanceNumber = [assistanceNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *phoneUrl = [NSURL URLWithString:[#"telprompt://" stringByAppendingString:assistanceNumber]];
NSURL *phoneFallbackUrl = [NSURL URLWithString:[#"tel://" stringByAppendingString:assistanceNumber]];
if ([UIApplication.sharedApplication canOpenURL:phoneUrl]) {
[UIApplication.sharedApplication openURL:phoneUrl];
} else if ([UIApplication.sharedApplication canOpenURL:phoneFallbackUrl]) {
[UIApplication.sharedApplication openURL:phoneFallbackUrl];
} else
{
[[[UIAlertView alloc] initWithTitle:#"" message:[NSString stringWithFormat:#"No se ha podido realizar la llamada a través de la aplicación. Puede llamar usted al %#", assistanceNumber] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] show];
[_viewEmergency setHidden:YES];
}
My Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>telprompt</string>
<string>tel</string>
</array>
Try running this on a real device instead of simulator. No need to add LSApplicationQueriesSchemes for the tel scheme.
try this one:
NSString *phone_number = [[yourPhoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"telprompt://%#", phone_number]]];
I am working on a navigation app which launches GOOGLE MAP from my native app when user click on any of the pins displayed on the apple map. The location data was pulled from GOOGLE. So I used this code for block showing a callout accessory and let user get to the GOOGLE MAP
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
[self.navigationController pushViewController:[[UIViewController alloc] init] animated:YES];
NSString* addr = [NSString stringWithFormat:#"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr", view.annotation.coordinate.latitude,view.annotation.coordinate.longitude];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
Then I found out there is a x-callback url which can put a back button in the google map and let the user go back to my native app. but anyone know how to use these lines of codes? i have registered my app in the properties list but i have no idea what to do the next. the code from google is like this:
comgooglemaps-x-callback://?center=40.765819,-73.975866&zoom=14
&x-success=sourceapp://?resume=true
&x-source=SourceApp
Can anyone please tell me where shall i put these codes? and How to use that in my app?
If you scroll down in the Google documentation they give example code on how to use this in iOS.
Code provided:
NSURL *testURL = [NSURL URLWithString:#"comgooglemaps-x-callback://"];
if ([[UIApplication sharedApplication] canOpenURL:testURL])
{
NSString *directionsRequest = #"comgooglemaps-x-callback://" +
#"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York" +
#"&x-success=sourceapp://?resume=true&x-source=AirApp";
NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
[[UIApplication sharedApplication] openURL:directionsURL];
}
else
{
NSLog(#"Can't use comgooglemaps-x-callback:// on this device.");
}
You would change everthing after daddr= with your address. Also change AirApp to the name of your application.
you should use below code :
NSURL *testURL = [NSURL URLWithString:#"comgooglemaps-x-callback://"];
if ([[UIApplication sharedApplication] canOpenURL:testURL])
{
NSMutableString *directionsRequest = [NSMutableString stringWithString:#"comgooglemaps-x-callback://"];
[directionsRequest appendFormat:#"?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York"];
[directionsRequest appendFormat:#"&x-success=CouriorMap://?resume=true&x-source=AirApp"];
NSURL *directionsURL = [NSURL URLWithString:directionsRequest];
[[UIApplication sharedApplication] openURL:directionsURL];
}
else
{
NSLog(#"Can't use comgooglemaps-x-callback:// on this device.");
}
To use the google map call back function:
Add the following content to info.plist
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.example.GoogleMapTest</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myapp</string>
</array>
</dict>
</array>
</dict>
</plist>
com.example.GoogleMapTest is you app bundle id.
myapp is the url scheme just like google map's comgooglemaps://.
Call the function anywhere you wish:
- (void)useGoogleMapApp {
if ([[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:#"comgooglemaps://"]]) {
NSString *url = #"comgooglemaps-x-callback://?saddr=1.294094,103.853722&daddr=1.244310,103.833386&directionsmode=transit&x-success=myapp://?resume=true&x-source=MyAppName";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
} else {
NSLog(#"Can't use comgooglemaps://");
}
}