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
UIWebView loadHTMLString:baseURL
came out.
[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:path]];
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
© 2024 OneMinuteCode. All rights reserved.