共享:
共享是用来与别人分享内容的,在IOS与OSX上,共享有一些不同。但相同的是,你需要提供一个你需要分享的数组。这个数组是有讲究的,如果你同时放入相同类型的数据,比如好几张图片,那么分享时自然就都一起发过去了,如果你分享的东西不是一种类型的,比如文本加图像,那么系统会找到你第一个可分享的数据的类型,并将此数组中同样属于这个类型的数据分享出去。
IOS:
let activity = UIActivityViewController(activityItems: [self.textView.text!], applicationActivities: nil)
//presentViewController是model的呈现一个ViewController,它有很多种呈现的方式,在iPhone上默认是覆盖整个屏幕,不用设置什么别的属性直接调用即可
//在iPad上则默认弹出一个小框,如果引发这个弹窗的是一个BarItem,貌似也不需要设置什么,系统会自动判断弹窗应该从什么地方出现
//但是如果是一个案件,就需要设置下面这句话了,告诉系统这个弹窗该指向哪里
activity.popoverPresentationController?.sourceView = sender as? UIView
self.presentViewController(activity, animated: true, completion: nil)
OSX:
在OSX上,你需要规定弹出框的位置和大小。
var share = NSSharingServicePicker(items: [text.stringValue])
share.showRelativeToRect(NSRect(x: 0.0, y: 0.0, width: 20, height: 30), ofView: self.view, preferredEdge: NSRectEdge.MaxY)
通知:
通知分两个部分,接收和发送。这两者之间通过通知的标识符来互相联系。通知有两种,一种是通过网络接收的通知,一种是本地App自己发送的通知。
接收通知:
//注册通知到来时可进行的操作
let notificationAction = UIMutableUserNotificationAction()
notificationAction.title = "Save World"
notificationAction.identifier = "saveWorld"
notificationAction.activationMode = UIUserNotificationActivationMode.Foreground
notificationAction.authenticationRequired = true
notificationAction.destructive = false
let badNotificationAction = UIMutableUserNotificationAction()
badNotificationAction.title = "Destroy World"
badNotificationAction.identifier = "destroyWorld"
badNotificationAction.activationMode = UIUserNotificationActivationMode.Background
badNotificationAction.authenticationRequired = false
badNotificationAction.destructive = true
let badbadNotificationAction = UIMutableUserNotificationAction()
badbadNotificationAction.title = "Conquer World"
badbadNotificationAction.identifier = "conquerWorld"
badbadNotificationAction.activationMode = UIUserNotificationActivationMode.Background
badbadNotificationAction.authenticationRequired = false
badbadNotificationAction.destructive = true
//这个就是通知到来时真正通知的对象,他的标示符应该和通知的一样
let notificationCategory = UIMutableUserNotificationCategory()
notificationCategory.identifier = "com.bbbb.exialym"
//在空间足够时显示什么操作
notificationCategory.setActions([notificationAction,badNotificationAction,badbadNotificationAction], forContext: UIUserNotificationActionContext.Default)
//在空间不够时显示什么操作
notificationCategory.setActions([notificationAction,badNotificationAction], forContext: UIUserNotificationActionContext.Minimal)
let notificationCategories = NSSet(object: notificationCategory)
let notificationSettingsWithAction = UIUserNotificationSettings(forTypes: UIUserNotificationType.Badge, categories: notificationCategories as? Set<UIUserNotificationCategory>)
notificationSettingsWithAction
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettingsWithAction)
发送本地通知:
let localNotification = UILocalNotification()
localNotification.fireDate = NSDate(timeIntervalSinceNow: 10)
localNotification.category = "com.bbbb.exialym"
localNotification.alertTitle = "Oh No"
localNotification.alertBody = "The world is in peril!"
localNotification.applicationIconBadgeNumber = 9999
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)