Can't retrieve EKCalendars from EKEventStore in iOS7 - objective-c

I've recently found out, that I do not receive any EKCalendar objects from EKEventStore in iOS7. In iOS 6.x.x there are no problems with same code snippet. When I'm trying to access defaultCalendarForNewEvents - I do receive a single EKCalendar object (as expected).
I do request access to entity type EKEntityTypeEvent.
The snippet:
__block NSMutableArray *calendarsArray = nil;
if ([self.eventsStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
[self.eventsStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
if (granted) {
EKAuthorizationStatus status = [EKEventStore authorizationStatusForEntityType:EKEntityTypeEvent];
if (status == EKAuthorizationStatusAuthorized) {
calendarsArray = [[NSMutableArray alloc] initWithArray:[self.eventsStore calendarsForEntityType:EKEntityMaskEvent]];
}
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error!" message:#"You haven't granted access to calendars. Expected things are not going to happen." delegate:nil cancelButtonTitle:#"I understand" otherButtonTitles:nil];
[alert show];
}
}];
} else {
calendarsArray = [NSMutableArray arrayWithArray:[self.eventsStore calendarsForEntityType:EKEntityTypeEvent]];
}
I do receive 0 objects into calendarsArray. I've also tried to get it by "running through" all EKSources that are of type Local or CalDAV ([source calendarsForEntityType:] - got the same empty (0 object containing) set).
By the way - access to calendars IS granted.
Any suggestions?

After a brief investigation I have found out, that the problem was not in the code. It appears that the problem is in iOS 7.0.3 itself.
After deleting all the sync'ed calendars from the iDevice and adding it back all of the calendars were displayed both within the native Calendar application, and the one I made. After taking this action my code was able to retrieve the calendars from EventStore not depending on the method I would access calendars (via EKSources or EKEventStore itself).

Related

location manager working on Always but not When In Use

I have an app that worked great before Apple insisted users be given a choice between "Always" and "When In Use" for Location Manager.
The app used iBeacons to send invitations to play games and accept.
When "Always" is selected the beacons work fine but when I switch to "When In Use" they don't work at all.
I started out using "Always" but change the following code to give users the choice.
In the app's plist I added "Privacy-Location Always and When In Use Usage Descriptions and Privacy-Location When In Use Usage Description" and removed the "Privacy-Location Always Usage Description".
In the app's Delegate I have this
- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedAlways){
NSLog(#"Always");
AlertView2 = [[UIAlertView alloc] initWithTitle:#"Dual player on two devices is enabled."
message:#"To save battery power go to Settings/Privacy/Location Services and choose \"Never\" when not using I'M GAME. Two people can still play on one device when in \"Never\" mode. To recieve invitations to play only when the app is open select \"When In Use\"."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[AlertView2 show];
[[NSUserDefaults standardUserDefaults] setObject:#"YES" forKey:#"accessKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusAuthorizedWhenInUse){
NSLog(#"WhenInUse");
AlertView2 = [[UIAlertView alloc] initWithTitle:#"Dual player on two devices is enabled."
message:#"To save battery power go to Settings/Privacy/Location Services and choose \"Never\" when not using I'M GAME. Two people can still play on one device when in \"Never\" mode. To recieve invitations to play while app is in background select \"Always\"."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[AlertView2 show];
[[NSUserDefaults standardUserDefaults] setObject:#"YES" forKey:#"accessKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusRestricted){
NSLog(#"restricted");
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
NSLog(#"denied");
AlertView2 = [[UIAlertView alloc] initWithTitle:#"Dual player on a single device Only."
message:#"To play on two devices go to Settings Privacy/Location Services and choose \"Always\" or \"When In Use\" for I'M GAME. In \"Always\" you can recieve invites while app is in background, in \"When In Use\" invites only appear when the app is on screen. To preserve battery choose \"Never\" when not using I'M GAME."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[AlertView2 show];
[[NSUserDefaults standardUserDefaults] setObject:#"YES" forKey:#"accessKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
NSLog(#"undetermined2");
[locationManager requestAlwaysAuthorization];
[locationManager requestWhenInUseAuthorization];
}
}
Does iBeacon need to have Privacy-Location set to "Always" to work?
So I just found out that in "When In Use" you can't monitor for beacon region is entered or exited only find its range. So I guess the question is how would I use range to send a notification to my user.
When you have your app authorized to do beacon ranging only when it is in the foreground, it's easy to simulate entry/exit logic with just a didRangeBeacons callback.
Set up one class variable:
var beaconLastSeen: Date? = nil
Add this code to your didRangeBeacons method:
if beacons.count > 0 {
if beaconLastSeen == nil {
// call didEnterRegion logic here
}
beaconLastSeen = Date()
}
else {
if beconLastSeen != nil && Date() - beaconLastSeen > 30 {
beaconLastSeen = nil
// call didExitRegion logic here
}
}
You will get an exit event 30 secs after the last beacon detection. You will get an enter event when one is first seen.
EDIT: Here's the same code in Objective C:
NSDate *beaconLastSeen = nil;
...
if (beacons.count > 0) {
if (beaconLastSeen == nil) {
// call didEnterRegion logic here
}
beaconLastSeen = [[NSDate alloc] init];
}
else {
if (beaconLastSeen != nil && [[[NSDate alloc] init] timeIntervalSinceDate: beaconLastSeen] > 30 ) {
beaconLastSeen = nil;
// call didExitRegion logic here
}
}

How to get the name/id of default calendar in iOS 6

I have two (2) calendars (iCal) on my iPad (one personal, one for the app). They are sync'd to my iMac for testing only. (Saves me time making entries to the specific app calendar).
I am currently writing an app that needs to access the app's calendar. It is the primary calendar on the iPad. I am trying to get Apple's SimpleEKDemo (unmodified) to work with the app's calendar, but so far I can't even get it not crash, much less to return anything. I have been looking at Google and SO questions for hours now, and decided it's time to call in the big guns.
This is the code where it's crashing:
- (void)viewDidLoad
{
self.title = #"Events List";
// Initialize an event store object with the init method. Initilize the array for events.
self.eventStore = [[EKEventStore alloc] init];
self.eventsList = [[NSMutableArray alloc] initWithArray:0];
// Get the default calendar from store.
self.defaultCalendar = [self.eventStore defaultCalendarForNewEvents]; // <---- crashes here --------
// Create an Add button
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemAdd target:self action:#selector(addEvent:)];
self.navigationItem.rightBarButtonItem = addButtonItem;
[addButtonItem release];
self.navigationController.delegate = self;
// Fetch today's event on selected calendar and put them into the eventsList array
[self.eventsList addObjectsFromArray:[self fetchEventsForToday]];
[self.tableView reloadData];
}
This is the output from the "crash":
2012-10-05 14:33:12.555 SimpleEKDemo[874:907] defaultCalendarForNewEvents failed: Error Domain=EKCADErrorDomain Code=1013 "The operation couldn’t be completed. (EKCADErrorDomain error 1013.)"
I need to make sure I'm on the correct calendar... how do I do that?
You need to ensure you ask permission before trying to access the Event Store. Note that you need to only call this once. If the user denies access, they need to go to iOS Settings (see comment in code) to enable permissions for your app.
/* iOS 6 requires the user grant your application access to the Event Stores */
if ([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)])
{
/* iOS Settings > Privacy > Calendars > MY APP > ENABLE | DISABLE */
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error)
{
if ( granted )
{
NSLog(#"User has granted permission!");
}
else
{
NSLog(#"User has not granted permission!");
}
}];
}
In iOS 5, you are only allowed to access Events (EKEntityTypeEvent) in the Event Store, unlike in iOS 6, where you can access Reminders (EKEntityTypeReminder). But you need the above code to at least get granted 1 time.
I should also mention that you need to be granted permission BEFORE you access the EventStore, in your case: [self.eventStore defaultCalendarForNewEvents];.
Also, defaultCalendarForNewEvents would be the correct way to access the users Default Calendar. If you wish to access a calendar with another name, then you need to iterate through the calendars and choose the appropriate one based on the results returned.
//Check if iOS6 or later is installed on user's device *********
if([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
//Request the access to the Calendar
[eventStore requestAccessToEntityType:EKEntityTypeReminder completion:^(BOOL granted,NSError* error){
//Access not granted-------------
if(!granted){
NSString *message = #"Hey! I Can't access your Calendar... check your privacy settings to let me in!";
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Warning"
message:message
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil,nil];
//Show an alert message!
//UIKit needs every change to be done in the main queue
dispatch_async(dispatch_get_main_queue(), ^{[alertView show];});
//Access granted------------------
}
else
{
self.defaultCalendar=[self.eventStore defaultCalendarForNewEvents];
}
}];
}
//Device prior to iOS 6.0 *********************************************
else{
self.defaultCalendar=[self.eventStore defaultCalendarForNewEvents];
}

Access Facebook Accounts on OS X Mountain Lion

As Mountain Lion now officialy has Facebook integrated I wanted to play a little bit with Accounts.framework and Social.framework of OS X.
I set up an App in the Facebook Developer App and used the following code to request access to the Facebook Accounts of the logged in user:
ACAccountStore *acStore = [[ACAccountStore alloc] init];
ACAccountType *accType = [acStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:FB_APP_ID, ACFacebookAppIdKey, #"read_stream, publish_stream", ACFacebookPermissionsKey, ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
[acStore requestAccessToAccountsWithType:accType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(#"Accounts: %#", acStore.accounts);
NSLog(#"Access granted");
} else {
NSLog(#"Access denied");
}
}];
Now I always get the error Error Domain=XPCObjectsErrorDomain Code=2 "The operation couldn’t be completed. (XPCObjectsErrorDomain error 2.)"
When I launched the App from Xcode the first 2-3 times, OS X displayed the confirmation dialog "XYZ wants to access your Facebook Accounts", I clicked ok and I got an error saying something like "The Application is not yet installed". This error now "disappeared" and I'm getting the error noted above.
My question is: Do I have to configure something special in my Xcode Project (Info.plist) or in the Facebook Developer App to make it work with OS X? Or am I missing something in the code? Samples and Tutorials regarding this seem to be very rare :-/
I already watched the WWDC 2012 Video about the Twitter/Facebook Integration but it uses almost exactly the code I used.
Any help is appreciated :-)
Cheers,
Björn
Update
I solved the issue, I just did not read the docs carefully enough ;-) I have to pass the permissions as an NSArray and not as a string. I've modified the options dictionary to look like this:
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
FB_APP_ID, ACFacebookAppIdKey,
[NSArray arrayWithObjects:#"read_stream", #"publish_stream", #"email", #"user_likes", nil], ACFacebookPermissionsKey,
ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
Now OS X shows an alert that asks me if I want to allow the App to post to Facebook on my behalf, I click OK and a new error appears resulting in no access to the Facebook accounts:
Error Domain=com.apple.accounts Code=7 "The Facebook server could not fulfill this access request: The app must ask for a basic read permission at install time."
I already modified the permissions to be just email or just user_likes but it all resulted in the same error. If I only request access to read_stream the access gets granted but results in the OAuth error 190 with the message Error validating access token: User XXXXXX has not authorized application XXXXXX.
Update 2:
I now solved all my problems, see my answer below.
Alright, I finally solved it :-)
When you first request access to a users Facebook accounts, you may only request permissions from the user listed under "User and Friend permissions" in Facebook's Permission Reference.
Once the user granted access and with that also installed the application on Facebook, you may request any additional permissions required by your App. It's important that you may not ask for read and write permissions at the same time, one after the other! So requesting the permission read_stream and publish_stream in the same request will not work.
This is the basically the code I used:
self.store = [[ACAccountStore alloc] init];
ACAccountType *accType = [self.store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:
FB_APP_ID, ACFacebookAppIdKey,
[NSArray arrayWithObjects:#"email", #"user_about_me", #"user_likes", nil], ACFacebookPermissionsKey, ACFacebookAudienceFriends, ACFacebookAudienceKey, nil];
[self.store requestAccessToAccountsWithType:accType options:options completion:^(BOOL granted, NSError *error) {
if (granted && error == nil) {
self.accounts = self.store.accounts;
[options setObject:[NSArray arrayWithObjects:#"read_stream, read_mailbox, read_requests", nil] forKey:ACFacebookPermissionsKey];
[self.store requestAccessToAccountsWithType:accType options:options completion:^(BOOL granted, NSError *error) {
if (granted && error == nil) {
completionHandler(granted, error);
} else {
NSLog(#"Access denied 2");
NSLog(#"%#", [error description]);
}
}];
} else {
NSLog(#"Error: %#", [error description]);
NSLog(#"Access denied");
}
}];
Hope this will help somebody :-)
I would like to add that there is an additional caveat in the Facebook docs:
// if a user has *never* logged into your app, you MUST include one of
// "email", "user_location", or "user_birthday". Other read
// permissions can also be included here.
Failure to do this leads to the same error.

Filter the friend returned by FBFriendPickerViewController in new Facebook 3.0

I have an Facebook app on http://developer.facebook.com of type "Native iOS" which is connected to the actual iOS app using the new Facebook iOS SDK 3.0.
How can I know if the multiple people that already install the the app and authorize the Facebook app too so I can put them in some list, but this not my question because I know that there is a field called "installed" that return true when the user install the app.
So if I want to filter the returned friend by the FBFriendPickerViewController based on if the user use the app, so how I can use the installed field or something else to filter the friends.
Note: I know where to filter the friend (*) all I need is the field or the property that I need to check to make sure the user is install my app.
*- (BOOL)friendPickerViewController:(FBFriendPickerViewController *)friendPicker
shouldIncludeUser:(id<FBGraphUser>)user
I found a way to do this :
I create a method that send an FQL to those I need (for example my friends that they are male)
and then compare those returned by this request with the already exists by the FBFriendPickerViewController using the method :
- (BOOL)friendPickerViewController:(FBFriendPickerViewController *)friendPicker shouldIncludeUser:(id<FBGraphUser>)user
for the request :
NSDictionary *queryParam = [NSDictionary dictionaryWithObjectsAndKeys:#"query", #"q", nil];
[FBRequestConnection startWithGraphPath:#"/fql" parameters:queryParam HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oops" message:#"Some thing go wrong !, Try again" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else
{
NSArray *friendInfo = (NSArray *) [result objectForKey:#"data"];
for(int i = 0; i < [friendInfo count]; i++)
{
[self.friendsNames addObject:[[friendInfo objectAtIndex:i] objectForKey:#"name"]];
}
}
}];
As you see I have an NSArray that store the filtered friend, the next step is to compare them in the delegate method above.
Hope you find it useful.

How to: Implement server forcing disconnect of client in GKSession

I am implementing the GKSession server/client mode operation in my application on iOS. I found one question related to mine but with no answer. I am trying to allow the server to disconnect any client that is currently connected to the session. I thought that calling disconnectPeerFromAllPeers:(NSString *)peerID would allow me to do this, but is seems to have no effect.
Any suggestions?
Thanks
Actually answered via question update ion 01/03/2012, but moved this text to the answer section
I wanted to share how I implemented a disconnect request sent from server to client. All of the code presented below is contained within a class I created to completely encapsulate all the interfacing with a GKSession instance (also implements the GKSessionDelegate methods).
First I have the server send a disconnect request to the client that shall be disconneted. Any data that is sent from client to server or vice versa is contained within a dictionary that also has a key-value pair to specify the type of data that is sent (in this case the data is a disconnect request).
- (void)sendDisconnectRequestToPeer:(NSString *)peer {
//create the data dictionary that includes the disconnect value for the data type key
NSMutableDictionary *dictPrvw = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithInt:GKSessionDataTypeDisconnect], kDictKeyDataType, nil];
NSData *dataChunk = [[NSKeyedArchiver archivedDataWithRootObject:dictPrvw] retain];
//[self printDict:dictPrvw];
NSArray *peers = [[NSArray alloc] initWithObjects:peer, nil];
[self sendData:dataChunk toPeers:peers];
[dataChunk release];
[dictPrvw release];
}
The client receives the data, casts it into a dictionary and examines the key-value pair that specifies what type of data was sent. If it's a disconnect request, my "GKSessionManager" class then implements a disconnect.
- (void)recievedAllDataChunksInSession:(GKSession *)session fromPeer:(NSString *)peer context:(void *)context {
//The chunk was packaged by the other user using an NSKeyedArchiver,
//so unpackage it here with our NSKeyedUnArchiver
NSMutableDictionary *responseDictionary = (NSMutableDictionary *)[[NSKeyedUnarchiver unarchiveObjectWithData:self.recievedPackets] mutableCopyWithZone:NULL];
//[self printDict:responseDictionary];
//get the enumerator value for the data type
NSNumber *gkSessDataType = [responseDictionary objectForKey:kDictKeyDataType];
int intDataType = [gkSessDataType intValue];
UIAlertView *anAlrtVw;
switch (intDataType) {
case GKSessionDataTypeMessageData:
[self sessionManager:self recievedDataDictionary:responseDictionary];
break;
case GKSessionDataTypePreviewRequest:
if (sess.sessionMode == GKSessionModeServer) {
[self sendMsgPreviewToPeer:peer];
}
break;
case GKSessionDataTypePreviewSend:
//[self sessionManager:self recievedDataDictionary:responseDictionary];
[self sessionManager:self connectedWithPrelimData:responseDictionary];
break;
case GKSessionDataTypeDisconnect:
anAlrtVw = [[UIAlertView alloc]
initWithTitle:nil
message:#"The server has disconnect you."
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[anAlrtVw show];
[anAlrtVw release];
[self closeSession];
[self disconnectedByServer];
default:
break;
}
}
- (void)closeSession {
[sess disconnectFromAllPeers];
[sess setDataReceiveHandler: nil withContext: NULL];
sess.available = NO;
sess.delegate = nil;
self.sess = nil;
self.serverId = nil;
self.rqstPeerId = nil;
serverIsConnecting = NO;
}
The user never sees the disconnect request and so has no control over whether or not to deny it.
Hope this information helps. I realize what I wrote my not be entirely clear and I have left a lot of other code out (on purpose) so feel free to comment or ask questions.