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.
Related
now im doing the push notification on iphone by using xcode. When i sending the message/notification to the user that contain URL for the user to open it, so the user will receive the notification and message that contained URL and open it in the webview in the apps. Is it possible to open that URL outside the apps by chrome or safari?
what are coding need to modified in order to connect that clicked URL to open the browser outside the apps?
this is the coding that make the webview is open when clicked on the link.
- (BOOL)webView:(UIWebView *)wv shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSURL *url = [request URL];
if ([[url scheme] isEqualToString:#"ua"]) {
if ((navigationType == UIWebViewNavigationTypeLinkClicked) || (navigationType == UIWebViewNavigationTypeOther)) {
[UAInboxMessage performJSDelegate:wv url:url];
return NO;
}
With the use of this code you can open a link to safari browser just pass your link instead of provided
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.daledietrich.com"]];
you can make static method.
+ (void)openBrowser:(NSString *)url {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
and then call it anywhere
[className openBrowser:#"http://rajneesh071.blogspot.in/2013/03/ios-sdk-open-other-app-from-our-app.html"];
If you have rich text in your url then you can make encoding with it.
NSError *err = nil;
NSString *resultStr=[[NSString alloc] initWithContentsOfFile:url encoding:NSUTF8StringEncoding error:&err];
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
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];
}
My app has a UIWebView that shows a fair amount of content. For some of that content I would like to exit the app, and launch Safari to handle the web content rather than doing it in my UIWebView. Is there an url format that will explicitly launch Safari rather than loading a page in the UIWebView?
Obviously I can't use http:// since that just opens the url in place. Can I use safari:// or something of the sort?
EDIT: Apologies, I wasn't clear originally. I am looking for a solution that involves changing urls on pages without making modifications to my client. Hoping for a native Safari launching pattern along the lines of tel:// for the phone.
Sure. Then just have the UIWebView delegate do:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSString * customScheme = #"safari";
NSURL * loadURL = [request URL]; //this is the url the user tapped
if ([[loadURL scheme] isEqual:customScheme]) {
//if the url starts with "safari://"
NSString * absoluteURL = [loadURL absoluteString];
//replace "safari" with "https"
absoluteURL = [absoluteURL stringByReplacingCharactersInRange:NSMakeRange(0,[customScheme length]) withString:#"https"];
NSURL * openURL = [NSURL URLWithString:absoluteURL];
//open the URL in MobileSafari
[[UIApplication sharedApplication] openURL:openURL];
//tell your UIWebView to ignore this request
return NO;
} else {
//this is not a safari:// url, so handle it normally
return YES;
}
}