読者です 読者をやめる 読者になる 読者になる

Pragmatic ball boy

iOSを中心にやってる万年球拾いの老害エンジニアメモ

Xcode8にしたときのプッシュ通知対応

Project SettingsのCapabilitiesのタブを開き、PushNotificationsをONにします

f:id:yanamura:20160916183405p:plain

production.entitlements

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>production</string>
</dict>
</plist>

development.entitlements

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>development</string>
    <key>keychain-access-groups</key>
    <array>
        <string>$(AppIdentifierPrefix)share</string>
    </array>
</dict>
</plist>

Build SettingsのCode Signing Entitlementsで、Configurationによって使うentitlementsを変えるようにします

f:id:yanamura:20160916183853p:plain

最後にこれは必須ではないですが、

        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Alert, .Sound, .Badge]) { granted, _ in
                if granted {
                    UIApplication.sharedApplication().registerForRemoteNotifications()
                }
            }
        } else {
            let settings = UIUserNotificationSettings(forTypes: [.Alert, .Sound, .Badge], categories: nil)
            UIApplication.sharedApplication().registerUserNotificationSettings(settings)
            UIApplication.sharedApplication().registerForRemoteNotifications()
        }