Executing tasks while app is active and in background - ios

I need to scan a website for changes every few minutes and when difference occurs i need inform user with popout window that contains information about that change.
Problem is i can't find solution to that, i tried putting that code in applicationDidEnterBackground but it didnt work. I'm new to swift and mobile programming at all so please be understanding.
Where should i begin? Little tip maybe.
Thank you!

Related

Thread that work in background of project until the end of program

I need to write a thread that do work in background of project.
when should I create it and where , for example in AppDelegate or others?
if I want to run thread when user do a job,for example when click on a button run a thread that work in background until the end of program,for this what should I do?
In iOS, a program never ends unless it crashes or the user turns their phone off. So running "until the end of program" is a very, very long time. Users will not thank you for emptying their battery.
It is most likely that what you are asking for is by far not the best possible solution, and will be rejected if you want to put your app into the app store. You should really explain what it is that you are actually trying to achieve.
In case you just posted the wrong question, the answer is "use GCD". If you don't know what GCD is, Google and Apple's documentation are your friend.

IOS - Running background task for update user location using swift

i'm new user here, but I have used stackoverflow a lot of times :)... I'm android developer, but now i'm working in an ios app using swift. I'm totally new using xcode, maybe my question es very simple for a lot... Great, I really need help.
I'm trying to implement a backgroud task that updates the user location each ten minutes. I need to get the lat and long data and after send this info to an API web service. I have been rading the backgroud docs, all documentation, and all the documentation about user location services. There is a lot of information, pretty information, but, maybe i'm very wonted to the android information, with s lot of information too, but a lot of easy examples also, then, is very easy for me. and an previous stackoverflow questionmaybe helpme a lot.
I understand a lot of concepts, and I cofigured my info.plist like the documentation says, but... how can I run a background task for get the user location? Where I must write the background task code? What is the beginning? I have a project with three diferent viewcontrollers and I need to start the service with a button... Thank you so much, I really need to understand the start point of all this.
I am currently doing kind of the same thing as my first iOS-project - what helped me was this :
Periodic iOS background location updates
And from there on I found a Swift-Port of https://github.com/voyage11/Location: https://github.com/silento/Location. In the project from voyage11, there is also linked a blog post that a bit of clarifies what is happening and that also links to a second one that tells how to restart location services using "significant location changes". I am currently trying to implement this with "region location monitoring", but not sure if it will work.
Oh, and I moved the code that triggers a new background task out of the didUpdateLocation-part to the restartLocationUpdates-part - so you can be sure your task is restarted even if you do not get a location in time.
It did not work from the start, but when I added the code from https://stackoverflow.com/a/19085518/3085985 as a replacement for the stopUpdatingLocation() and startUpdatingLocation() - parts.
It is really more complicated than it should be and what I am used from Android - but it is how it is. If you are developing for Iphone5+ you can also consider this post (I could not test it right now as I only have an iPhone 4S and the feature is not supported in the Simulator): https://stackoverflow.com/a/24666487/3085985.
I hope you got some starting points, I am currently still testing my solution but it seems to work as far as I can see now ;)
P.S.: I somewhere read something about background tasks sleeping for more than 5 minutes being canceled - as I only wait 30 seconds I do not have this issue, but you might have. But maybe keeping location services running with a really high distance prevents this from happening.
P.P.S.: As Swift is new you should get used to reading Objective C-Code and trying to translate it to Swift. It often is the only way to find good tutorials.
One more thing - for iOS8 you need something like :
if (self.locationManager.respondsToSelector(Selector("requestWhenInUseAuthorization"))) {
// on iOS8+, we need to request the location authorization ourselves -.-
self.locationManager.requestAlwaysAuthorization()
}
And don't forget to add the "NSLocationAlwaysUsageDescription" to your Info.plist as well as allowing background location updates in your project capabilities.
Sorry for this mess of text, but it is a really complicated topic ;)
edit: Regarding your first comment :
You should create a Swift project, e.g. with a single window and in Main. storyboard add a switch. Then you should implement a ToggleActionChanged-event enabling or disabling the listeners, depending on the new state of the switch. I cannot explain all the basics, you should watch a basic tutorial, I like https://www.youtube.com/playlist?list=PL_4rJ_acBNMHa_RGZigG2nQI3aL1kTa4r and used especially the 7th video as an example of how to build a basic app. The only thing that can be made easier than shown there is using right-click-dragging from the storyboard to the code view instead of implementing some methods by hand. There is a handy assistant.
For your location event listeners you can add a new swift class and name it e.g. LocationDelegate.swift . It makes it a bit more clean. I use a Singleton-pattern for that, you can look up how to do this. Or you could use the structure of the github-project I posted above. I can't provide the complete source as you need to change it so it fits your individual needs ;)
Oh, and one more thing : To remember if your switch is enabled between application launches, you can use something like this :
#IBAction func toggleTrackingChanged(sender: UISwitch) {
// This is your method for when the switch changed... put your location activation/deactivation here
let userDefaults = NSUserDefaults.standardUserDefaults()
userDefaults.setBool(sender.on, forKey: "ODLTrackingActive")
}
}
and in your viewDidLoad-Method :
let userDefaults = NSUserDefaults.standardUserDefaults()
var trackingActive = userDefaults.boolForKey("ODLTrackingActive")
toggleTrackingSwitch.setOn(trackingActive, animated: false)
// reactivate location tracking if necessary
Good luck!

How to access Iphones call timer

this is my first time posting so if I make a mistake correct me please!
I am currently brain-storming an app, and as I have been researching part of it on here, and other places I have come to a bit of a snag. I want the app to be able to access the call timer on a iPhone (as in, it will be able to track how long you are in a call) and then store this information. As of 2011 it seems that this was either not possible, or you had to use something called CoreTelephony. I however could not find any recent material on this, so I was wondering if anything has changed in recent years.
Thanks in advance for any help!
If your trying to count/show how long someone has spent on the phone from the iphone usage data you cannont, If you are trying to show how long someone has been on the phone in your app using your phone/voip or what ever you use then you can use NSTimer

Adding code for tracking events (iOS)

I would like to add a code for a background service which may track all events (names of current View Controller, name of called method when executed, etc...). It is a little bit like analytics api but in some way different.
I have successfully programmed a background service with NSTimer which execute every 5 secondes, but I can't figure out how to detect executed actions on the device (I have tried to see console log, but nothing there !)
Could you please help me.
Thanks in advance

Calculating the launch time of apps

I would like to compare the launch time of a few apps On the app store. Obviously I do not have access to source code, so I was wondering what is the best way to get accurate results ?
Is there a way to know when the system makes a call to applicationDidFinishLaunching (without source).
Any help is appreciated
Video them starting up and look at the time codes.
Maybe fire up Instruments and look for some kind of timeline evidence with one of their profilers?

Resources