How do I insert a POST request into a UIWebView

前端 未结 7 1503
故里飘歌
故里飘歌 2021-02-01 08:50

For a GET request I\'ve tried this simple method:

NSString *urlAddress = @"http://example.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLReq         


        
7条回答
  •  余生分开走
    2021-02-01 09:22

    Thanks for you answer Seva. I've made a method out of your code, hope this helps other people :)

    //-------------------------------------------------------------------
    //  UIWebViewWithPost
    //       init a UIWebview With some post parameters
    //-------------------------------------------------------------------
    - (void)UIWebViewWithPost:(UIWebView *)uiWebView url:(NSString *)url params:(NSMutableArray *)params
    {
        NSMutableString *s = [NSMutableString stringWithCapacity:0];
        [s appendString: [NSString stringWithFormat:@""
         "
    ", url]]; if([params count] % 2 == 1) { NSLog(@"UIWebViewWithPost error: params don't seem right"); return; } for (int i=0; i < [params count] / 2; i++) { [s appendString: [NSString stringWithFormat:@"\n", [params objectAtIndex:i*2], [params objectAtIndex:(i*2)+1]]]; } [s appendString: @"
    "]; //NSLog(@"%@", s); [uiWebView loadHTMLString:s baseURL:nil]; }

    to use it

    NSMutableArray *webViewParams = [NSMutableArray arrayWithObjects:
                                     @"paramName1", @"paramValue1",
                                     @"paramName2", @"paramValue2",
                                     @"paramName3", @"paramValue3", 
                                     nil];
    [self UIWebViewWithPost:self.webView url:@"http://www.yourdomain.com" params:webViewParams];
    

提交回复
热议问题