0

I have following dictionary:

NSDictionary *param = @{@"schoolid":@"schooldb1",

                        @"token":del.tokenString,

                        @"mobile":del.phoneString

                        };

NSLog(@"param:%@",param);

I want to send this parameters (schoolid, token, mobile) to web view. But I don't know how to send that. I tried to search on internet but I didn't get any proper solution for my question.

My main URL is:

     NSString *url=@"https://MyURL.com/School/AppSingleTrack";

and I'm going to call UIWebview like following:

NSString *finalurl=[NSString stringWithFormat:@"https://MyURL.com/School/AppSingleTrack/?%@",param];
NSURL *nsurl=[NSURL URLWithString:finalurl];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[_webview loadRequest:nsrequest];
[self.view addSubview:_webview];
7
  • Are you using AFNetworking? Commented Jun 4, 2016 at 6:57
  • Yes, i'm using AFN in my project..@JAGAT Commented Jun 4, 2016 at 7:00
  • you would have to i guess inject javascript into webview and call ajax in it with those params. Commented Jun 4, 2016 at 7:00
  • I think you are using NSURLRequest, i am using NSMutableURLRequest, get the response, you need i post the method Commented Jun 4, 2016 at 7:01
  • thanks for your interest @Shubhank.... but how to do that, can you please post that method in your answer? Commented Jun 4, 2016 at 7:02

3 Answers 3

1

Try like this,

NSDictionary *param = @{@"schoolid":@"schooldb1",

                        @"token":del.tokenString,

                        @"mobile":del.phoneString

                        };

NSLog(@"param:%@",param);

NSString *url=@"https://24x7tracker.com/School/AppSingleTrack";


NSString *finalurl=[NSString stringWithFormat:@"https://24x7tracker.com/School/AppSingleTrack/"];
NSURL *nsurl=[NSURL URLWithString:finalurl];
NSMutableURLRequest *nsrequest=[NSMutableURLRequest requestWithURL:nsurl];
NSData *data = [NSJSONSerialization dataWithJSONObject:param options:0 error:nil];

[nsrequest setHTTPBody:data];

[_webview loadRequest:nsrequest];
[self.view addSubview:_webview];

set request's necessary properies if require like [nsrequest setHTTPMethod:@"GET"]; or POST and contentType etc.

You should use AFNetworking, It will make it more easier.

Sign up to request clarification or add additional context in comments.

2 Comments

@Lion...I tried your solution..there is something wrong. means url with above parameter are not in proper format..... my server side requirement is all that parameter are comma(,) separated.
and one another thing... if i add / after AppSingleTrack (as per your code) then it give me 404 error.... after removing that / it give me that another issue(passing data in improper format )
0

Use this Code,

    NSString *sUrl =  @"https://24x7tracker.com/School/AppSingleTrack";

    NSMutableURLRequest *res = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:sUrl]];

    [res setHTTPMethod:@"POST"];

    NSDictionary *params; = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"schooldb1",@"schoolid",
                                del.tokenString,@"token",
                                del.phoneString,@"mobile",
                                nil];

    NSMutableArray *pairArray = [[NSMutableArray alloc] initWithCapacity:0];
    for (NSString *key in params)
        [pairArray addObject:[NSString stringWithFormat:@"%@=%@", key, params[key]]];

    [res setHTTPBody:[[pairArray componentsJoinedByString:@"&"] dataUsingEncoding:NSUTF8StringEncoding]];


    [NSURLConnection sendAsynchronousRequest:res
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                           NSLog(@"request URL : %@",res.URL);
                           NSLog(@"request Method : %@",res.HTTPMethod);
                           NSLog(@"parameters : %@",params);
                           NSLog(@"response : %@",response);

                           Resp *r = [ [Resp alloc] initWithDictionary:[NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:nil]];

                                   // Success - Show Sucess message
                                    if ([r.sCode isEqualToString:@"success"]) {

                                         NSLog(@"response message : %@",r.sData); 
                                     }

    }];

Use the Class Resp:

Resp.h

#import <Foundation/Foundation.h>

@interface Resp : NSObject

@property (nonatomic, copy) NSString *sCode;
@property (nonatomic, copy) NSString *sMessage;
@property (nonatomic, copy) NSString *sData;


- (id)initWithDictionary:(NSDictionary *)dictionary;

@end

Resp.m

#import "Resp.h"

@implementation Resp

@synthesize sCode             = _id;
@synthesize sMessage          = _title;
@synthesize sData             = _data;


- (id)initWithDictionary:(NSDictionary *)dictionary {

    self = [super init];

    if (self) {
        self.sCode           = [dictionary objectForKey:@"code"];
        self.sMessage        = [dictionary objectForKey:@"message"];
        self.sData           = [dictionary objectForKey:@"data"];
    }

    return self;
}

@end

then finally get the response, hope its helpful

7 Comments

I got this error ..."use of undeclared identifier r" ..and this exception- sendAsynchronousRequest i deprecated in ios 9..... @Iyyappan
i replace r by res in above code.. then i got following response --: response : <NSHTTPURLResponse: 0x7963fcd0> { URL: 24x7tracker.com/School/html/errorpage.html } { status code: 200, headers { "Accept-Ranges" = bytes; "Cache-Control" = "no-cache, no-store, must-revalidate"; Connection = close; "Content-Length" = 427; "Content-Type" = "text/html"; Date = "Sat, 04 Jun 2016 08:16:46 GMT";Etag = "W/\"427-1459747608000\"";Expires = "Thu, 01 Jan 1970 00:00:00 GMT"; "Last-Modified" = "Mon, 04 Apr 2016 05:26:48 GMT"; Pragma = "no-cache"; Server = Apache;} }
I think you got correct response but response data is not get?
@Iyyappan.....i get this :(.... 2016-06-04 14:09:29.406 SchoolBusParentApp[926:43894] jsonList: (null)
ok i have used class and i update my code, finally run this method
|
0

Firstly, talk to your colleague or check the documents of the URL, to confirm what format of parameters the API needs, and the Request method, such as GET or POST.

Secondly, concatenate the params to the proper format, and don't forget escape the parameters.

If your URL need parameters as normal, try these:

NSDictionary *params = @{@"schoolid" : @"",
                         @"token" : @"",
                         @"mobile" : @""};
NSMutableArray *keyValues = [NSMutableArray array];
for (NSString *key in params) {
    [keyValues addObject:[NSString stringWithFormat:@"%@=%@&", key, params[key]]];
}
NSString *paramsString = [keyValues componentsJoinedByString:@"&"];
paramsString = [paramsString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

// Don't add / at last unless the URL has, because / is another path
NSString *baseURL = @"https://24x7tracker.com/School/AppSingleTrack";

// If GET, you can use these two lines, or use below
// NSString *urlString=[NSString stringWithFormat:@"%@?%@", baseURL, paramsString];
// NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];


// if POST
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:baseURL]];
request.HTTPMethod = @"POST"; // Default is GET, you can send get request by default,
request.HTTPBody = [paramsString dataUsingEncoding:NSUTF8StringEncoding];

[webView loadRequest:request];

Edit: According to @Shubhank's guess, if the webview's request via ajax, you should confirm the function of javascript, and try these codes in webview's delegate webViewDidFinishLoad:

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"javascriptFunction(%@, %@, %@)", param1, param2, param3]];
}

2 Comments

hey [email protected] side ajax is used....I tried this..but something is wrong
@SurajSukale So, the server side need you pass parameters via invoke the function of javascript, and you know the function name and format of parameters, then invoked the function with stringByEvaluatingJavaScriptFromString in the webViewDidFinishLoad:, but it's still wrong, right? May be you should debug this URL in Chrome or Firefox, and try to directly invoke this function on the console of the Brower.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.