AppDelegate.swift 設定例
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var message:String?
var yourtoken:String?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Push通知を許可する
// let types: UIUserNotificationType = UIUserNotificationType.Badge
// UIUserNotificationType.Alert
// UIUserNotificationType.Sound
//
// let settings: UIUserNotificationSettings = UIUserNotificationSettings( forTypes: types, categories: nil )
//
// let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound,], categories: nil)
// UIApplication.sharedApplication().registerUserNotificationSettings(settings)
// UIApplication.sharedApplication().registerForRemoteNotifications()
//
// application.registerUserNotificationSettings( settings )
// application.registerForRemoteNotifications()
if #available(iOS 8.0, *) {
// iOS8以上
//forTypesは.Alertと.Soundと.Badgeがあります。
let notiSettings = UIUserNotificationSettings(forTypes:[.Alert,.Sound,.Badge], categories:nil)
application.registerUserNotificationSettings(notiSettings)
application.registerForRemoteNotifications()
} else{
// iOS7以前
application.registerForRemoteNotificationTypes( [.Alert,.Sound,.Badge] )
}
return true
}
// デバイストークンを取得する
func application( application: UIApplication!, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData! ) {
// <>と" "(空白)を取る
var characterSet: NSCharacterSet = NSCharacterSet( charactersInString: "<>" )
var deviceTokenString: String = ( deviceToken.description as NSString )
.stringByTrimmingCharactersInSet( characterSet )
.stringByReplacingOccurrencesOfString( " ", withString: "" ) as String
print( deviceTokenString )
var appDelegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate //AppDelegateのインスタンスを取得
appDelegate.yourtoken = deviceTokenString //appDelegateの変数を操作
//Construct url object via string
var url = NSURL(string: "http://push.animalnote.com:99/animalnote2/register.php")
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)
var req = NSMutableURLRequest(URL: url!)
var token = "token=\(deviceTokenString)"
req.HTTPMethod = "POST"
req.HTTPBody = token.dataUsingEncoding(NSUTF8StringEncoding)
var task = session.dataTaskWithRequest(req, completionHandler: {
(data, resp, err) in
print(resp!.URL!)
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
})
task.resume()
// var server = "http://localhost/register.php"
// var request = NSMutableURLRequest(URL: NSURL(string: server)!)
// request.HTTPMethod = "POST"
//
// var token = "token=\(deviceTokenString)"
//
// request.HTTPBody = token.dataUsingEncoding(NSUTF8StringEncoding)
//
// var response: NSURLResponse?
//
// var resultData = try NSURLConnection.sendSynchronousRequest(request, returningResponse: nil)
//
// var myData:NSString = NSString(data: resultData!, encoding: NSUTF8StringEncoding)!
//
// print(myData)
}
// デバイストークンの取得に失敗した場合
func application( application: UIApplication!, didFailToRegisterForRemoteNotificationsWithError error: NSError! ) {
print( error.localizedDescription )
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}