Executing code of app without opening view for user - swift

I want to execute code on background, when user clicks on app icon? Is it possible? I want to turn bluetooth when I click on app icon on Iphone. Thanks a lot !

As per this answer, there is no way to turn on Bluetooth without Apple rejecting your app. Therefore, the best way to have the user turn on Bluetooth is to tell the user to do so(e.g. through an alert/popup).
As for executing code in the background, check out this answer, which describes how threading works in Swift.

Related

Close iPhone app from within the app

Is there a way to close the iPhone app from within the app? A client asked me if there was a way to close the app by pushing a button in the app, without manually doing it by double tapping the home button and then press-hold the app icon and then hit the - sign.
Apparently, the client wants to build an app which uses bluetooth and ANT+ devices which are battery hogs and wants the ability to quickly close the app.
Any thoughts?
I'm thinking you should just make the button dispose of the Bluetooth and other 'battery hogs'. You might even make the app smart enough to turn it off based on certain events like when the user leaves the app or when a certain function is completed.
Aside from quitting from a button within the application being against the Apple Human Interface Guidelines, it isn't even recommended behaviour.
But if you still want to close your app. Use 'exit();'.
This behavior is against Apple's Human Interface Guidelines. If you still want to do so, use the C function exit();. But for the user this will look like the application crashed, so I wouldn't do so, even not on an non-AppStore app!

passworded exit from the app

I am developing an iPhone app, that will not be distributed on AppStores. I have tried to search for private api but no luck. When the user tries to quit the app, I want to prompt the user to enter the correct password so as to enable him to exit the app. How can I achieve this? Thanks in advance.
Cheers!!
It sounds like you need something similar to iOS "Kiosk Mode" as described in this answer:
Put an iPhone or iPad into 'Store Demo' mode
Using that description in conjunction with the following steps might achieve precisely what you want:
Have a "Quit" button
Show a password dialogue when it is pressed
If it was correct, read an out of bounds array location (or do whatever) to crash the app
With that config installed you'd get stuck in whatever app you launch next, though.
You can't! You'll have to password protect the launch instead.
If you want to prevent the user from quitting your app, you'll need to physically cover the home button. This isn't a bad solution if your goal is to do something like a kiosk or a point of sale system, where you probably want to mount an iPad in a secure enclosure to prevent tampering or theft anyway. There are quite a few such enclosures on the market in a variety of styles.
For a normal app, though, there's really no solution -- iOS isn't designed for whatever you're trying to do.
The only way I could think to do what you want (which is to prevent access to certain apps) would be to replace the SpringBoard application (this is the method used by Apple itself in the App store models of the iPhone and iPad that restrict the things you can do on the devices). #owenfi pointed at one way to do that without jailbreaking, but in general it is limited: you won't be able to "exit" the app, you will basically just be able to run a single app.

Device auto-lock

I have an application in which I want to lock the iPhone programmatically so that the user can't perform any action.
How can I determine the user is not working with the iPhone for some period, and after that period automatically lock the phone?
Such applications are used in all phone devices, where the user can set an auto-lock time and then the device will automatically lock if the user does not handle the device.
How is it done programmatically? Please give me one example of the code so I can perform this easily.
I am new to iPhone programming, and I am struggling in my application. Help me. Thanks in advance.
(For your lock the iphone device through programatic only )
Impossible.
But , you could prevent your phone from locking when your app is running.
[UIApplication sharedApplication].idleTimerDisabled = YES
There is no way to disable the Home Button in iOS using the documented API. The user will always be able to get back to the home screen or close your application using the Home Button.
Even if you find a way do it (which is very unlikely) your app will be definitely rejected from the App Store for a blatant violation of the iOS Human Interface Guidelines. If I were you I would reconsider implementing this behaviour, frankly, it's just not a good idea.
If you're only looking for blocking the the UI of your app, check out the answers to this question, more specifically, check out the incredibly useful MBProgressHUD library:

How can I close an iPad app in Objective-C?

I would like to close an iPad application as a result of clicking on a UIButton. However, I have not seen how to do this in the Apple documentation.
What call needs to be made to close an app?
Thanks.
You can call exit(0) to terminate the app. But Apple don't like this as this gives the user a feeling of sudden crash. If you still want to have an exit function (with a potential risk of rejection) then you should also send your app delegate the applicationWillTerminate message (if you have anything important there) before performing the exit.
It says:
Don’t Quit Programmatically
Never quit an iOS app programmatically because people tend to
interpret this as a crash. However, if external circumstances prevent
your app from functioning as intended, you need to tell your users
about the situation and explain what they can do about it. Depending
on how severe the app malfunction is, you have two choices.
Display an attractive screen that describes the problem and suggests a
correction. A screen provides feedback that reassures users that
there’s nothing wrong with your app. It puts users in control, letting
them decide whether they want to take corrective action and continue
using your app or press the Home button and open a different app
If only some of your app's features are unavailable, display either a
screen or an alert when people use the feature. Display the alert only
when people try to access the feature that isn’t functioning.
The only way for a user to exit an application is by pressing the Home button. You can't do it in your app, at least not in a way that Apple would accept.
You can try to use command:
exit(0);

how to stop snapshot in iphone programmatically?

HI all ,
I need a functionality by which i can restrict user when he is using my app he cann't take the screenshot of the application .as he exit from the app he can take snapshot in normal mode .Is there any thing by whcih we can restrict the user to stop to take the screen shot of the application.
Thanks
Balraj
As far as I know, Apple won't let you use "hardware"-control to stop the user from the phones intended functions.
Taptaptap wanted to use the volume-control button as a shutter to their camera app. But the app got rejected. You can read more on this issue on their blog: Camera+ VolumeSnap: Rejected
Answer on the question:
No, I don't think that is possible.