I develop app for IOS 6.
I want to run maps application and pass it start and destination so I can navigate user.
UIApplication *app = [UIApplication sharedApplication];
NSString *coordinates = [NSString stringWithFormat:#"http://maps.google.com/maps?daddr=%f,%f&saddr=%f,%f", ...];
[app openURL:[NSURL URLWithString: coordinates]];
I thought that this code will open google maps in browser on simulator, and maps app on device, but on the device it runs browser google map.
Am i doing something wrong?
If you're not aware, Apple no longer uses Google maps so you have to use their new URL scheme for the Apple maps.
(Note: if you're supporting iOS 5, then you should use both. The google maps scheme and apple maps)
Here is an example query http://maps.apple.com/maps?daddr=San+Francisco,+CA&saddr=cupertino
Here is the documentation for it: Apple Maps URL Schemes
Another option, if you have an MKPlacemark object:
// placemark is your MKPlacemark object
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark:placemark];
if([destination respondsToSelector:#selector(openInMapsWithLaunchOptions:)])
{
// Using iOS6 native maps app
[destination openInMapsWithLaunchOptions:#{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];
}
else
{
// Using iOS5 which has the Google Maps application
NSString *currentLocation = #"Current%20Location";
NSString *routeString = [NSString stringWithFormat:#"%#saddr=%#&daddr=%#", kMapsBaseUrl, currentLocation, address.mapAddress];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:routeString]];
}
This is what I use on iOS 8.
First I try to open the url #"comgooglemaps://", if it works this means that they installed the google maps app, so then I can open the app.
If it doesn't work then the app isn't there, just open google maps in Safari.
In both cases it passes the query q=London.
if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"comgooglemaps://"]]){ //open google maps app
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"comgooglemaps://?q=London"]];
}
else{ //open browser
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://maps.google.com/maps?q=London"]];
}
-(void)openAddressOnNativeMapApp{
NSString *addressOnMap = #"cupertino"; //place name
NSString* addr = [NSString stringWithFormat:#"http://maps.apple.com/?q=%#",addressOnMap];
NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:url];
}
For More Info visit Apple Doc for open native map App
Related
My aim is to open a map application from ios application with directions, I am able to open maps application but it is not showing directions, i have written the code as follows
NSString *mystr=[[NSString alloc] initWithFormat:#"http://maps.apple.com/maps?saddr=Current+Location&daddr=Newyork"];
NSURL *myurl=[[NSURL alloc] initWithString:mystr];
[[UIApplication sharedApplication] openURL:myurl];
Can any one please help me how figure out how to pass parameters to this url and any other?
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(self.location.latitude,self.location.longitude);
//create MKMapItem out of coordinates
MKPlacemark* placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
MKMapItem* destination = [[MKMapItem alloc] initWithPlacemark:placeMark];
if([destination respondsToSelector:#selector(openInMapsWithLaunchOptions:)])
{
//using iOS6 native maps app
if(_mode == 1)
{
[destination openInMapsWithLaunchOptions:#{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeWalking}];
}
if(_mode == 2)
{
[destination openInMapsWithLaunchOptions:#{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving}];
}
if(_mode == 3)
{
[destination openInMapsWithLaunchOptions:#{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeTransit}];
}
} else{
//using iOS 5 which has the Google Maps application
NSString* url = [NSString stringWithFormat: #"http://maps.google.com/maps?saddr=Current+Location&daddr=%f,%f", self.location.latitude, self.location.longitude];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
}
If you mean taking the user to the maps application based on two points, then you can do it like this:
Create an NSURL that looks like this:
NSURL *URL = [NSURL URLWithString:#"http://maps.google.com/maps?saddr=%f,%f&daddr=%f,%f"];
You plug in your starting address and destination (in lat. and long.) appropriately.
Tell your application to open the URL
[[UIApplication sharedApplication] openURL:URL];
It should take you to the maps application automatically!
I am showing apple maps with directions from start and end destination.I want it to open and start siri when user opens the map with the route.For some reason its opening the url successfully but not opening the siri to guide the user.
Code used for it is as shown below:
NSString* versionNum = [[UIDevice currentDevice] systemVersion];
NSString *nativeMapScheme = #"maps.apple.com";
if ([versionNum compare:#"6.0" options:NSNumericSearch] == NSOrderedAscending)
nativeMapScheme = #"maps.google.com";
NSString* url = [NSString stringWithFormat: #"http://%#/maps?daddr=%#&saddr=%f,%f",nativeMapScheme,[description stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],mylat, mylon];
NSLog(#"Location - %#",url);
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
I'm trying to open a mobile configuration file (mobileconfig) in safari to install it but nothing work.
I use URL Scheme:
NSURL *finalURL = [NSURL URLWithString:[NSString stringWithFormat:#"myAppURLScheme://%#",fileName]];
BOOL canOpen = [[UIApplication sharedApplication] openURL:finalURL];
if (canOpen) NSLog(#"can open");
else NSLog(#"can't open");
log --> can open
and i try to set all the path(the file is in the Documents folder) to the file instead fileName, nothing.
how can I do it. ?
Edit1: this application do the same(open safari to install configuration)
Edit2: I think that i have to search the way to send file(any) to safari, and safari will know what to do with it.
Authorize a background task
.h file :
UIBackgroundTaskIdentifier bgTask;
.m file :
In applicationDidEnterBackground add a new background task :
bgTask = [application beginBackgroundTaskWithExpirationHandler: ^{
dispatch_async(dispatch_get_main_queue(), ^{
[application endBackgroundTask:self->bgTask];
self->bgTask = UIBackgroundTaskInvalid;
});
}];
Add CocoaHTTPServer to your project
Run the server and open the .mobileconfig file :
RoutingHTTPServer *httpServer = [[RoutingHTTPServer alloc] init];
[httpServer setType:#"_http._tcp."];
[httpServer setPort:12345];
[httpServer setDefaultHeader:#"Content-Type" value:#"application/x-apple-aspen-config"];
[httpServer setDocumentRoot:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
if([httpServer start:nil])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://localhost:12345/myprofile.mobileconfig"]];
}
The mobile config file is inside your app's sandbox. Safari doesn't have access to it. The return value of [UIApplication openURL] only indicates if there was an application that understands that url scheme. It looks to me as if you're sending that url to yourself, assuming that you added myAppURLScheme as a uri handler to your info.plist file.
I think you can use data URI to encode and launch mobileconfig. (I don't have IOS device here, so I cannot test right now_
You can use http://dopiaza.org/tools/datauri/index.php to encode your profile (don't forget to add mime type: application/x-apple-aspen-config)
Then you can open:
[[UIApplication sharedApplication] openURL:dataURLGenerated];
quite had no luck either but I post this anyway if someone else can use this information. I tried opening the string via data: url which is supported by Mobile Safari, but not by openURL: – sadly.
NSString *urlHeader = #"data:application/x-apple-aspen-config;charset=utf-8,";
NSString *mobileConf = #"<?xmlversion=\"1.0\"encoding=\"UTF-8\"standalone=\"yes\"?>"
"<!DOCTYPEplistPUBLIC\"-//Apple//DTDPLIST1.0//EN\"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"
"<plistversion=\"1.0\"><dict><key>PayloadUUID</key><string>A0670934-C558-42E1-9E80-9B8E079E9AB2</string><key>PayloadDisplayName</key><string>EnableTethering</string><key>PayloadDescription</key><string>EnablesTethering</string><key>PayloadOrganization</key><string>de.iphone-notes</string><key>PayloadVersion</key><integer>1</integer><key>PayloadIdentifier</key><string>de.iphone-notes.etisalat</string><key>PayloadType</key><string>Configuration</string><key>PayloadContent</key><array><dict><key>PayloadUUID</key><string>C1A41907-0CD9-4DC9-BAF1-A04A73B7E296</string><key>PayloadDisplayName</key><string>AdvancedSettings</string><key>PayloadDescription</key><string>ProvidescustomizationofcarrierAccessPointName.</string><key>PayloadOrganization</key><string>de.sendowski</string><key>PayloadVersion</key><integer>1</integer><key>PayloadIdentifier</key><string>de.iphone-notes.etisalat.apn</string><key>PayloadContent</key><array><dict><key>DefaultsDomainName</key><string>com.apple.managedCarrier</string><key>DefaultsData</key><dict><key>apns</key><array><dict><key>apn</key><string>Etisalat.ae</string><key>username</key><string></string><key>password</key><string></string><key>type-mask</key><integer>-2</integer></dict></array></dict></dict></array><key>PayloadType</key><string>com.apple.apn.managed</string></dict></array></dict></plist>";
mobileConf = [mobileConf stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *finalURL = [NSURL URLWithString:[urlHeader stringByAppendingString:mobileConf]];
BOOL canOpen = [[UIApplication sharedApplication] openURL:finalURL];
if (canOpen) NSLog(#"can open");
else NSLog(#"can't open");
For testing you can prepend http:// before data: then it will at least open in Safari and you can delete the prefix to try it. Maybe some javascript injection to remove the prefix will work; I don't know.
i am working on signup feature. In this feature when the user create account successfully. i am asking him or her to activate his account.
i want to open the mail application of iphone if user say yes.
now my question is simple how to open mail application from my own application?
#define URLEMail #"mailto:sb#sw.com?subject=title&body=content"
NSString *url = [URLEMail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding ];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString: url]];
Try this out.
-(void)launchMailAppOnDevice
{
NSString *recipients = #"mailto:myemail#gmail.com?subject=subjecthere";
NSString *body = #"&body=bodyHere";
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
stringByAddingPercentEscapesUsingEncoding and openURL are deprecated.
Now use this:
#define URLEMail #"mailto:sb#sw.com?subject=title&body=content"
NSString * encodedString = [URLEMail stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
UIApplication *application = [UIApplication sharedApplication];
[application openURL:[NSURL URLWithString: encodedString] options:#{} completionHandler:nil];
Ahoy!
The long and short of it is; you can't.
You can create an email compose view for the purpose of sending emails (see MFMailComposeViewController), but you cannot open applications arbitrarily without a purpose.
See this previous post for clarification: Launch an app from within another (iPhone)
Really though, it's not much effort for the user to close your app and open Mail so I wouldn't worry too much about it anyway.
The page I want to open using twitter app:
https://twitter.com/#!/PAGE
To open twitter app I use the following code:
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"twitter://https://twitter.com/#!/PAGE"]];
[[UIApplication sharedApplication] openURL:urlApp];
But this code doesn't seem to work as expected, I got only twitter app launched without the page which i want to show.
You are looking for the following url:
twitter:///user?screen_name=PAGE
Note that Twitter is not installed on all devices. You should check the result of openURL method. If it fails, open the page in Safari with regular url.
The following code opens twitter page on twitter app if it is already installed, otherwise opens twitter on safari:
NSURL *twitterURL = [NSURL URLWithString:#"twitter://user?screen_name=username"];
if ([[UIApplication sharedApplication] canOpenURL:twitterURL])
[[UIApplication sharedApplication] openURL:twitterURL];
else
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.twitter.com/username"]];
Don't forget to replace 'username' with your name.
I know its quite a late response to this question and I agree that, Murat's answer is absolutely correct.
Simply add a check as follows:
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"twitter:///user?screen_name=PAGE]];
if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
[[UIApplication sharedApplication] openURL:urlApp];
}
I hope this helps someone. Cheers!! :)
This is the full code required in Swift. I am using Swift 4 but i believe it is the same for Swift 3.
let Username = "YOUR_USERNAME_HERE"
let appURL = NSURL(string: "twitter:///user?screen_name=\(Username)")!
let webURL = NSURL(string: "https://twitter.com/\(Username)")!
let application = UIApplication.shared
if application.canOpenURL(appURL as URL) {
application.open(appURL as URL)
} else {
// if Twitter app is not installed, open URL inside Safari
application.open(webURL as URL)
}
Don't forget to add the Info keys needed to use canOpenURL:
#Alexey: If you just want to know how to launch twitter from your application do this:
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"twitter://"]];
if ([[UIApplication sharedApplication] canOpenURL:urlApp]){
[[UIApplication sharedApplication] openURL:urlApp];
}else{
UIAlertView *appMissingAlertView = [[UIAlertView alloc] initWithTitle:#"Twitter App Not Installed!" message:#"Please install the Twitter App on your iPhone." delegate:nil cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[appMissingAlertView show];
[appMissingAlertView release];
}