FBFriendPickerViewController is loading a empty tableview intermittently - session

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;
}
}];
}

Related

Dialog like Xcode in OS X

I want to show the dialog with text input as sheet below.
I try with NSAlert but i don't want to show app icon in dialog.
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:kAppTitle];
[alert setInformativeText:kMsgSetDeviceName];
[alert addButtonWithTitle:kButtonOK];
[alert addButtonWithTitle:kButtonCancel];
NSString *deviceName = #"";
NSTextField *input = [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 300, 24)];
[input setStringValue:deviceName];
[alert setAccessoryView:input];
[alert beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger button) {
}];
You can use http://www.knowstack.com/nsalert-cocoa-objective-c/ link to create custom alert sheet in OSX.
-(void)showCustomSheet
{
{
if (!_customSheet)
//Check the myCustomSheet instance variable to make sure the custom sheet does not already exist.
[NSBundle loadNibNamed: #"CustomSheet" owner: self];
[NSApp beginSheet: self.customSheet
modalForWindow: self.window
modalDelegate: self
didEndSelector: #selector(didEndSheet:returnCode:contextInfo:)
contextInfo: nil];
// Sheet is up here.
}
}
- (IBAction)closeMyCustomSheet: (id)sender
{
[NSApp endSheet:_customSheet];
}
- (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
NSLog(#"%s",__func__);
NSLog(#"return Code %d",returnCode);
[sheet orderOut:self];
}
The below method is required to have a different point to show the alert from
- (NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet
usingRect:(NSRect)rect
{
NSLog(#"%s",__func__);
if (sheet == self.customSheet)
{
NSLog(#"if block");
NSRect fieldRect = [self.showAlertButton frame];
fieldRect.size.height = 0;
return fieldRect;
}
else
{
NSLog(#"else block");
return rect;
}
}

Error for request to endpoint 'me/feed': An open FBSession must be specified for calls to this endpoint

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];
}
}];
}
}];
}

how to check if PFUser has verified it's email - Xcode Parse

I'm trying to check if the current user has verified it's email before
they proceed to the next step. What am i doing wrong please help thank you. The phone number saving in background works.. When i call on the "SavePhoneInBackground" the app crashes
(The SVProgressHUd is the activity indicator)
-(void) SavePhoneInBackground {
PFUser *user = [PFUser currentUser];
user[#"phone"] = _phone_register.text;
[user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
NSLog(#"PhoneUpdatingSucces!");
_phone_register.text = nil;
[self checkUserEmailVerify];
}else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Whoops!" message:#"Something went wrong! Try again." delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil];
[alert show];
[SVProgressHUD dismiss];
NSLog(#"There was an error in the registration!");
}
}];
}
-(void) checkUserEmailVerify {
PFUser *user = [PFUser currentUser];
if (![[user objectForKey:#"emailVerified"] boolValue]) {
// Refresh to make sure the user did not recently verify
[user refresh];
if (![[user objectForKey:#"emailVerified"] boolValue]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Whoops!" message:#"You need to verify your emailadress!" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
return;
[SVProgressHUD dismiss];
}
}
// This is a triumph.
[SVProgressHUD dismiss];
[self performSegueWithIdentifier:#"login2" sender:self];
}
func giveCaketoUser(user: PFUser) {
if (user.objectForKey("emailVerified").boolValue == true){
println("true")
} else {
println("flase")
}
}
Here is something I used in one of my recent projects:
- (BOOL)validateEmail:(NSString *)emailStr {
NSString *emailRegex = #"[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", emailRegex];
return [emailTest evaluateWithObject:emailStr];
}
There are other manipulations of the regex you can use. When I used this method the problem I had was that the project would continue to register the user while still showing my custom alert and NSLog error, but that may have been failure on my end and not the method.
Hope this will send you down the right direction!
This is how I did it in my App and it will only let verified email users to access. You will need to modify it slightly for what you want.
- (void)logInViewController:(MyLogInViewController *)logInController didLogInUser:(PFUser *)user {
if (![[user objectForKey:#"emailVerified"] boolValue]){
NSLog(#"User not validated email");
[[[UIAlertView alloc] initWithTitle:#"Access restricted" message:#"To access this area please validate your email address" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil] show];
}
else if ([[user objectForKey:#"emailVerified"] boolValue]){
NSLog(#"Email validated");
[self dismissViewControllerAnimated:YES completion:NULL];
[[[UIAlertView alloc] initWithTitle:#"Welcome back" message:(#"%#", user.username) delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil] show];
}
// [self dismissViewControllerAnimated:YES completion:NULL];
NSLog(#"Login sucessfull and username is %#",user.username);
}
Hope this helps if you have not worked it out yet!

UIAlertView exec_bad_access form block object

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.

How to Post to Facebook on iOS 6 in Objective-C using ACAccountStore class

I want to know how to post a status message to Facebook on iOS 6 using the new frameworks on Xcode 4.5. Thanks! :)
Posting a message is rather simple. It's almost like with the Twitter Framework.
First you have to import the Frameworks: Social and Accounts
#import <Social/Social.h>
#import <Accounts/Accounts.h>
In your .h file:
SLComposeViewController *mySLComposerSheet;
This code has to be included inside your action in your .m file:
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) //check if Facebook Account is linked
{
mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social platform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#"Test",mySLComposerSheet.serviceType]]; //the message you want to post
[mySLComposerSheet addImage:yourimage]; //an image you could post
//for more instance methods, go here: https://developer.apple.com/documentation/social/slcomposeviewcontroller#//apple_ref/doc/uid/TP40012205
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successful";
break;
default:
break;
} //check if everything worked properly. Give out a message on the state.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
And what do I have to do, when I just want to receive an alert in case the post was successful, and nothing when the user cancelled the post?
And unfortunately this does not work properly for Twitter... It doesn't dismiss the TweetSheet anymore. Here is my code:
if(NSClassFromString(#"SLComposeViewController") != nil)
{
mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; //Tell him with what social plattform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:[NSString stringWithFormat:story.title,mySLComposerSheet.serviceType]]; //the message you want to post
[mySLComposerSheet addURL:[NSURL URLWithString:story.link]];
//for more instance methodes, go here:https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40012205
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = NSLocalizedStringFromTable(#"As it seems you didn't want to post to Twitter", #"ATLocalizable", #"");
break;
case SLComposeViewControllerResultDone:
output = NSLocalizedStringFromTable(#"You successfully posted to Twitter", #"ATLocalizable", #"");
break;
default:
break;
} //check if everything worked properly. Give out a message on the state.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Twitter" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
- (IBAction)btn_facebook:(id)sender {
SLComposeViewController *facebookcomposer =
[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebookcomposer setInitialText:#"This is just a test"];
[facebookcomposer addURL:[NSURL URLWithString:#"http://www.google.com"]];
[facebookcomposer addImage:[UIImage imageNamed:#"images.jpg"]];
[self presentViewController:facebookcomposer animated:YES completion:nil];
[facebookcomposer setCompletionHandler:^(SLComposeViewControllerResult result)
{
switch (result)
{
case SLComposeViewControllerResultDone:
NSLog(#"done");
break;
case SLComposeViewControllerResultCancelled:
NSLog(#"cancelled");
break;
default:
break;
}
}];
}
- (IBAction)btn_twitter:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *twitter =
[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[twitter setInitialText:#"this is just a test"];
[twitter addURL:[NSURL URLWithString:#"http://www.google.com"]];
[twitter addImage:[UIImage imageNamed:#"images.jpg"]];
[self presentViewController:twitter animated:YES completion:nil];
} else {
NSLog(#"it is not configured");
}
}
Ok guys, so I tweaked the original post, works for iOS 6/7. Change ServiceType, Alert Title and Message for Facebook. Enjoy!
- (IBAction)tweetMessage:(id)sender {
if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) //check if Facebook Account is linked
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Unable to Tweet!" message:#"Please login to Twitter in your device settings." delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
return;
}
//self.mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[self.mySLComposerSheet setInitialText:[NSString stringWithFormat:#"I found this Thing, check it out at this Place:\n %# \n", [self someplace]]];
[self.mySLComposerSheet addImage:self.photos.firstObject];
[self presentViewController:self.mySLComposerSheet animated:YES completion:nil];
//}
[self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successfull";
break;
default:
break;
} //check if everything worked properly. Give out a message on the state.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Twitter" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
}
This is how I actually use it in my app though. Smoother and lets the controller do the hard work.
- (IBAction)postToFacebook:(id)sender {
if(![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
NSLog(#"log output of your choice here");
}
// Facebook may not be available but the SLComposeViewController will handle the error for us.
self.mySLComposerSheet = [[SLComposeViewController alloc] init];
self.mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self.mySLComposerSheet setInitialText:[NSString stringWithFormat:#"I found this Thing, check it out at SomeWhere:\n %# \n", [self someURLString]]];
[self.mySLComposerSheet addImage:self.photos.firstObject]; //an image you could post
[self presentViewController:self.mySLComposerSheet animated:YES completion:nil];
[self.mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successfull";
break;
default:
break;
}
if (![output isEqualToString:#"Action Cancelled"]) {
// Only alert if the post was a success. Or not! Up to you.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
}];
}

Resources