小编Mar*_*rco的帖子

Swift-带有完成处理程序的getDeliveredNotifications在第一次通知时给出一个空数组

我有以下代码:

let center = UNUserNotificationCenter.current()

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    center.getDeliveredNotifications { (delivered) in
        print(delivered.count)
    }
    completionHandler([.alert, .badge])
}
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚在传递第一个通知时传递的是一个空数组。这意味着,例如,如果安排了2条通知,则第一个到达时将打印(0),然后第二个到达时将打印(1)。

有谁知道为什么这样的行为?我正尝试在通知发送后立即对其进行计数。谢谢!

ios swift unusernotificationcenter

6
推荐指数
0
解决办法
1383
查看次数

Swift - 交付应用程序上的本地通知图标徽章编号更新在后台

我试图弄清楚如何在传递本地通知时动态更新图标徽章编号。在安排时注册徽章号码不是一个选项,因为如果我在发送任何通知之前注册两个或多个通知,

UIApplication.shared.applicationIconBadgeNumber // this will be zero 
Run Code Online (Sandbox Code Playgroud)

在发送通知之前将始终为零。

我可以将 UNUsernotification 委托与 func 一起使用

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { } 
Run Code Online (Sandbox Code Playgroud)

但仅当应用程序处于活动状态时才会调用此函数。如果应用程序未激活怎么办?

我读过一些书,几乎我读过的每个人都说没有办法做这样的事情!但这可能吗?

苹果如何管理提醒和日历的通知?他们是本地通知并更新图标徽章吗?或者我犯了一个错误?我相信这一定是在发送本地通知时更新图标徽章的一种方法?

大家有什么想法吗?不敢相信苹果没有提供实现这一目标的方法!谢谢你!

ios uilocalnotification swift unusernotificationcenter

5
推荐指数
1
解决办法
4214
查看次数

Swift - CoreData NSPredicate - 获取父母的孩子

我正在尝试获取父母的所有孩子。

在我的例子中,父对象是一个实体 AgendaEvent,它有许多 AgendaDate(子对象)。

所以这是我的功能:

 func getRelatedAgendaEvent(event: AgendaEvent) ->NSFetchRequest<AgendaDate> {
    // create a fetch request that will retrieve all the AgendaDate.
    let fetchRquest = NSFetchRequest<AgendaDate>(entityName: "AgendaDate")

    // set the predicate to only keep AgendaDate related with the AgendaEvent selected
    fetchRquest.predicate = NSPredicate(format: "parent == %@", event)

    return fetchRquest
}
Run Code Online (Sandbox Code Playgroud)

我在 didselectRow 中使用它作为 tableView:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    var eventToPass: AgendaEvent!
    var eventDateToPassArray: [AgendaDate]!

    eventToPass = myTempEvents[indexPath.row]
    eventDateToPassArray = try! context.fetch(getRelatedAgendaEvent(event: eventToPass))
    DispatchQueue.main.async { () -> Void …
Run Code Online (Sandbox Code Playgroud)

core-data nsfetchrequest ios swift

5
推荐指数
1
解决办法
2692
查看次数

取消更改控制器时,表格视图单元格内的 Swift Avplayer 仍在播放

我在我的 tableview 单元格中配置了一个 avPlayer:

func createPlayer(url: URL) {
    self.playerCell = AVPlayer(url: url)
    self.videoContainerView.playerLayer.bounds = self.videoContainerView.bounds
    self.videoContainerView.playerLayer.videoGravity = .resizeAspectFill
    self.videoContainerView.playerLayer.player = playerCell
     playerCell?.play()
}
Run Code Online (Sandbox Code Playgroud)

每当用户单击按钮时调用此函数

@IBAction func playBtnPressed(_ sender: Any) {
    if let videoUrlString = post?.mediaUrl, let url = URL(string: videoUrlString) {
        createPlayer(url: url)
    }
    playBtn.isHidden = true
}
Run Code Online (Sandbox Code Playgroud)

在我的 tableview 控制器中,只要不再显示单元格,我就可以停止播放器,如下所示:

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    guard let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as? VideoCell else {return}
    if cell.playerCell != nil {
        if …
Run Code Online (Sandbox Code Playgroud)

uitableview ios avplayer swift

5
推荐指数
1
解决办法
2435
查看次数

Swift 3 - 具有一对多关系的核心数据保存

我正在处理核心数据,我有两个实体:

游泳池参数 每个游泳池可以有多个参数

泳池与参数的关系

游泳池实体

参数实体

我已经保存并获取了游泳池的数据。

我已经通过表格保存了它们。

在我的 AppDelegate 中我有:

let ad = UIApplication.shared.delegate as! AppDelegate
let context = ad.persistentContainer.viewContext
Run Code Online (Sandbox Code Playgroud)

这是函数:

 var swimmingpool: SwimminPool!

    if swimmingPoolToEdit == nil {
        swimming = SwimminPool(context: context)
    } else {
        swimming = swimmingPoolToEdit
    }

    if let name =  swimmingName.text {
        swimming.name = name
    }
    if let volume = swimmingVolume.text {
        swimming.volume = volume
.........................................................
ad.saveContext()
Run Code Online (Sandbox Code Playgroud)

我像这样执行获取:

var controller: NSFetchedResultsController<SwimmingPool>!

func attemptFetch() {

    let fetchRequest: NSFetchRequest<SwimminPool> = SwimminPool.fetchRequest()
    let dataSort = NSSortDescriptor(key: "createdAt", ascending: false)

    fetchRequest.sortDescriptors = …
Run Code Online (Sandbox Code Playgroud)

core-data ios swift

4
推荐指数
1
解决办法
3922
查看次数

Swift - AVFoundation 捕获图像方向

我有一个自定义相机,可以在人像模式下拍照。

我的问题是,如果我尝试以横向方式拍摄照片并保存,那么图片仍会以纵向模式保存。

我有这个功能来设置预览层:

 static func setupPreviewLayer(view: UIView) {
    cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
    switch UIDevice.current.orientation {
    case .portrait:
        cameraPreviewLayer?.connection?.videoOrientation = 
       AVCaptureVideoOrientation.portrait
    case .portraitUpsideDown:
        cameraPreviewLayer?.connection?.videoOrientation = 
          AVCaptureVideoOrientation.portraitUpsideDown
    case .landscapeLeft:
        cameraPreviewLayer?.connection?.videoOrientation =
        AVCaptureVideoOrientation.landscapeLeft
    case .landscapeRight:
        cameraPreviewLayer?.connection?.videoOrientation = 
         AVCaptureVideoOrientation.landscapeRight
    default:
        cameraPreviewLayer?.connection?.videoOrientation = 
         AVCaptureVideoOrientation.portrait
    }
    cameraPreviewLayer?.frame = view.frame
    view.layer.insertSublayer(cameraPreviewLayer!, at: 0)
}
Run Code Online (Sandbox Code Playgroud)

我称之为 viewWillAppear (我认为这不是正确的做法)。

然后我拍了张照片:

@IBAction func takePhotoBtnPressed(_ sender: Any) {
    let settings = AVCapturePhotoSettings()
    useFlash(settings: settings)
    settings.isAutoStillImageStabilizationEnabled = true
    CustomCamera.photoOutput?.capturePhoto(with: settings, delegate: self)
}
Run Code Online (Sandbox Code Playgroud)

并使用此扩展名:

extension FishCameraVC: AVCapturePhotoCaptureDelegate {
func …
Run Code Online (Sandbox Code Playgroud)

avfoundation ios landscape-portrait swift

3
推荐指数
2
解决办法
7669
查看次数

Swift-在textView中的光标之前识别单词(带有特殊字符)

我有一个扩展,可以识别并返回光标之前的单词:

extension UITextView {

var currentWord : String? {

    let beginning = beginningOfDocument

    if let start = position(from: beginning, offset: selectedRange.location),
        let end = position(from: start, offset: selectedRange.length) {

        let textRange = tokenizer.rangeEnclosingPosition(end, with: .word, inDirection: 1)

        if let textRange = textRange {
            return text(in: textRange)
        }
    }
    return nil
}
Run Code Online (Sandbox Code Playgroud)

我正在使用UItextGranularity.word并正常工作。

但是我的问题是这样的:

如果单词的开头有@,则不会返回。因此,如果我有@jon,则当前单词将为jon。有没有办法将@包含在内,以使完整的单词带有特殊字符?

谢谢

string uitextview ios swift

3
推荐指数
1
解决办法
441
查看次数

Swift 4 - Tab Bar项目按钮

我正在尝试将自定义按钮放在我的标签栏的其中一个项目上.

func setupMiddleButton() {

    let numberOfItems = CGFloat(tabBar.items!.count)
    let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
    let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: tabBarItemSize.width, height: self.tabBar.frame.size.height))
    var menuButtonFrame = menuButton.frame
    menuButtonFrame.origin.y = self.view.bounds.height - menuButtonFrame.height
    menuButtonFrame.origin.x = self.view.bounds.width/2 - menuButtonFrame.size.width/2
    menuButton.frame = menuButtonFrame

    menuButton.backgroundColor = UIColor.white

    self.view.addSubview(menuButton)


    self.view.layoutIfNeeded()
Run Code Online (Sandbox Code Playgroud)

我的问题是,使用前面的代码,按钮不完全在条形项上(见图): 在此输入图像描述

有什么建议吗?我真的不知道怎么试试.

谢谢!

uitabbar ios swift

2
推荐指数
1
解决办法
2776
查看次数