Skip to content

Instantly share code, notes, and snippets.

@ryokwkm
Last active Oct 27, 2015
Embed
What would you like to do?
【swift】iOS 8以上対応 push通知ソース
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
//push通知の許可
if( application.respondsToSelector("registerUserNotificationSettings:")) {
// iOS8
let types:UIUserNotificationType = (.Alert | .Badge | .Sound )
let settings:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
} else {
// iOS7以下
application.registerForRemoteNotificationTypes(UIRemoteNotificationType.Badge | .Sound | .Alert )
}
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
//デバイストークンを取得
//<>と空白を取る
let characterSet:NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
let deviceTokenString:String = (deviceToken.description as NSString )
.stringByTrimmingCharactersInSet(characterSet)
.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
println(deviceTokenString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment