How to check if Location Services is On or not? - iphone

How can I check if the user has turned off Location Services ?
So that I can prompt him/her to turn it On in order to use my app.
Thank you !

The CLLocationManager provides class methods to determine the availability of location services:
- (BOOL)locationServicesEnabled (for < iOS 4.0)
+ (BOOL)locationServicesEnabled (for iOS 4.0 and greater)
+ (CLAuthorizationStatus)authorizationStatus (for iOS 4.2+)
(and others, see documentation)

If your application absolutely cannot run without Location Services, then you can make Location Services a requirement for installing/running your app using the app's Info.plist. You do this by adding the UIDeviceCapabilities key to your app's Info.plist and giving it a corresponding value of "location-services" minus the quotes.
With the Info.plist configured this way, if Location Services are turned off, or if the device is in airplane mode, or anything else is preventing the use of Location Services on the device, iOS will prompt the user to turn on Location Services when the app is opened.
EDIT: Brief experimentation seems to indicate that iOS does not prompt the user in this circumstance, so this would not be a good solution for you.
For more information, you can look at the Information Property List Key Reference section of Apple's developer documentation.

Use the below piece of code...
if (![CLLocationManager locationServicesEnabled]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Location Service Disabled"
message:#"To re-enable, please go to Settings and turn on Location Service for this app."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}

Use the following code, which will work even in iOS 8.
if([CLLocationManager locationServicesEnabled]&&
[CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
//...Location service is enabled
}
else
{
//...Location service is disabled
}

Related

Facebook sharing on app doesn't work

I use social framework for Facebook and Twitter sharing integration into app. On the iPad Air iOS 8.1 it doesn't work for Facebook.
-(IBAction)Facebook:(id)sender{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *FACEBOOK = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[FACEBOOK setInitialText:[NSString stringWithFormat:#"Wow, look at my new high score!"];
[self presentViewController:FACEBOOK animated:YES completion:nil];
}
else{
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:#"FaceBook Signed Out" message:#"Add or create Facebook account in Settings." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
For the above iPad Air iOS 8.1, nothing at all appears in shared message.
However, for iPad non-retina iOS 8.1.2, the above code works fine and message appears in shared message.
What's going on? Even the signed out message doesn't appear as I stated in code for iPad Air iOS 8.1. Why isn't it working on all iOS 8's or is this an iPad Retina issue?
Any form is prefilling is not allowed, as stated in the policy. Previously it was still possible using setInitialText, but recently this has been also been enforced on the software side. Any values provided here are not used in the dialog.
In this case, it's actually a bug (or limitation of the platform) that it is still working on the non-retine iOS 8.1.2 iPad. Please don't rely on this; in a next update this might also become enforced through software.

How to implement twitter login functionality on iPhone

I've already tried to use Facebook login functionality. During the login I got user token and using this token I could identify user. So, Facebook is OK for me.
1) How can I implement login functionality with Twitter? Will I get anything the same like user token on Facebook?
As far as I know, to use twitter API user should be login in twitter in the settings of iPhone. Can I implement anything to enable user to login even he is not yet logged in in the settings of iPhone?
2) After user login I want him to invite his twitter friends to my application. Is it possible to do? And in what ways?
I will be grateful for some code examples.
Please check the link below each and every step is clearly told by Twitter itself.
https://dev.twitter.com/docs/ios
If you still having problem please follow the link
http://www.raywenderlich.com/5519/beginning-twitter-in-ios-5
That contains the code and all steps !
Hope it will help you !
Please go through this link
it will provide stepwise details.
add
#import <Twitter/Twitter.h>
Then you can tweet something using:
-(IBAction)tweetTapped:(id)sender {
{
if ([TWTweetComposeViewController canSendTweet])
{
TWTweetComposeViewController *tweetSheet =
[[TWTweetComposeViewController alloc] init];
[tweetSheet setInitialText:
#"Tweeting from iOS 5 By Tutorials! :)"];
[self presentModalViewController:tweetSheet animated:YES];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You can't send a tweet right now, make sure
your device has an internet connection and you have
at least one Twitter account setup"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}

How to turn ON location services through Code

How to turn ON location services through Code in my application if location service is in OFF condition i want it to ON through alertView i have two buttons in alertview cancel & settings
if i click settings it needs to go settings of the device is it possible???
Could anyone help me??
thanks...........
You can't enable location services in code. You just invoke the service, and iOS will seek permission from the user on behalf of your app.
I've had to deal with the same problem but this is just not possible under iOS. You have to check if location services is enabled :
[CLLocationManager locationServicesEnabled];
and if not, ask the user to go to the settings to enable it (through an AlertView for example).
Note that Apple says :
locationServicesEnabled : You should check the return value of this
method before starting location updates to determine if the user has
location services enabled for the current device. If this method
returns NO and you start location updates anyway, the Core Location
framework prompts the user with a confirmation panel asking whether
location services should be reenabled.
But that only seem to work the very first time you check. If the user denies, the confirmation panel is not shown anymore.
You may also check :
switch ([CLLocationManager authorizationStatus]) {
case kCLAuthorizationStatusDenied:
case kCLAuthorizationStatusRestricted:
case kCLAuthorizationStatusNotDetermined:
}

How do I design and implement password authentication for IOS?

I have what seems to be a simple issue but I can't find a solution. I have spent hours trying to find an example that shows how to build a view, allow a user to enter a password, authenticate the password and return the view to a designated view. I have found a lot a great resources on bits and pieces, but they all are somewhat vague on how to authenticate the password and then send the user to a designated view.
This seems to be the best solution to my problem, which is: I have several pdfs that I need to password protect in my iPad app.
Issue1: Prompt for userName and Password
Present a view modally. if username/password combination is correct, dismiss the modal view. alternatively, you can use an alert with textfields.
Issue2: Store username/password in a secure way
Use key chain as suggested in the other answer. USage of key chain is as simple as using NSUserDefaults with Carlbrown's PDKeychainBindingsController. You can find it at the below link
https://github.com/carlbrown/PDKeychainBindingsController
EDITED to add info requested in comment:
Assuming you are using a custom view controller for login prompt, you have to do something like this when you want to prompt for password. it can be in your application didFinishLaunchingWithOptions.
LoginViewController *controller = [LoginViewController alloc];
[self presentModalViewController:controller animated:YES];
[controller release];
Then in your LoginViewController, you have to do something like this.
PDKeychainBindings *keyChain =[PDKeychainBindings sharedKeychainBindings];
if ([[keyChain objectForKey:#"User Name"] isEqualToString:userName] && [[keyChain objectForKey:#"Password"] isEqualToString:passWord] ) {
[self dismissModalViewControllerAnimated:YES];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"ERROR" message:#"Wrong User Name and/or Password\nEnter Login Information Again"
delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
Please note that the strings userName and PassWord are captured from your textfields in login view controller.
In anticipation of your answer to my comment--
If you want the password to be stored locally, I would store it (hashed or otherwise) in the Keychain, to compare against the user-entered password. More in the Apple docs here.
I'd recommend downloading the Apple sample project for an example of storing/retrieving data from the keychain.
Oliver over at Cocoanetics.com has a very nice implementation of the security login screen apple uses. Look at the PinLockController class in MyAppSales.
https://github.com/Cocoanetics/MyAppSales
For keychain storage I use SFHFKeychainUtils. Its a simple method call to store and retrieve secure passwords. But I think standard NSUserDefaults would be sufficient for your needs.
(handy site http://gorgando.com/blog/tag/sfhfkeychainutils)
However, if you want to sell that app, keep in mind, that apple says
The user can set a four-digit personal identification number (PIN) to
prevent unauthorized use of the device, so there is no need for the
user to be authenticated and there are no authentication or
authorization APIs in iPhone OS.
This can be found in the Security Coding How-To's

GPS location is on/off in iphone?

I want to do like, when the app start, app check for iPhone GPS ON or OFF? if GPS off then app quite.
What do you want exactly to know, location service or does user granted access to the location service?
If first, than depending on iOS version you can use:
on iOS pre 4.x:
CLLocationManager *testLocationManager = [[CLLocationManager alloc] init];
BOOL servicesEnabled = [testLocationManager locationServicesEnabled];
[testLocationManager release];
on iOS 4.x+:
BOOL servicesEnabled = [CLLocationManager locationServicesEnabled];
If second, than:
on iOS pre 4.x: there is no quick snippet, you have to wait if didFailWithError or didUpdateToLocation will be called and if second, than user granted access
on iOS 4.x+:
BOOL accessGranted = [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized;
And than easily use exit(0) if you option not reached. And yes, try notify user about what you is about to quit the app.
You need to create a CLLocationManager and ask it for locationServicesEnabled. This will tell you if location services are available.
Note however that Apple does not allow apps to quit on their own, the user needs to press the home button instead. If you discover location services are unavailable, you need to tell the user that your app needs them and they should be activated for your app to work properly.