iOS get Last mobile shutdown time [duplicate] - ios

This question already has answers here:
Getting iOS system uptime, that doesn't pause when asleep
(6 answers)
Closed 6 years ago.
Is this possible to get Last shutdown or reboot time using objective c or Swift.
I tried many websites and forums. i can't find solution for this problem.

Check out NSProcessInfo there is a property called systemUptime of type NSTimeInterval
NSProcessInfo.processInfo().systemUptime
[[NSProcessInfo processInfo] systemUptime]
I gave you your answer in both objective-c and swift since you tagged them both.

You cannot detect the last shutdown time. But you can detect the last reboot time as:
let systemUptime = NSProcessInfo.processInfo().systemUptime;
And to detect the time, since last reboot is:
let systemUptime = NSProcessInfo.processInfo().systemUptime;
let timeNow = NSDate().timeIntervalSince1970
let dateOfLastReboot = NSDate(timeIntervalSince1970: timeNow-systemUptime)

Related

Every time Apple release a new XCode version my UI tests fail [duplicate]

This question already has answers here:
Xcode UI Testing Error keyboard
(2 answers)
Closed 2 years ago.
Every time Apple release a new XCode version, my UI tests fail. And I need to spend days figuring out what needs to be changed in the tests.
Is there something I am missing?
Example:
let tablesQuery = app.tables
let passwordCellsQuery = tablesQuery.cells.containing(.staticText, identifier:"Password")
passwordCellsQuery.children(matching: .secureTextField).element.tap()
passwordCellsQuery.children(matching: .secureTextField).element.typeText("12345678")
let memorableDateDdMmYyyyCellsQuery = tablesQuery.cells.containing(.staticText, identifier:"Memorable Date (dd/mm/yyyy)")
memorableDateDdMmYyyyCellsQuery.children(matching: .secureTextField).element(boundBy: 2).tap()
memorableDateDdMmYyyyCellsQuery.children(matching: .secureTextField).element(boundBy: 2).typeText("1")
memorableDateDdMmYyyyCellsQuery.children(matching: .secureTextField).element(boundBy: 0).tap()
memorableDateDdMmYyyyCellsQuery.children(matching: .secureTextField).element(boundBy: 0).typeText("2")
memorableDateDdMmYyyyCellsQuery.children(matching: .secureTextField).element(boundBy: 1).tap()
memorableDateDdMmYyyyCellsQuery.children(matching: .secureTextField).element(boundBy: 1).typeText("3")
This time around I get "Failed to synthesize event: Neither element nor any descendant has keyboard focus. Event dispatch snapshot: SecureTextField"
It's got to the point that I dread any new XCode release as it ALWAYS breaks all my UI tests, this time it's Version 11.4.1 (11E503a).
Unit tests behave (thankfully).
You should not stick to the autogenerated code.
Write the test code and elements' description by yourself – this way you test will be more stable.
Try to make your code simpler – it will be easier to maintain.
let table = app.tables.element
let passwordCell = table.cells["Password"]
passwordCellsQuery.tapAndType("12345678")
let dateCell = table.cells["Memorable Date (dd/mm/yyyy)"]
dateCell.secureTextFields.element(boundBy: 2).tapAndType("1")
dateCell.secureTextFields.element(boundBy: 0).tapAndType("2")
dateCell.secureTextFields.element(boundBy: 1).tapAndType("3")
extension XCUIElement {
func tapAndType(_ text: String) {
tap()
typeText(text)
}
}

How to get iOS APP archive date using Swift [duplicate]

This question already has answers here:
Get build date and time in Swift
(6 answers)
Closed 5 years ago.
I have a requirement in my application that, i need to show .ipa file creation date using swift.
Can anybody tell me how to do that.
Thanks in advance.
You can get the url of your app using Bundle property executableURL and use url method resourceValues to get the bundle creation date:
if let executableURL = Bundle.main.executableURL,
let creation = (try? executableURL.resourceValues(forKeys: [.creationDateKey]))?.creationDate {
print(creation)
}

Swift Dateformatting fails for just a specific day [duplicate]

This question already has an answer here:
1st april dates of 80s failed to parse in iOS 10.0
(1 answer)
Closed 5 years ago.
Today a very weird bug (or so I believe) started occurring. Here is the piece of code:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let dateObj = dateFormatter.dateFromString(serviceDate)
dateFormatter.dateFormat = "dd/MM/yyyy"
let serviceDateBr = dateFormatter.stringFromDate(dateObj!)
Now comes the strange part. When I set serviceDate = "2016-10-15", for example, it works:
let serviceDate = "2016-10-15"
...
print("dateSQL: \(serviceDate), dateBR: \(serviceDateBr)")
--------
Answer = dateSQL: 2016-10-15, dateBR: 15/10/2016
On the other hand, when I just change serviceDate to "2016-10-16" it crashes. Not on day 17, 18 or any other. Just 16.
let serviceDate = "2016-10-16"
...
print("dateSQL: \(serviceDate), dateBR: \(serviceDateBr)")
--------
Answer = fatal error: unexpectedly found nil while unwrapping an Optional value
I already know that this fatal error occurs when the first formatting fails, returns nil and then I try to force unwrap it on stringFromDate(). But I can't see why it fails in first place. Can anyone help me?
If it is relevant, I am using Xcode 7.3.1. This bug occurs on device and simulator.
Many thanks.
I suppose you are living in Brasil.
On October 16, 2016 the daylight saving time changes and there is no 0:00.
I have tried on Xcode 7.3 and it seems work fine and the results
dateSQL: 2016-10-15, dateBR: 15/10/2016
cloud you provide more info?

How to save information ( Swift application )

I am developing an iOS application which generate random inspirational quote every day. At the moment when I close the app, open it again and click on the button which generates the daily quote, it shows me a new one.
Can you help me, how can I save the same quote all over the day and when the day is over generate a new quote. I mean at 00:00 o'clock in the morning.
I want to keep 1 quote per day, not 1 quote for every time I open the app.
Okay, I have some time on my hands and I can provide you a small tutorial on how to save data to the user defaults.
You will want to save the date on which you create a quote right after you created it and then each time your app goes to foreground check against that. Obviously you want to only remember the day, not the hours, minutes and seconds. Here's a small function for that:
func makeCleanToday() -> NSDate {
let today = NSDate()
let comps = NSCalendar.currentCalendar().components([.Day, .Month, .Year], fromDate: today)
return NSCalendar.currentCalendar().dateFromComponents(comps)!
}
Note that this forcibly unwraps the date on the last line, but this is fine here, since I do nothing that could lead to dateFromComponents returning nil.
The next two lines you will always call right after you created your quote:
let cleanToday = makeCleanToday()
NSUserDefaults.standardUserDefaults().setObject(cleanToday, forKey: "MyAppDateKey")
Obviously you should use a better key to identify this (I suggest defining a constant somewhere for this). This saves your date (only day, month and year) in the app's user defaults.
Next time your app goes into the foreground (use the app delegate for this), then do this here:
if let savedDate:NSDate = NSUserDefaults.standardUserDefaults().objectForKey("MyAppDateKey") as? NSDate {
let cleanToday = makeCleanToday()
if savedDate.earlierDate(cleanToday) == savedDate {
// create new sentence
NSUserDefaults.standardUserDefaults().setObject(cleanToday, forKey: "MyAppDateKey")
}
} else {
// create new sentence
NSUserDefaults.standardUserDefaults().setObject(makeCleanToday(), forKey: "MyAppDateKey")
}
Please note that I added the two NSUserDefaults.standardUserDefaults().setObject.... lines here just to illustrate what happens. You will probably have that code already in your quote creation method, like I told you above to do. Also, the else part of this is for when you run the app the first time. Then you don't have anything saved in the user defaults, so savedDateis nil and you have to create your quote as normal.
Lastly a general remark: Using NSCalendar is relatively expensive (though it is designed for this scenario), so you shouldn't use it, for example, when filling cells in a table or something (because that might negatively affect frame rate during scrolling). In this case it's perfectly fine (relatively being the keyword here), I just wanna let you know before you eventually advance with your coding to a point where you run into this. :) Same is true for date formatters.
Oh, and regarding the comment: No need to say sorry, I just wanted to let you know how things here on SO are and cushion the fall a bit when you see your question getting downvoted. I hope this could help you.

How to get String from a NSTaggedPointerString? [duplicate]

This question already has answers here:
convert NSTaggedPointerString to NSString
(5 answers)
Closed 6 years ago.
It's about a Get network request, for more information please see this pic.
I want to get the argument "pilotCode", but its value is "0xa00006e....", I want to get its real value which is "admin", please tell me what should I do?
Thank you very much.
I solved this problem just now.
just add this code:
"NSString *realStr = [NSString stringWithString:pilotCode];"

Resources