migrating to AFNetworking 3.0 in objective C - objective-c

The code if for checking login data with mysql database using php. the code was working with AFNetworking 2.0 but not I'm migrating to AFNetworking 3.0.
I modified the code according to the GitHub documentation.
however I got this error!
if I remove this code, the errors goes away!
NSURL *URL = [NSURL URLWithString:stringURL];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject)
{
// dismiss uiviewController after data loading is done
[self dismissViewControllerAnimated:NO completion:nil];
//NSLog(#"success");
NSDictionary *temp = [[NSDictionary alloc]init ];
temp = (NSDictionary *)responseObject;
if(temp.count > 0)
{
NSDictionary *TeacherInfo = responseObject[0];
TeacherName = TeacherInfo[#"Teacher_Name"];
TeacherID = TeacherInfo[#"Teacher_NO"];
IsUserAdmin = TeacherInfo[#"IsAdmin"];
TeacherSubject1 = TeacherInfo[#"Subject1No"];
TeacherSubject2 = TeacherInfo[#"Subject2No"];
TeacherSubject3 = TeacherInfo[#"Subject3No"];
TeacherSubject4 = TeacherInfo[#"Subject4No"];
// ViewController *Navigate = (ViewController *)
// [self.storyboard instantiateViewControllerWithIdentifier:#"ChoicesViewController"];;
// [self presentViewController:Navigate animated:YES completion:nil];
}
else
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"wrong user name or password"
message:#"please enter correct data"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}
}
// if failed to connect
failure:^(NSURLSessionTask *operation, NSError *error)
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:#"connection error"
message:#"" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:#"OK" style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {}];
[alert addAction:defaultAction];
[self presentViewController:alert animated:YES completion:nil];
}];

It seems you don't use dependency manager like CocoaPods. Probably you add AFNetworking library manually. You must check your target name in
Show the File in inspector for the .m file of AFHTTPSessionManager.

Related

App crashes when mail account is not setup

App is randomly crashing when mail account is not setup. Sometimes, I am getting error alert. Please help me.
if ([MFMailComposeViewController canSendMail]) {
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc addAttachmentData:myData mimeType:#"application/pdf" fileName:#"myPdf2.pdf"];
[self presentViewController:mc animated:YES completion:NULL];
}
else {
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:#"Alert"
message:#"Please set up your mail account!"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* okButton = [UIAlertAction
actionWithTitle:#"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
//Handle your yes please button action here
}];
[alert addAction:okButton];
[self presentViewController:alert 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?

Add Item in Firebase Database 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

how to select multiple file using UIDocumentPicker in IOS

Through this only one file can be selected
- (void)Choose:(UIButton *)sender {
NSArray *arry = [[NSArray alloc] initWithObjects:#"public.data", nil];
UIDocumentPickerViewController *pickDoc = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:arry inMode:UIDocumentPickerModeImport];
pickDoc.delegate = self;
pickDoc.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:pickDoc animated:YES completion:nil];
}
-(void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
if (controller.documentPickerMode == UIDocumentPickerModeImport) {
NSString *alertMessage = [NSString stringWithFormat:#"Successfully imported %#", [url lastPathComponent]];
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:#"Import"
message:alertMessage
preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:#"Ok" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
});
}
}

iPad crashes when showing photos or taking photo--nil source view

The follow code crashes when run on an iPad with the error message:
(<UIPopoverPresentationController: 0x1377a19e0>) should have a non-nil
sourceView or barButtonItem set before the presentation occurs.'
However, in my code I have the following, and I checked with breakpoints and console prinouts to ensure sourceview and barbutton are not nil. What do I need to do to avoid this? The error only appears on an iPad. Here is the method that causes trouble:
- (void)showChooseImageOptions {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *chooseAction = [UIAlertAction actionWithTitle:#"Choose From Photos" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
[self showLibrary];
}];
UIAlertAction *takeAction = [UIAlertAction actionWithTitle:#"Take Photo" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
[self showCamera];
}];
self.directionsTextView.text=#"";
[alertController addAction:chooseAction];
[alertController addAction:takeAction];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
if (self.imageView.image) {
self.directionsTextView.text=#"Tap the circle to change the image";
} else{
self.directionsTextView.text=#"Tap the blue circle to choose or take an image";
}
}];
[alertController addAction:cancelAction];
alertController.view.tintColor=[ColorSuperclass returnApplicationMainColor];
alertController.popoverPresentationController.barButtonItem = self.navigationItem.rightBarButtonItem;
if ( [alertController respondsToSelector:#selector(popoverPresentationController)] ) {
// at least iOS8
alertController.popoverPresentationController.sourceView = self.view;
}
[self presentViewController:alertController animated:YES completion:nil];
}
And the method showCamera and showLibrary are nothing but:
-(void)showCamera {
if (TARGET_IPHONE_SIMULATOR) {
//do nothing, the simulator cannot handle pressing the take photos...
} else{
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypeCamera];
}
}
- (void)showLibrary {
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
and the show source type method:
- (void)showImagePickerForSourceType:(UIImagePickerControllerSourceType)sourceType {
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.modalPresentationStyle = UIModalPresentationPopover; //this line is very important, because otherwise, the tab bar could go out of scope (a consequence of using modal segues and tab controllers!)
imagePickerController.sourceType = sourceType;
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
[self presentViewController:imagePickerController animated:YES completion:nil];
}