How to open Maps app from my code to show directions? - iphone

HI.. I have to show directions between two coordinates. I'd like to open Maps app, passing the start and end coordinates from my code.
I don't want to open it in the Google maps, which opens in browser(Safari). I tried that method. That was working perfect.
NSString* urlStr = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", s_lat, s_long, d_lat, d_long];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:urlStr]];
But, I want to open iPhone Maps app. How can i do this? Is this possible? Any help will be appreciated.

Try it on a real device. I'm using the same code and it opens Safari at the simulator and iPhone Maps at the device.

Hey Simon the method you are using is
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:urlStr]];
will only open safari.So there is no chance that with open url you would open a diiferent thing.You can use the UIWebView for this and then load the webview with the url.I think that would be the simple thing like this
views=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
[views setBackgroundColor:[UIColor lightGrayColor]];
NSString* urlStr = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f", s_lat, s_long, d_lat, d_long];
NSURL *url=[NSURL URLWithString:urlStr];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
//[views loadHTMLString:googlePage baseURL:requestURL];
[views loadRequest:req];
self.view=views;
And if you even don't want to use this then then you can use the MapKit provided in your XCode Frameworks.Hope this would help you.

Related

iPhone 4 problems dialing from inside app

There is an iOS 5.0+ app, released in App Store, which displays employees profiles with their phone numbers. The users of the app can tap on the phone number and the number will be dialed on their iPhones.
The problem is that some users with iPhone 4 are reporting that it does NOT dial.
This is the code I am using to dial:
NSString *cleanedString = [[self.member.phone componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"0123456789-+()"] invertedSet]] componentsJoinedByString:#""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urlText = [NSURL URLWithString:[NSString stringWithFormat:#"tel://%#", escapedPhoneNumber]];
[[UIApplication sharedApplication] openURL:urlText];
Any ideas about this?
There shouldn't be any / in the tel: scheme:
NSURL *urlText = [NSURL URLWithString:[NSString stringWithFormat:#"tel:%#", escapedPhoneNumber]];
In the Apple URL Scheme Reference you can see some example for what is allowed in the tel: scheme.
If you use telpromt instead of tel this will return you to app after your call ends|
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"telprompt:1234567890"]];

Open PDF In iBooks

I've a a pdf placed in my Code files. I want it to open in iBooks, while researching I've found this :
NSString *stringURL = #"itms-books:";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
NSString *stringURL = #"itms-bookss:";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];
How can I use this or what other way i can open my PDF File in iBook?
Regards
Maybe too late but I created a small example where the method explained in andycodes used. I uploaded to Github and you can download it here:
https://github.com/cjimenezpacho/openpdfibooks
Basically is this piece of code in a IBAction and the required delegate methods:
NSURL *url = [[NSBundle mainBundle]
URLForResource: #"iPhoneintro" withExtension:#"pdf"];
self.docController = [UIDocumentInteractionController interactionControllerWithURL:url];
self.docController.delegate = self;
[self.docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
Links to Apple documentation:
Class:
https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocumentInteractionController_class/Reference/Reference.html
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:#"itms-bookss://%#",self.pathToFile]]];
this does open a local pdf to Ibooks needs 2 s's.
Edit: to be specific on this this will open the local PDF if it is in iBooks already locally on the device.
if you need to add it to iBooks so far as I know you need to use the UIDocumentInteractionController.
Take a look at this Tumblr post explaining how do achieve a similar result using UIDocumentInteractionControllers.
http://andycodes.tumblr.com/post/738375724/ios4-sdk-opening-pdfs-in-ibooks

Return to application after call is cancelled

I want to use telprompt:// scheme to return to my application after it makes a call. But I have a problem.
If remote person is busy two buttons are shown to me: Redial and Cancel. So if I press Cancel I won't return to my application. Is there any way to handle it?
P. S. Isn't telprompt:// in Private API?
Here You can Use the WebView and Load Request on it.
UIWebView *callWebview = [[UIWebView alloc] init];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:#"tel://%#", phoneNumber]
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];
I am Sure It would work with Charm.....
I think you need:
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:[NSString stringWithFormat:#"telprompt:%#",num]]];

Open a map URL with MapKit

I was wondering how can I open a google map URL with MapKit ?
this code exit the application and shows the location but I want shows on my mapView.
NSString *urlString = #"http://maps.google.com/maps?daddr=San+Francisco,+CA&saddr=cupertino";
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: urlString]];
MapKit supports Geolocation - so how about something like this:
CLLocation *sanFran = [CLLocation locationUsingForwardGeoLocation:#"San Francisco, Califronia"];
This returns a CLLocation which you can then show on your map view.
Have a look at the World Cities sample code (iOS dev membership required) which shows how to animate an MKMapView to coordinates.
Edited to add
There are ways to get the Lat-long with Google Tools. You can use these to create a CLLocation as well.
You can use UIWebview to load the contents of the url. Create a webView and initialise as follows:
NSString *urlString = #"http://maps.google.com/maps?daddr=San+Francisco,+CA&saddr=cupertino";
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString: urlString]];
[webView loadRequest:req];
Yeah you should add this webview as a subview to your main view.

Text as Hyperlink in Objective-C

I have a text label with the text set as Contact Us.. When user taps on this label it should launch safari to open the web page.My doubt is how to make Contact Us as hyperlink.Now I can't modify my code to include a UIWebView..Please help me guys..I am in final stages of my project..If possible please help me with sample code..thanks for all your time
The easiest way is to make the UILabel into a UIButton, style it (use Custom type to get rid of button look). Then connect to an Action that opens safari.
The action should do this:
NSURL *url = [[[ NSURL alloc ] initWithString: #"http://www.example.com" ] autorelease];
[[UIApplication sharedApplication] openURL:url];
for XOS use NSWorkspace instead:
- (IBAction)btnPressed:(id)sender {
NSURL *url = [[NSURL alloc] initWithString: #"https://www.google.com"];
[[NSWorkspace sharedWorkspace] openURL:url];
}