My app cannot generate device token - swift2

Suddenly my app cannot generate device token.
I created a app to receive remote push notification, thje app worked well, but today my app dont generate device token, I run code on xcode and dont generate token. No delegates method called.
I dont change anything and dont work more. The function didRegisterForRemoteNotificationsWithDeviceToken dont work. Look:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print(deviceToken)
}
dont print nothing
this function do nothing:
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print(error)
}
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
// Override point for customization after application launch.
let types:UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: types, categories: [newsCategory]))
return true
}
Why this problem? How can I fix it?

You have also to call application.registerForRemoteNotifications() in application:didFinishLaunchingWithOptions after registering the settings.

Related

UIPicker not showing in Mac Catalyst

I'm taking an iPhone app and making it available for Mac.
When I run the app on the Mac, the UIPicker displays nothing.
It places the cursor inside of the button and allows me to type, which is not the behavior I want (want to allow selecting from a list of options). Video in action.
I've tried putting Print statements in each of those functions (since removed) and they won't print so for whatever reason it seems like the functions themselves don't even run.
What approach(es) should I take to make a picker work on the Mac?
What am I screwing up?
What other information should I provide?
UI Picker (using UIKit):
// Mortgage Length Picker
#IBOutlet weak var mortgageLengthButton: UITextField!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return mortgageLengthData.count
}
func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return mortgageLengthData[row]
}
func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
mortgageLengthButton.text = mortgageLengthData[row]
let mortgageArray = mortgageLengthData[row].components(separatedBy: " ")
mortgageLengthYears = Int(mortgageArray[0]) ?? 30
calculateMortgage()
calculateCashFlow()
}
OS 10.15.6
xCode 12.0.1

Xcode 11.4 didFailToRegisterForRemoteNotificationsWithError was called when registering for push notifications

Seems like an Xcode bug. I'm on 11.4 and my simulator is on 13.4. For some reason, the fail handler is triggering.
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
granted,
_ in
if granted {
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
}
return true
}
func application(_: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
print("registed")
}
func application(_: UIApplication,
didFailToRegisterForRemoteNotificationsWithError error: Error) {
assertionFailure(String(describing: error))
}
Error: Error Domain=NSCocoaErrorDomain Code=3010 "remote notifications are not supported in the simulator" UserInfo={NSLocalizedDescription=remote notifications are not supported in the simulator}

Xcode8 Swift3 ImageURL returns nil

I am trying to download an image from my web server, using Xcode 8 and Swift 3. It looks like this. The request returns nil. Any suggestions why?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var view1: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func load(_ sender: UIButton) {
let url = URL(string:"http://www.example.com/folder2/pic3-1.jpg")
let task = URLSession.shared.dataTask(with: url!) { data, response,
error in
guard let data = data, error == nil else { return }
DispatchQueue.main.sync() {
self.view1.image = UIImage(data: data)
}
}
task.resume()
}
#IBAction func reset(_ sender: UIButton) {
self.view1.image="blankU.png"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
Any idea why the image is returning nil from the server?
The Xcode8, Swift3 coding is okay. The server is blocked.

Swift : EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

Hi I am brand new to swift and need some help
Every time I run my code my app crashes and spits out a " the above error
Can anyone please tell me what I need to do or what I need to replace in my code?
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
#IBOutlet weak var picker: UIPickerView!
var employeeNames = ["John","Jane","Jack","James"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.picker.delegate = self
self.picker.dataSource = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return employeeNames.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return employeeNames [row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print(employeeNames[row])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Your code is absolutely fine, so the only alternative is that you haven't hooked up your pickerView outlet. Ctrl click on your picker view in the storyboard and see if your outlet 'picker' shows up. If it doesn't then ctrl drag from the pickerView to your outlet to hook it up.

Send push notification via parse to your iphone as a test

I implemented parse push notifications in my project and I tested it out with a test code. In viewController:
var push = PFPush()
push.setMessage("This is a test")
push.sendPushInBackgroundWithBlock({
(isSuccesful: Bool!, error: NSError!) -> Void in
println(isSuccesful)
})
and in the appDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Here is the code for parse Push notifications.
Parse.setApplicationId("oIqRHQ8SBqLiuFzU5fIXRKgMVTHrH4ft6Gat7BW7", clientKey: "zPJ5SpDRFg9IqgiWZmW0N3FumzEwSDK1YvPxsipl")
var pushSettings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: .Alert, categories: nil)
application.registerUserNotificationSettings(pushSettings)
application.registerForRemoteNotifications()
// Override point for customization after application launch.
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
println("succesful")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
println("failed :(")
}
When I launch the app, it prints successful and true. Also, on the parse.com website I can see the notifications were send. However, why can't I send a notification to my phone using the parse website? When I try to do that, it says there are no registered devices. But I did register my device (with the .p12 certificate).
What could solve this?
I guess what you forgot is to save the deviceToken for your installation on parse. Try this:
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
PFInstallation.currentInstallation().setDeviceTokenFromData(deviceToken)
PFInstallation.currentInstallation().saveInBackgroundWithBlock() { (success, error) in
if error != nil {
println("Saving failed")
}
if success {
println("Saved the new device push token to parse successfully")
}
}
}

Resources