users device fails data property encoding, but works on another device - ios

Had a very unusual bug with a handful of our users (less than 10 known out of thousands). Some of the objects that we store in coreData use date types. For some reason, on a specific person's device, some objects would fail during initialization whenever the JSON tried to map the dateString to our formatter.
static let iso8601DateTime: DateFormatter = {
let formatter = DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)[enter image description here][1]
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
return formatter
}()
When I made a breakpoint, all JSON time strings came in this format:
I was puzzled why some objects were successfully mapping at that point and others failed despite all of them returning in that format. By change, I read an issue on Apple's support website about language & region with ISO8601. I changed his region from US -> UK -> (back to) US, and found that all of the issues were resolved and He was able to use the application as normal.
Some advice I received said to specify a locale for the dateformatter, but I haven't been able to test yet.
formatter.locale = Locale(identifier: "en_US_POSIX")
Any idea why basically soft-refreshing this setting resolved it (setting to another and then back again)?

Related

How to use swift extension to get the IAP price in local currency

I am learning swift on my own and I cannot seem to figure out how to implement the extension below. I have an app on the App Store with a functioning IAP. But I now need to indicate the price in the local currency and not simply in US$ before the user clicks the button to buy.
I have read numerous posts about using an extension similar to the one below. However, I have been unable to get the cost to display as a String I can put in a label.
extension SKProduct {
/// - returns: The cost of the product formatted in the local currency.
var regularPrice: String? {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = self.priceLocale
return formatter.string(from: self.price)
}}
Among the methods I tried were:
label = SKProduct.regularPrice
label = product.regularPrice
I tried putting in my product ID ("com.afafaf.2323") but none of the attempts worked.
Yes, this is similar to some other posts, but none of them explain the implemtation to the extent I need.
I am clearly missing something easy, but I am not seeing it. Can someone please just show me how to use this extension properly?

How to get currentUTC time in swift

I want to set count down timer in swift. I have an option is to get current time is Date() but this method is giving wrong date time when my device time set wrong.
Is it possible to get exact current UTC time in swift, so I will set count down timer with exact time remaining.
Thanks.
The Date class doesn't have a timezone, it's just a "point int the time line" that's the same for all the timezones. When you use a DateFormatter to convert a date to a string (or a string to a date) you can specify the timezone like this:
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
If you cannot trust the device date you will have to use a NTP service like this:
https://github.com/instacart/TrueTime.swift
https://github.com/jbenet/ios-ntp
Get Date and Time from Apple Server
Many times, I have faced the same issue if the user changed his current time then lot's of logic will disturb, Unfortunately, no luck because of Date() always returns your device date.
In well-known game Candy crush, We can not play it for a specific time if my points got over, But if I change device time to feature time then everything will be unlocked. This is the best example of your problem.
You can use below-given web service or your web service to achieve your requirements. Below are some
free API's which provides date and time.
Geonames
Timezonedb
TrueTime
In addition to other answers, you can write an extension for Date class to get formatted Data in specific TimeZone to make it as utility function for future use. Like
extension Date {
func dateInTimeZone(timeZoneIdentifier: String, dateFormat: String) -> String {
let dtf = DateFormatter()
dtf.timeZone = TimeZone(identifier: timeZoneIdentifier)
dtf.dateFormat = dateFormat
return dtf.string(from: self)
}
}
Now you can call it like
Date().dateInTimeZone(timeZoneIdentifier: "UTC", dateFormat: "yyyy-MM-dd HH:mm:ss");

Converting string to Date in Swift 4 fails in "Jailbroken" iPhone devices only. Why?

Converting string to Date fails in jailbroken iPhone devices, but works fine in normal iPhone devices.
Note: I get the date string from a server in UTC format.
My code snippet is as below.
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "UTC")! as TimeZone
if let date = dateFormatter.date(from: result["timestamp"].stringValue) {
//convert the UTC time to Mexico's local time if string to date conversion is successful
} else {
//failed to get Date from string (happens in Jailbroken devices only)
}
This code enters the else condition when it is run on jailbroken iPhone devices. My client reports that it happens only on jailbroken devices.
When I run the code on non-jailbroken iPhone devices, the string to date conversion works perfectly.
So, I am not getting this: how do I get a Date from a string on "jailbroken" iPhone devices?
Please do help me out.
Thanks in advance.

iOS UI Test Datepicker

I'm making a UI Test for an iOS app. I'm having trouble finding out how to use automated testing for a date picker. Right now I'm stuck trying to find the date picker itself so I can change the values.
I've searched many threads regarding this issue but have found nothing.
If anyone has any insight to this it would be much appreciated. Thanks!
To get started, I'd advise you to use the "Record UI Test" button in Xcode to easily find out how to access the UIDatePicker in the UI Test.
For a simple UIDatePicker in date mode, changing the date is as simple as:
let datePickers = XCUIApplication().datePickers
datePickers.pickerWheels.element(boundBy: 0).adjust(toPickerWheelValue: "June")
datePickers.pickerWheels.element(boundBy: 1).adjust(toPickerWheelValue: "1")
datePickers.pickerWheels.element(boundBy: 2).adjust(toPickerWheelValue: "2015")
If the datePicker is connected to a UILabel, you could then check if the text in the label has been updated correctly.
For anyone, supporting multiple localizations, an easier way would be
let dateComponent = Calendar.current.dateComponents([.day, .month, .year], from: Date())
let formatter = DateFormatter()
formatter.dateFormat = "MMMM"
let monthText = formatter.string(from: Date())
datePicker.pickerWheels[String(dateComponent.year!)].adjust(toPickerWheelValue: "2017")
datePicker.pickerWheels[monthText].adjust(toPickerWheelValue: "December")
datePicker.pickerWheels[String(dateComponent.day!)].adjust(toPickerWheelValue: "21")

Firebase 2.0 - Store Date and Timezone

I need to store the date that something was created in the firebase database for a friend request, how can i store a date in firebase that i can work out how long ago since this date something happened. I would just store an NSDate in there but then what about timezones?
I dunno if firebase has something like Parse had where it would store date created and timezone automatically so you could read it from the server?
You need to set it up by yourself !
I use this code to do the timestamp. Hope this is what u want.
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy/MM/dd, H:mm:ss"
let defaultTimeZoneStr = formatter.stringFromDate(NSDate())
let post = ["\(key)":["username": username, "userPhoto": userPhotoUrl, "postPhoto": downLoadURL,"postText": postText!,"User":FIRAuth.auth()!.currentUser!.uid, "time":defaultTimeZoneStr] ]
self.DatabaseRef.child("posts").updateChildValues(post)

Resources