Add Item in Firebase Database Objective-C - objective-c

I want to Add something into Firebase Database, but it does not work.
In My ViewController.m, I think a problem from here :
- (IBAction)addButtonDidTouch:(id)sender {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Grocery Item" message:#"Add an Item" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *saveAction = [UIAlertAction actionWithTitle:#"Save" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UITextField *textField = alert.textFields[0];
GroceryItem *groceryItem = [[GroceryItem alloc]initWithName:textField.text byUser:self.user.email completed:false withKey:#""];
Firebase *groceryItemRef = [[Firebase alloc ]childByAppendingPath:textField.text.lowercaseString];
[groceryItemRef setValue:groceryItem.toAnyObject];
}];
UIAlertAction *cancelAlert = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(#"You was cancel");
}];
[alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
[alert addAction:saveAction];
[alert addAction:cancelAlert];
}];
[self presentViewController:alert animated:true completion:nil]; }
When Tap Button Add and type text , then I tap SAVE..it not work
Please watch my images

Related

Understanding decidePolicyForNavigationResponse with user interaction

I would like to get user approval before opening a link using WKWebView. (Objective C) I am using decidePolicyForNaviagationResponse.
When it encounters a link in the HTML it should ask using a UIAlertController if it is OK to follow the link or not (in the simplest implementation).
However it appears to be running asynchronously, so first it opens the link and then eventually gets around to popping up the alert.
How do I encounter the link, pop up the alert and THEN either open the link or not. I’m guessing either something about blocks that I don’t understand like a completion handler or perhaps using semaphores, although my modest attempts at them didn’t work.
I have simplified the code to make it clear what’s happening.
Thank you!
static bool launchPermission = false;
#property (strong, nonatomic) WKWebViewConfiguration *theConfiguration;
#property (strong, nonatomic) WKWebView *webView;
.
.
.
_webView.navigationDelegate = self;
[_webView loadRequest:nsrequest];
[self.view addSubview:_webView];
.
.
.
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
[self askPermissionForExternalLink];
if (launchPermission)
{
decisionHandler(WKNavigationResponsePolicyAllow);
}
else
{
decisionHandler(WKNavigationResponsePolicyCancel);
}
}
- (void) askPermissionForExternalLink
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Open external Web Conten?" message:#"Link is embedded with other content" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
[self cancelMethod];
//return;
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
//[self launchURL];
[self OKMethod];
}];
[alert addAction:cancelAction];
[alert addAction:okAction];
[alert show];
}
- (bool) cancelMethod
{
launchPermission = false;
return false;
}
- (bool) OKMethod
{
launchPermission = true;
return true;
}
You can try it in this way using blocks first in decidePolicyForNavigationResponse make decideAction inside the block
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
void (^ launchPermission)(BOOL) = ^(BOOL isAllow)
{
if(isAllow)
{
decisionHandler(WKNavigationActionPolicyAllow);
}
else
{
decisionHandler(WKNavigationActionPolicyCancel);
}
return;
};
[self askPermissionForExternalLink];
}
here based on user choice send YES or NO to launchPermission block
- (void) askPermissionForExternalLink
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Open external Web Conten?" message:#"Link is embedded with other content" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
launchPermission(NO);
return;
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
launchPermission(YES);
return ;
}];
[alert addAction:cancelAction];
[alert addAction:okAction];
[alert show];
}

How can I go back to the previous view controller?

When user click "OK" button from UIAlertController popup, it will go back to the previous view controller.I got stuck how to that.Below this is my code.
if (jsonData == nil){
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Error!" message:#"This Git repository is empty" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}
Try this -
if (jsonData == nil){
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Error!" message:#"This Git repository is empty" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* ok = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action){
// Ok action example
[self.navigationController popViewControllerAnimated:YES];
}];
[alertController addAction:ok];
[self presentViewController:alertController animated:YES completion:nil];
}

Why when using GCD, UIAlertController display is delayed or not shown?

I have this code, which gets called from another class like this:
// send message not to turn off machine and display network indicator
[cm displayAlert: #"Warning" andData: #"Database restore is about to begin; DO NOT turn iPad OFF during restore!" andTag:0 andViewController:self];
and this is the code:
#pragma mark - displayAlert
- (void) displayAlert: (NSString *)alertTitle andData: (NSString *) alertMessage andTag: (int) tag andViewController: vc {
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:alertTitle
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"Cancel", #"Cancel action")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction *action)
{
NSLog(#"Cancel action");
}];
UIAlertAction *okAction = [UIAlertAction
actionWithTitle:NSLocalizedString(#"OK", #"OK action")
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action)
{
NSLog(#"OK action");
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
dispatch_async(dispatch_get_main_queue(), ^{
[vc presentViewController:alertController animated:YES completion:nil];
});
}
The problem is that the presentation is either delayed or not shown at all. I used the GCD code which many, if not all, of the examples on SO show... what could be causing this delay?

UIAlertView is Deprecated how can i fix this code as UIAlertController? I can't figure out how to use switch statement in UIAlertController

I've tried Several ways to fix this deprecated code but nothing helped me I'm new to Objective C and iOS please help me fix this...It's working fine in iOS8 but not in iOS9..
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
[super alertView:alertView clickedButtonAtIndex:buttonIndex];
if (buttonIndex)
{
switch (alertView.tag)
{
case kAccessAddressBook:
{
[self displayFindFriendView:[NSNumber numberWithInteger: CS_CONTACTS ]];
}
break;
case kFindFriendEmail:
{
}
break;
case kLogout:
{
// Hit Logout API
[self userLogout];
}
break;
case kClearSearchHistory:
{
// Clear Search History Data base.
[[CSCoreDataHandler sharedInstance] deleteManagedObjectsInModel:#"CSRecentSearch"];
}
break;
default:
break;
}
}
}
-(void)alert
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"My Alert"
message:#"This is an alert."
preferredStyle:UIAlertControllerStyleAlert];
if (buttonIndex)
{
switch (alertView.tag)
{
case kAccessAddressBook:
{
defaultAction = [UIAlertAction actionWithTitle:[self displayFindFriendView:[NSNumber numberWithInteger: CS_CONTACTS ]]; style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
}
break;
case kFindFriendEmail:
{
}
break;
case kLogout:
{
// Hit Logout API
[self userLogout];
}
break;
case kClearSearchHistory:
{
// Clear Search History Data base.
[[CSCoreDataHandler sharedInstance] deleteManagedObjectsInModel:#"CSRecentSearch"];
defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
}
break;
default:
break;
}
}
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
AlertView is depricated in iOS 8.So we need to use UIAlertController.
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:#"Title"
message:#"Message"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionAccessAddressbook = [UIAlertAction
actionWithTitle:#"Access"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self displayFindFriendView:[NSNumber numberWithInteger: CS_CONTACTS ]];
}];
UIAlertAction *actionFindFriendEmail = [UIAlertAction
actionWithTitle:#"Find Friend Email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//...Do your stuff here
}];
UIAlertAction *actionLogout = [UIAlertAction
actionWithTitle:#"Logout"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[self userLogout];
}];
UIAlertAction *actionClearSearchHistory = [UIAlertAction
actionWithTitle:#"ClearSearchHistory"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
[[CSCoreDataHandler sharedInstance] deleteManagedObjectsInModel:#"CSRecentSearch"];
}];
[alert addAction:actionAccessAddressbook];
[alert addAction:actionFindFriendEmail];
[alert addAction:actionLogout];
[alert addAction:actionClearSearchHistory];
[self presentViewController:alert animated:YES completion:nil];

UIAlertController not allowing click

I am generating a UIAlertController for use in iOS8 to replace a UIActionSheet function.
The controller presents correctly, but I cannot click anywhere in the controller to get a response. I put in simple NSLog into the handler, and it is absolutely not firing.
Can anyone shed some light on this problem?
BTW, in iOS8 the UIActionSheet is ALSO not generating click events, but in iOS7 this works fine with absolutely no code change.
here is the code:
NSString *alertMessage = #"How do you wish to share ?";
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Share Image"
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
// You can add as many actions as you want
UIAlertAction *emailShare = [UIAlertAction actionWithTitle:#"Share using email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
NSLog(#"EMAIL");
}];
UIAlertAction *smShare = [UIAlertAction actionWithTitle:#"Share using twitter/facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self actionSheet:nil didDismissWithButtonIndex:1];
}];
UIAlertAction *cancelShare = [UIAlertAction actionWithTitle:#"cancel"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[self actionSheet:nil didDismissWithButtonIndex:2];
}];
// Add actions to the controller so they will appear
[alertController addAction:emailShare];
[alertController addAction:smShare];
[alertController addAction:cancelShare];
// Finally present the action
[self presentViewController:alertController animated:YES completion:nil];
You have a few different issues with your code. I've commented on the changes that are necessary. Let me know if you have any questions.
// No need to create a NSString. Just set the message.
// NSString *alertMessage = #"How do you wish to share?";
// If you'd like a UIActionSheet use UIAlertControllerStyleActionSheet as the preferredStyle
// Opposed to UIAlertControllerStyleAlert, which presents a UIAlertView
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:#"Share Image"
message:#"How do you wish to share?" // Message Set
preferredStyle:UIAlertControllerStyleActionSheet]; // Changed to show action sheet
UIAlertAction *emailShare = [UIAlertAction actionWithTitle:#"Share using email"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
[alertController dismissViewControllerAnimated:YES completion:nil];
// Call your function here
// ie. [self emailShare];
NSLog(#"First Button");
}];
UIAlertAction *smShare = [UIAlertAction actionWithTitle:#"Share using twitter/facebook"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
// [self actionSheet:nil didDismissWithButtonIndex:1];
// Where did you create actionSheet?
// didDismissWithButtonIndex: is not neccessary
// Just simply dismiss the UIAlertController
[alertController dismissViewControllerAnimated:YES completion:nil];
// And then call your function here
// ie. [self twitterShare];
NSLog(#"Second Button");
}];
UIAlertAction *cancelShare = [UIAlertAction actionWithTitle:#"Cancel"
style:UIAlertActionStyleCancel // Set style to cancel
handler:^(UIAlertAction *action) {
// [self actionSheet:nil didDismissWithButtonIndex:2];
// Where did you create actionSheet?
// didDismissWithButtonIndex: is not neccessary
// Just simply dismiss the UIAlertController
[alertController dismissViewControllerAnimated:YES completion:nil];
NSLog(#"Cancel Button");
}];
[alertController addAction:emailShare];
[alertController addAction:smShare];
[alertController addAction:cancelShare];
[self presentViewController:alertController animated:YES completion:nil];