I am getting an EXEC_BAD_ACCESS when I try to display a UIAlertView within a block object. If I call the "onEventAdded" method from the "viewDidAppear" method the alert works as expected.
Thanks in advance.
- (void) addEvent:(NSDictionary *) event_data
{
NSLog(#"event_data: %# ",event_data);
// create event
EKEventStore *store = [[EKEventStore alloc] init];
[store requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
// event code ...
NSError *err;
[store saveEvent:event span:EKSpanThisEvent error:&err];
if(err == NULL)
{
[self onEventAdded];
}
}];
}
-(void) onEventAdded{
uiAlert = [[UIAlertView alloc]initWithTitle: nil
message: #"Event added to calendar."
delegate: nil
cancelButtonTitle:nil
otherButtonTitles:#"OK",nil];
[uiAlert show];
}
The completion block is probably not executed on the main thread. You can only do UI stuff (e.g. showing alert) on the main thread.
Related
I know this question is posted many times but truly i didn't get any suggestion that can solve my problem. I wasted my two days on this problem but till now i didn't get any solutions. Please give your valuable guidance.
I want to post status on Facebook.
if ([PostType isEqual:#"article"])
{
[dict setObject:PostTitle forKey:#"message"];
params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
PostTitle, #"message", nil];
if (FBSession.activeSession.isOpen) {
// Yes, we are open, so lets make a request for user details so we can get the user name.
if ([[[FBSession activeSession] permissions]indexOfObject:#"publish_actions"] == NSNotFound) {
[[FBSession activeSession] requestNewPublishPermissions:[NSArray arrayWithObjects: #"publish_actions",nil] defaultAudience:FBSessionDefaultAudienceFriends
completionHandler:^(FBSession *session,NSError *error){
[self promptUserWithAccountName];
}];
}else{
[self promptUserWithAccountName];
}
// a custom method - see below:
} else {
NSArray *permissions = [[NSArray alloc] initWithObjects:
#"publish_actions",
nil];
// OPEN Session!
[FBSession openActiveSessionWithPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session,
FBSessionState status,
NSError *error) {
// if login fails for any reason, we alert
if (error) {
// show error to user.
} else if (FB_ISSESSIONOPENWITHSTATE(status)) {
// no error, so we proceed with requesting user details of current facebook session.
[self promptUserWithAccountName]; // a custom method - see below:
}
}];
}
Custom method:
-(void)promptUserWithAccountName {
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
[FBRequestConnection startWithGraphPath:#"me/feed"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error){
if (!error) {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:#"Success"
message:#"Photo Uploaded"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[tmp show];
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Some error happened"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:#"Ok", nil];
[tmp show];
}
}];
}
}];
}
bit of a beginner here and I've been stuck on something all day!
I've got 2 Parse Tables: a User (which works perfectly for logging in etc) and a UserStats that holds variables (in this case the User's Level as the first column)
All I want to do is retrieve the User's current level and display it in a label in Xcode. I've done lots of research but I cannot work out where I've gone wrong.
I'm using this Query in my .m:
- (void) retrieveFromParse {
PFQuery query = [PFQuery queryWithClassName:#"UserStats"];
[query whereKey:#"User" equalTo:[PFUser currentUser]];
[query getFirstObjectInBackgroundWithBlock:(PFObjectobject, NSError *error)
{
if (!object)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oh No!" message:#"Could not locate value!" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alert show];
}
else
{
int Level = [[object objectForKey:#"Level"] intValue];
[_newlabel setText:[NSString stringWithFormat:#"%d", Level]];
}
}];
}
and my .h looks like this:
import <UIKit/UIKit.h>
import <Parse/Parse.h>
#interface ShowLevel : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *newlabel;
#end
Edit: I've turned on exception breakpoints and it's highlighting:
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
What is the actual error that you are getting? Also you have to do anything that has to do with UI on the main thread. The query is actually running on the background thread and you are either showing alertView or updating a label which you have to do on the main thread. Add dispatch_async(dispatch_get_main_queue(), ^{ }); to your code where you are doing UI
if (!object)
{
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Oh No!" message:#"Could not locate value!" delegate:self cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alert show];
});
}
else
{
int Level = [[object objectForKey:#"Level"] intValue];
dispatch_async(dispatch_get_main_queue(), ^{
[_newlabel setText:[NSString stringWithFormat:#"%d", Level]];
});
}
This has since been resolved. I used the .m code:
PFQuery *query = [PFQuery queryWithClassName:#"UserStats"];
PFObject *Object = [query getObjectWithId:#"fghsd7fddhf"];
level = [[Object objectForKey:#"Level"] intValue];
[levelLabel setText:[NSString stringWithFormat:#"%i", level]];
While it's only temporary since I'll need to change it to point to values without specifying objectId, this works nicely for testing. Thanks to everyone who provided help :)
How to retrieve default calendar and reminder programmatically in ios, I have one simple project which have one button ,i want click on button to access the default reminder and calendar
I have done simple code for that but it is nort working properly
following is the my sample code
#import "ViewController.h"
#import <EventKit/EventKit.h>
#interface ViewController ()
#end
#implementation ViewController
- (IBAction)btn:(id)sender {
EKEventStore *eventStore=[[EKEventStore alloc]init];
EKEvent *event =[EKEvent eventWithEventStore:eventStore];
NSDate *startDate=[[NSDate alloc]init];
NSDate *endDate =[[NSDate alloc]init];
event.title=#"Title for new event";
event.startDate=startDate;
event.endDate=endDate;
event.allDay=YES;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
// if (err == noErr){
// UIAlertView * alert = [[UIAlertView alloc]initWithTitle:#"Event create" message:#"how about that?" delegate:nil cancelButtonTitle:#"okey" otherButtonTitles:nil];
//
// [alert show];
}
When I'm running it on iphone 4s reale device on that time it show's me the following error:
2013-03-13 09:52:22.638 remind[774:907] defaultCalendarForNewEvents
failed: Error Domain=EKCADErrorDomain Code=1013 "The operation
couldn’t be completed. (EKCADErrorDomain error 1013.)"
this is my .h header file where u have to declare this below code:
uikit framework
#import <UIKit/UIKit.h>
#import <EventKitUI/EventKitUI.h>
#define ALERT_Reminder 0
#interface GaSchedulesDeWorming : UIViewController<UITableViewDataSource, UITableViewDelegate, EKEventEditViewDelegate>
{
EKEventStore *eventStore;
}
this ins my implementation file .m:
**#import <EventKitUI/EventKitUI.h>**
add these package
this below code is for open the by default calender for set the reminde in ipone
EKEventStore *eventStore=[[EKEventStore alloc]init];
EKEvent *event =[EKEvent eventWithEventStore:eventStore];
NSDate *startDate=[[NSDate alloc]init];
NSDate *endDate =[[NSDate alloc]init];
event.title=#"Title for new event";
event.startDate=startDate;
event.endDate=endDate;
event.allDay=YES;
[event setCalendar:[eventStore defaultCalendarForNewEvents]];
NSError *err;
[eventStore saveEvent:event span:EKSpanThisEvent error:&err];
eventStore=[[EKEventStore alloc]init];
__block BOOL accessGranted = NO;
if([eventStore respondsToSelector:#selector(requestAccessToEntityType:completion:)]) {
dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[eventStore requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
accessGranted = granted;
dispatch_semaphore_signal(sema);
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
} else {
accessGranted = YES;
}
if (accessGranted) {
}
EKEventEditViewController *controller = [[EKEventEditViewController alloc]init];
}
I am using FBFriendPickerViewController to load friends after user signs in. However, an empty table view is being loaded. The friends of the user from fb are not showing up.
Heres the code.
- (IBAction)inviteButtonTouchHandler:(id)sender {
if (!_friendPickerController) {
_friendPickerController = [[FBFriendPickerViewController alloc] initWithNibName:nil bundle:nil];
_friendPickerController.delegate = self;
_friendPickerController.title = #"Select friends";
_friendPickerController.allowsMultipleSelection = NO;
}
[_friendPickerController clearSelection];
[_friendPickerController loadData];
[self presentViewController:_friendPickerController animated:YES completion:nil];
}
This code is called after login which is done like this in appDelegate following the Facebook Tutorial -
- (void)openSession
{
NSArray *permissions = #[#"friends_about_me"];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
You need to add the following code in viewDidLoad method of your viewController.
if (!FBSession.activeSession.isOpen) {
// if the session is closed, then we open it here, and establish a handler for state changes
[FBSession.activeSession openWithCompletionHandler:^(FBSession *session,
FBSessionState state,
NSError *error) {
switch (state) {
case FBSessionStateClosedLoginFailed:
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
break;
default:
break;
}
}];
}
I am trying to publish on Facebook using openActiveSessionWithPublishPermissions , so if the user is not logged in he need's to first sign in and then post the message using io6 Facebook native Dialog.
What I found is I am able to login, but completion handler is not called.
Another thing I noticed, that when I click the login button again, it then calls completion handler with the following error FBSessionStateClosedLoginFailed.
I did refer to this post but still did not find a solution to my problem.
NSArray *permissions = [NSArray arrayWithObjects:#"publish_stream", nil];
[FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES completionHandler:
^(FBSession *session, FBSessionState status, NSError *error)
{
switch (status) {
case FBSessionStateOpen:
{
[FBNativeDialogs presentShareDialogModallyFrom:currentController initialText:nil image:nil url:nil handler:^(FBNativeDialogResult result, NSError *error) {}];
}
break;
default:
break;
}
}];
Make sure you implemented the handleOpenUrl method.
-(BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [[FBSession activeSession] handleOpenURL:url];
}
You should have to add this:
[FBNativeDialogs presentShareDialogModallyFrom:currentController initialText:nil
image:nil url:nil handler:^(FBNativeDialogResult result, NSError *error) {
if (result==FBNativeDialogResultCancelled) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Error"
message:#"Sending Cancelled" delegate:self cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
} else if (result==FBNativeDialogResultSucceeded) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:#"Succeed"
message:#"succeed" delegate:self cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}***}***];