0

hi I am new to iOS and below is my code I have to pass the top that I get from request reply...please help me I am stuck here first view controller is otp view second vc is verifyviewcontroller

Otp view c.h

#import <UIKit/UIKit.h>
@interface OtpViewController : UIViewController
@property (nonatomic, retain) NSString *str;
@end

otpviewcontroller.m

#import "OtpViewController.h"
#import "VerifyViewController.h"
@interface OtpViewController () <VerifyViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *bbi;
@property (weak, nonatomic) IBOutlet UIButton *submittf;
@property (weak, nonatomic) IBOutlet UITextField *mobiletf;
@property (weak,nonatomic) NSArray *tmp;
@property(weak,nonatomic) NSString *requestReply ;
//@property(weak,nonatomic) NSDictionary *A;
@end

@implementation OtpViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
NSLog(@"viewDidLoad");
[super viewDidLoad];

}

- (IBAction)submitb:(id)sender
 {

if (_mobiletf.text && _mobiletf.text.length >0 )
{
    /* not empty - do something */
    NSString *post = [NSString stringWithFormat:@"phone=%@",_mobiletf.text];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    //  Next up, we read the postData's length, so we can pass it along in the request.

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
    // Now that we have what we'd like to post, we can create an NSMutableURLRequest, and include our postData.

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://www.sitesandflats.com/send_otp.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:postData];
    NSLog(@"the data Details is %@", post);

    //   And finally, we can send our request, and read the reply by creating a new NSURLSession:

    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
        NSLog(@"requestReply: %@", requestReply);
        NSString *str=requestReply;
        VerifyViewController *vc = [[VerifyViewController alloc] init];
        NSString  *tmp=requestReply;
        NSLog(@"%@",str);
        NSLog(@"%@",_tmp);

    }] resume];
    [ self performSegueWithIdentifier:@"b1" sender:self];
   }
else
{
    /* what ever */
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
        message:@"Please check your input!!."
        delegate:self
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
    [alert show];
}


}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender{

VerifyViewController *loadCtr = (VerifyViewController *)segue.destinationViewController;
loadCtr.delegate = self;
loadCtr.tmpStr = (@"otp: %@",_tmp);
NSLog(@"passing val: %@",_tmp);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}

Verify view controller.h

#import <UIKit/UIKit.h>
@protocol VerifyViewControllerDelegate <NSObject>
-(void)moveToA:(NSString *)str;
@end
@interface VerifyViewController : UIViewController
@property (nonatomic, copy) NSString *tmpStr;
@property (nonatomic, assign) id <VerifyViewControllerDelegate> delegate;
//@property (nonatomic, strong) OtpViewController *received;
//@property (strong, nonatomic) IBOutlet UITextField *textDisplay;
@property(nonatomic,weak) NSMutableArray *myAray;
@property(nonatomic,strong) NSString *object;
@property(strong,nonatomic) NSString *tmp1;
@end

Verify view controller.m

#import "VerifyViewController.h"
#import "OtpViewController.h"
@interface VerifyViewController ()
@property (weak, nonatomic) IBOutlet UITextField *otptf;
@property (weak, nonatomic) IBOutlet UIButton *submitb;
//@property (weak,nonatomic) textDisplay;
//@property (weak,nonatomic) NSString  *received;
@end


@implementation VerifyViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage     imageNamed:@"background"]];

// Do any additional setup after loading the view.
}
- (IBAction)submitb:(id)sender {
NSLog(@"%@",_tmpStr); 
if(_otptf.text==_tmpStr)
{
    [self performSegueWithIdentifier:@"b2" sender:self];
}
else{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                    message:@"Incorrect otp please check the input!!!."
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];

}


}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

at nslog I get reply as ("success":"1";"otp":"985123") I need this otp to store and verify in next page please help

KKRocks
  • 8,222
  • 1
  • 18
  • 84
Akshay
  • 179
  • 1
  • 16

1 Answers1

0

First you need to change property

From

@property (weak,nonatomic) NSArray *tmp;

To

 @property (strong,nonatomic) NSStirng *tmp;

Assign value to tmp as below

[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
           NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // this is json string
           NSError *error;
           NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object

            NSLog(@"requestReply: %@", jsonDict);
            self.tmp=[jsonDict valueForKey:@"otp"] ; // you need to extract value using the key   [requestReply valueForKey:@"otp"];
            NSLog(@"%@",self.tmp);

    }] resume];

Pass tmp to VerifyVIewController

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(nullable id)sender{

VerifyViewController *loadCtr = (VerifyViewController *)segue.destinationViewController;
loadCtr.delegate = self;
loadCtr.tmpStr = self.tmp;
NSLog(@"passing val: %@",_tmp);
}
KKRocks
  • 8,222
  • 1
  • 18
  • 84
  • thanks for your effort!!bt stilll I get passing values null at log – Akshay May 31 '17 at 07:17
  • 2017-05-31 12:43:32.741 MenuBar[1398:90037] passing val: (null) 2017-05-31 12:43:33.464 MenuBar[1398:90096] requestReply:{"success":1,"otp":"753161"} 2017-05-31 12:43:33.464 MenuBar[1398:90096] {"success":1,"otp":"753161"} – Akshay May 31 '17 at 07:17
  • you need to extract value . – KKRocks May 31 '17 at 07:18
  • self.tmp = [requestReply valueForKey:@"otp"]; – KKRocks May 31 '17 at 07:19
  • 2017-05-31 12:51:08.063 MenuBar[1435:93502] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<__NSCFString 0x60800024eeb0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key otp.' *** First throw call stack: – Akshay May 31 '17 at 07:22
  • the app crashes – Akshay May 31 '17 at 07:22
  • wait i update my answer because it is the json string and you need to extract that. – KKRocks May 31 '17 at 07:23
  • I am getting the extracted value but the value is not passing to not vc – Akshay May 31 '17 at 07:33
  • what happens did you print while you have segue? – KKRocks May 31 '17 at 07:54
  • ya I have printed In prepare for segue method and passing value is still (null)....dnt understand it:( – Akshay May 31 '17 at 07:57
  • you can declare tmpStr as strong instead of copy and check it once – Rajesh Dharani May 31 '17 at 08:30
  • @RajeshDharani Thanks . might be this is the issue . – KKRocks May 31 '17 at 08:32
  • @RajeshDharani I checked it and ran the code still its showing null is there any other prob?? – Akshay May 31 '17 at 08:48
  • I have a doubt will it execute code even after moving to next page coz I have moved at the end of extracting top from json??@KKRocks @RajeshDharani – Akshay May 31 '17 at 08:54
  • Try this way may be its works I'm not tested Click on VerifyViewController xib file or if its in storyboard, remove all earlier bindings And Add new binding by providing IBOutlets,Clean project and run again. – Rajesh Dharani May 31 '17 at 09:06
  • did you check that it is passed to verifyController ? – KKRocks May 31 '17 at 09:08
  • 2017-05-31 15:14:08.219 MenuBar[1876:138678] the data Details is phone=9047038606. 2017-05-31 15:14:13.588 MenuBar[1876:138678] passed val: (null). 2017-05-31 15:14:21.172 MenuBar[1876:139004] requestReply: { otp = 453389; success = 1; } 2017-05-31 15:15:16.412 MenuBar[1876:139004] 453389 2017-05-31 15:15:25.896 MenuBar[1876:139004] requestReply: { otp = 453389; success = 1; } 2017-05-31 15:15:25.897 MenuBar[1876:139004] tmp storage:45338 – Akshay May 31 '17 at 09:46
  • why is my passed value coming first before even storing the value??@KKRocks – Akshay May 31 '17 at 09:47
  • where you written this **passed val**. – KKRocks May 31 '17 at 09:48
  • in prepare for segue method I have given nslog for passing value and no it gives only null!!I jus changed the passing value to passed value to know the difference!! – Akshay May 31 '17 at 12:02
  • did you use tmp string anywhere else in this class ? – KKRocks May 31 '17 at 12:05
  • NSLog(@"%@",self.tmp); is this print object ? – KKRocks May 31 '17 at 12:12
  • that works for other statements bt for this it shows null – Akshay May 31 '17 at 12:39
  • then you made some mistake for access that property . – KKRocks May 31 '17 at 12:41