2015-04-16 36 views
3

如何从Swift应用打开日历(例如,按下按钮时)?或者有没有办法在应用中的视图控制器中嵌入日历? 我想避免使用别人编写的外部日历。谢谢!从Swift应用打开日历

回答

6

您可以通过使用URL方案calshow://打开日历应用:

UIApplication.sharedApplication().openURL(NSURL(string: "calshow://")!) 

随着EventKit,你可以实现你的自我的日历。您应该从Apple网站上阅读Calendar and Reminders Programming Guide

+0

我找不到Swift的日历编程指南,只能用于Objective-C。我更喜欢Swift。 – Amy

+0

你可以将你自己的代码翻译成swift。或者看看这个:https://gist.github.com/mchirico/d072c4e38bda61040f91#file-cal-swift –

+0

我们可以像UIApplication.sharedApplication()一样打开默认提醒。openURL(NSURL(string:“calshow:// “)!)? –

1

随着HoaParis的提及,您可以使用openURL方法调用日历。

默认情况下,苹果没有嵌入式日历,但您可以查看其他日历,例如github上提供的其他日历,例如开源软件CVCalendar。所以你可以在你的项目中使用它,或者检查开发者是如何编码日历的。

2

的OpenURL弃用iOS10

从苹果公司的指导What’s New iOS中的部分上的UIKit:

新的UIApplication方法的OpenURL:选择:completionHandler :,它执行 异步并在主队列上调用指定的完成处理程序 (此方法替换openURL :)。

斯威夫特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://"]; 

注: -不要忘了在你的info.plist文件中添加隐私用法说明。如果你正在尝试打开任何系统应用程序,那么在iOS 10+中,你需要指定pri在您的info.plist文件中出现错误的使用说明,否则您的应用程序将崩溃。