카테고리 없음

swift 버튼에 알림음 기능 추가해보자

kingarthur 2024. 7. 10. 21:33

버튼에 알림기능이 있으면 좋겠다고 생각을하여서 

알림을 기능을 구현해보기 위해 공부를 하였다 ~ 

 

먼저 버튼 기능을 하나 만들고 코드를 추가하면 된다.

@objc func showAlert() {
        // UIAlertController 생성
        let alertController = UIAlertController(title: "Choose an option", message: nil, preferredStyle: .actionSheet)
        
        // 알림에 추가할 옵션들 생성
        let antic = UIAlertAction(title: "1-antic", style: .default) { action in
            self.performAction(for: "1-antic")
        }
        let cheers = UIAlertAction(title: "2-cheers", style: .default) { action in
            self.performAction(for: "2-cheers")
        }
        let chord = UIAlertAction(title: "3-chord", style: .default) { action in
            self.performAction(for: "3-chord")
        }
        let droplet = UIAlertAction(title: "4-droplet", style: .default) { action in
            self.performAction(for: "4-droplet")
        }
        let handoff = UIAlertAction(title: "5-handoff", style: .default) { action in
            self.performAction(for: "5-handoff")
        }
        let milestone = UIAlertAction(title: "6-milestone", style: .default) { action in
            self.performAction(for: "6-milestone")
        }
        let note = UIAlertAction(title: "7-note", style: .default) { action in
            self.performAction(for: "7-note")
        }
        let passage = UIAlertAction(title: "8-passage", style: .default) { action in
            self.performAction(for: "8-passage")
        }
        let portal = UIAlertAction(title: "9-portal", style: .default) { action in
            self.performAction(for: "9-portal")
        }
        let rattle = UIAlertAction(title: "10-rattle", style: .default) { action in
            self.performAction(for: "10-rattle")
        }
        let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        
        // 옵션들을 알림에 추가
        alertController.addAction(antic)
        alertController.addAction(cheers)
        alertController.addAction(chord)
        alertController.addAction(droplet)
        alertController.addAction(handoff)
        alertController.addAction(milestone)
        alertController.addAction(note)
        alertController.addAction(passage)
        alertController.addAction(portal)
        alertController.addAction(rattle)
        alertController.addAction(cancel)
        
        // 알림 표시
        present(alertController, animated: true, completion: nil)
    }
    
    func performAction(for option: String) {
        let alert = UIAlertController(title: "Selected", message: "You selected \(option)", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }

이렇게 알림 기능을 만들어 주면 된다. 

 

그리고 파일은 만들어주면 되는데 

 

https://onlineconvertfree.com/kr/convert-format/m4a-to-caf/

 

여기서 파일을 변경해주면 된다 

 

애플에서 정해놓은 것은 caf 음원파일만 가능하고 30초 이상 음원은 사용이 안되게 되어있다.

 

그리고 앱 자체 내부에 음원 접근도 금지되어 있어서 사용이 안된다고 한다. 일단 내가 찾아봤을때는 안됬다. 

 

그러니 나처럼 시간 낭비하지말고 그냥 앱 차제에 음원을 넣어주자. 

 

https://blog.naver.com/airdp36/223240595601

 

아이폰15 기본 벨소리 ios17 알림소리 파일 다운로드

최신 스마트폰 꿀정보를 알려드리는 럭키싱글입니다. 10월 13일 드디어 많은 분들이 학수고대하던 아이폰 1...

blog.naver.com

 

알림음은 여기서 다운을 받았다~ 

참고해서 사용하면 될것 같다.