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

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!

Related

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

FBFriendPickerViewController is loading a empty tableview intermittently

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

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

Facebook ios6 completionHandler not called after signing in

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

Multiple UIAlertView; each with their own buttons and actions

Im creating a view in Xcode 4.3 and im unsure how to specify multiple UIAlertView's that have their own buttons with separate actions. Currently, my alerts have their own buttons, but the same actions. Below is my code.
-(IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.examplesite1.com"]];
}
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"examplesite2.com"]];
}
}
Thanks for any help!
There is a useful property tag for UIView(which UIAlertView subclass from). You can set different tag for each alert view.
UPDATE:
#define TAG_DEV 1
#define TAG_DONATE 2
- (IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
alert.tag = TAG_DEV;
[alert show];
}
- (IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
alert.tag = TAG_DONATE;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_DEV) { // handle the altdev
...
} else if (alertView.tag == TAG_DONATE){ // handle the donate
}
}
easier & newer
UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 1;
UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 2;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if(alertView.tag == 1) {
// first alert...
} else {
// sec alert...
}
}
all done!
If you find it dificult to use delegate methods to differently identifying alert view Then you can also use This Category class to use completion Block for each AlertView.
Alert_ActionSheetWithBlocks
For eg.
UIAlertView* alert1 = [[UIAlertView alloc] initWithTitle:#"AlertView+Block 1" message:#"WithBlocks" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert1 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView1 Stuff here }];
UIAlertView* alert2 = [[UIAlertView alloc] initWithTitle:#"AlertView+Block 2" message:#"WithBlocks" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK", nil];
[alert2 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView2 Stuff here }];
I hope this one is the more easiest way as compare to tag + delegate method..
He's right but you need to add this:
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == TAG_DEV && buttonIndex == 1) { // handle the altdev
...
} else if (alertView.tag == TAG_DONATE && buttonIndex == 1){ // handle the donate
}
}
if buttonIndex==1 then you're using the FIRST otherbutton. 0 would be for cancel. But just do nothing for 0
Or you could do this (check the title name), is just another option... Mind identically titled alerts though!
-(IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleOneGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"titleTwoGoesHere"
message:#"messageGoesHere"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Continue", nil];
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1)
{
if([[alertView title] isEqualToString:#"titleOneGoesHere"])
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.examplesite1.com"]];
}
else
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"examplesite2.com"]];
}
}

Resources