Reputation: 399
How can I open a calendar from Swift app (when pressing a button for example)? Or is there a way to embed a calendar in a view controller in the app? I want to avoid using external calendars programmed by others. Thanks!
Upvotes: 9
Views: 10237
Reputation: 15784
You can open the Calendar app by using the url scheme calshow://
:
Swift 3+
guard let url = URL(string: "calshow://") else { return }
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Swift 2 and below
UIApplication.sharedApplication().openURL(NSURL(string: "calshow://")!)
With EventKit, you can implement your self a calendar. You should read Calendar and Reminders Programming Guide from Apple site.
Upvotes: 14
Reputation: 1243
openURL Deprecated in iOS10
From Apple’s guide to What’s New in iOS in the section on UIKit:
The new UIApplication method openURL:options:completionHandler:, which is executed asynchronously and calls the specified completion handler on the main queue (this method replaces openURL:).
Swift 3
func open(scheme: String) {
if let url = URL(string: scheme) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:],
completionHandler: {
(success) in
print("Open \(scheme): \(success)")
})
} else {
let success = UIApplication.shared.openURL(url)
print("Open \(scheme): \(success)")
}
}
}
// Typical usage
open(scheme: "calshow://")
Objective-C
- (void)openScheme:(NSString *)scheme {
UIApplication *application = [UIApplication sharedApplication];
NSURL *URL = [NSURL URLWithString:scheme];
if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
[application openURL:URL options:@{}
completionHandler:^(BOOL success) {
NSLog(@"Open %@: %d",scheme,success);
}];
} else {
BOOL success = [application openURL:URL];
NSLog(@"Open %@: %d",scheme,success);
}
}
// Typical usage
[self openScheme:@"calshow://"];
Note:- Don't forgot to add privacy usage description in your info.plist file.,if you are trying to open any system app then in iOS 10+ you need to specify privacy usage description in your info.plist file else your app get crash.
Upvotes: 2
Reputation: 22343
As HoaParis already mentioned, you can call the calendar by using the openURL
method.
There is no embedded calendar by apple by default but you could check out other calendars for example the open-source one CVCalendar which is available at github. So you could either use it in your project or check how the developer has coded the calendar.
Upvotes: 1