【发布时间】:2016-09-17 08:05:50
【问题描述】:
这些是应用程序崩溃前后的图像,控制台只显示消息来自调试器的消息:由于信号6而终止 [[我的应用程序启动(当未运行)从推送通知成功,我也获得了所需的屏幕和结果,但是当它在应用程序处于活动状态时通过点击通知再次收到通知时崩溃。当应用程序从主屏幕通知功能正常启动时,在应用程序处于活动状态和后台时操作都可以正常工作。我在下面发布我的代码,请帮助我。
当我从收到的通知启动我的应用程序时,应用程序午餐成功并转到受尊重的窗口,现在当我在应用程序打开时收到通知然后应用程序崩溃时,这意味着每当我的应用程序未在后台或前台运行并且我使用通知启动我的应用程序.. 然后当我收到通知应用程序崩溃时再次启动
下面是我的代码,请帮忙
导入 UIKit
@UIApplicationMain 类 AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let barAppearace = UIBarButtonItem.appearance()
barAppearace.setBackButtonTitlePositionAdjustment(UIOffsetMake(0, -60), forBarMetrics:UIBarMetrics.Default) //CODE TO REMOVETITLE OFACK BUTTON ITEM IN NAVIGATIION CONTROLLER
let notificationTypes : UIUserNotificationType = [.Alert, .Badge, .Sound]
let notificationSettings : UIUserNotificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings)
if let notification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject] {
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AgentAllTab.readyNotificationAction), name: "AgentReadyNotification", object: nil)
let userInfo = launchOptions![UIApplicationLaunchOptionsRemoteNotificationKey] as? [String: AnyObject]
let aps = userInfo!["aps"] as! [String: AnyObject]
print("Remote noti data from didFinishLaunchingWithOptions \(aps)")
let data = aps["data"] as! [String: AnyObject]
let type = aps["type"] as! Int
print("notification TYPE \(type)")
开关类型{ 案例0:
NSUserDefaults.standardUserDefaults().setObject(aps, forKey: "notificationlauch")
break
default:
break
}
返回真
}
func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings)
{
if notificationSettings.types != .None {
application.registerForRemoteNotifications()
}
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""
for i in 0..<deviceToken.length {
tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}
NSUserDefaults.standardUserDefaults().setObject(tokenString, forKey: "DeviceToken")
NSUserDefaults.standardUserDefaults().synchronize()
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print(error.localizedDescription)
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
let aps = userInfo["aps"] as! [String: AnyObject]
let data = aps["data"] as! [String: AnyObject]
let type = aps["type"] as! Int
//Do something when app is active
if UIApplication.sharedApplication().applicationState == UIApplicationState.Active {
switch type {
case 0:
let custName = data["customerName"] as! String
let notification = CWStatusBarNotification()
notification.notificationStyle = .NavigationBarNotification
notification.notificationAnimationInStyle = .Top
notification.notificationLabelBackgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.8)
notification.notificationLabelTextColor = UIColor.whiteColor()
notification.notificationLabelFont = UIFont.boldSystemFontOfSize(15)
notification.displayNotificationWithMessage("\(custName) shorlisted you", forDuration: 3.0)
notification.notificationTappedClosure = {
NSNotificationCenter.defaultCenter().postNotificationName("AgentReadyNotification", object: self)
notification.dismissNotification()
}
break
default:
break
}
} else {
// Do something else when your app is in the background
switch type {
case 0 :
NSNotificationCenter.defaultCenter().postNotificationName("AgentReadyNotification", object: self)
break
default:
break
}
}
}
func applicationWillResignActive(application: UIApplication) {
print(" applicationWillResignActive")
}
func applicationDidEnterBackground(application: UIApplication) {
print(" applicationDidEnterBackgroundndddddddddddddd")
}
func applicationWillEnterForeground(application: UIApplication) {
print(" applicationWillEnterForeground")
}
func applicationDidBecomeActive(application: UIApplication) {
print(" applicationDidBecomeActive")
}
func applicationWillTerminate(application: UIApplication) {
print(" applicationWillTerminate")
}
}
//此代码来自app delegate.swift //现在来自处理推送的视图控制器的代码
import UIKit
class AgentAllTab: UITableViewController ,UIPopoverPresentationControllerDelegate ,AgentFilterDelegate {
var allQuoteId = String()
var filterTitle = "Market"
var quotes: [[String: AnyObject]] = [[:]] //VALUE FOR RESPONSE DICT
var newQuoteDict: Dictionary<String, String> = [String: String]() // DECLARING DICTIONARY FOR POST PARAMETERS
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AgentAllTab.readyNotificationAction), name: "AgentReadyNotification", object: nil)
if ( NSUserDefaults.standardUserDefaults().objectForKey("notificationlauch") != nil){
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AgentAllTab.readyNotificationAction), name: "NotificationLaunchReady", object: nil)
NSNotificationCenter.defaultCenter().postNotificationName("NotificationLaunchReady", object: self)
NSUserDefaults.standardUserDefaults().removeObjectForKey("notificationlauch")
NSUserDefaults.standardUserDefaults().synchronize()
return
}
executeFetch("/Market_all/")
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return quotes.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if filterTitle == "Ready"{
cell = tableView.dequeueReusableCellWithIdentifier("AgentAllTabReadyCell", forIndexPath: indexPath) as! AgentAllTabCellSubClass
if(quotes[indexPath.row].count == 0){
//normal code to hide all content of cell
}
}else{
//code in case we get data
}else{
//Code for other filter title same as above
}
return cell
}
func executeFetch(apiurl : String){
//function to fetch data from server and feed into uitableviewcontroller
}
//处理推送通知的函数 func readyNotificationAction(notification:NSNotification) {
filterTitle = "Ready"
self.tabBarController?.tabBar.hidden = true
executeFetch("/agentReady/")
}
}
【问题讨论】:
-
在哪一行崩溃?
-
当它从通知启动时一切正常,但是当它再次收到处于活动状态的通知时(从通知启动后)它会崩溃。
-
好的,但您没有提供崩溃原因,问题中有错误。你能添加品脱屏幕崩溃吗?
-
@IAmDav 来自调试器的消息:由于信号 6 而终止这是我从控制台得到的唯一消息
-
@IAmDav 我添加了一张图片
标签: notifications apple-push-notifications ios9 swift2.2