Open your calendar from Swift app

How do I open a calendar from a Swift app (for example, on a button press)? Or is there a way to embed the calendar in an in-app view controller? I want to avoid using external calendars programmed by others. Thank!

+3


source to share


3 answers


You can open the Calendar app using the diagram 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 own calendar. You should read the Calendar and Reminders Programming Guide from the Apple website.

+6


source


openURL Deprecated in iOS10

From the Apples guide to What's New in iOS under UIKit:

New UIApplication method openURL: options: completeHandler: that runs 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 forget to add a privacy statement in the info.plist file. If you are trying to open any system app, then in iOS 10+ you need to specify the use of privacy description in your info.plist file, otherwise your app will crash.

+2


source


As mentioned in HoaParis, you can call the calendar using the method openURL

.

There is no built-in apple calendar by default, but you can check out other calendars like the open source CVCalendar which is available on github. So you can use it in your project or check how the developer has coded the calendar.

+1


source







All Articles