Swiftを写経7

今日の写経

008 UILocalNotificationの表示 - Swift Docs

所感

myNotificationFireButton.tag = 2 を書かなかったから時限が起動しなかった。

眠気眼で書いたらいけん。

画面キャプチャ

f:id:novlessn:20150807144305p:plain f:id:novlessn:20150807144313p:plain

ソースコード

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // アラート表示の許可をもらう
        UIApplication.sharedApplication().registerUserNotificationSettings(
            UIUserNotificationSettings(
                forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert, categories: nil
            )
        )
        
        // すぐにNotificationを発火するボタンを作成する
        let myNotificationButton: UIButton = UIButton(frame: CGRectMake(0, 0, 200, 100))
        myNotificationButton.backgroundColor = UIColor.orangeColor()
        myNotificationButton.layer.masksToBounds = true
        myNotificationButton.setTitle("Notification", forState: .Normal)
        myNotificationButton.layer.cornerRadius = 20.0
        myNotificationButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: 200)
        myNotificationButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside)
        myNotificationButton.tag = 1
        
        // 時間をおいてNotificationを発火するボタンを作成する
        let myNotificationFireButton: UIButton = UIButton(frame: CGRectMake(0, 0, 200, 80))
        myNotificationFireButton.backgroundColor = UIColor.blueColor()
        myNotificationFireButton.layer.masksToBounds = true
        myNotificationFireButton.setTitle("Notification(Fire)", forState: .Normal)
        myNotificationFireButton.layer.cornerRadius = 20.0
        myNotificationFireButton.layer.position = CGPoint(x: self.view.bounds.width/2, y: 400)
        myNotificationFireButton.addTarget(self, action: "onClickMyButton:", forControlEvents: .TouchUpInside)
        myNotificationFireButton.tag = 2
        
        //ViewにButtonを追加する
        view.addSubview(myNotificationButton)
        view.addSubview(myNotificationFireButton)
    }

    /*
    ボタンイベント
    */
    internal func onClickMyButton(sender: UIButton){
        println("onClickMyButton")
        if sender.tag == 1 {
            showNotification()
        } else if sender.tag == 2 {
            showNotificationFire()
        }
    }
    
    /*
    Show Notification
    */
    private func showNotification(){
        println("showNotification")
        
        // Notificationを生成する
        let myNotification: UILocalNotification = UILocalNotification()
        myNotification.alertBody = "TEST"
        myNotification.timeZone = NSTimeZone.defaultTimeZone()
        UIApplication.sharedApplication().scheduleLocalNotification(myNotification)
    }
    
    /*
    Show Notificatiion(10 sec後に発火)
    */
    private func showNotificationFire(){
        println("shoNotificationFire")
        
        // Notificationを生成する
        let myNotification: UILocalNotification = UILocalNotification()
        myNotification.alertBody = "TEST(Fire)"
        myNotification.soundName = UILocalNotificationDefaultSoundName
        myNotification.timeZone = NSTimeZone.defaultTimeZone()
        myNotification.fireDate = NSDate(timeIntervalSinceNow: 10)
        UIApplication.sharedApplication().scheduleLocalNotification(myNotification)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}