NSURLSession GETs PHP and displays response in UIWebView

Asked 2 years ago, Updated 2 years ago, 69 views

We would like to get queries from iOS applications to PHP using NSURLSession and display the response (HTML) in UIWebView on the application.

As for the response from PHP, I think HTML is included in the NSURLResponse as text, but how do I pass the data to UIWebView and display it?

ios objective-c php

2022-09-30 17:35

2 Answers

UIWebView loadHTMLString:baseURL came out.

[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];


2022-09-30 17:35

NSURLResponse itself does not contain HTML text.
You can retrieve data from the callback arguments that you set to NSURLSessionDataTask.

Text encoding must be obtained from NSURLResponse.

NSURL*url=[NSURLURLWithString:@"http://google.com/"];
NSURLSessionConfiguration* config = NSURLSessionConfiguration defaultSessionConfiguration;
NSURLSession* session = [NSURLSession sessionWithConfiguration:config];

NSURLSessionDataTask*task=
session dataTaskWithURL: url
       completionHandler:^(NSData*data, NSURLResponse*response, NSError*error){

         // Get Encoding and Convert to Text
         CFSstringEncoding = CFSstringConvertIANACharSetNameToEncoding ((CFSstringRef) [response textEncodingName]);
         NSSstring*html = [[NSString alloc] initWithData:data encoding:CFSStringConvertEncodingToNSStringEncoding(encoding)];

         dispatch_async(dispatch_get_main_queue(),^{
           // Show HTML in WebView
           [webView loadHTMLString: html baseURL: [response URL]];
         });
       }];

// Download Start
task resume;

Reference Page: http://chicketen.blog.jp/archives/1256068.html


2022-09-30 17:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.