How to load HTTP content in Webkit - ios

How to load HTTP content in Webkit in objective-c?
The code I use:
NSString *loadedURL = #"http://www.apple.com";
NSURL *targetURL = [NSURL URLWithString:loadedURL];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[_webView loadRequest:request];
If i post a link "https://www.apple.com" (with httpS) its open correctly, but from HTTP not. Can you help me?

Please check whether the App Transport Security restrictions are
disabled for all network connections, which means that unsecured http connections may be allowed.
Steps:
Opened my Projects info.plist file
Added a Key called NSAppTransportSecurity as a Dictionary.
Added a Subkey called NSAllowsArbitraryLoads as Boolean and set its value to YES as like following image.
Now clean the Project and Now Everything will Run fine as like before.

Related

Why http request was redirected to https unexpectedly?

I am using WKWebView to send some http requests. The using code is simple as below:
WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc] init];
self.webView = [[WKWebView alloc] initWithFrame:webviewFrame configuration:config];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
But sometimes, a few http requests would be redirected to https automatically。The method of these http request were POST, but would be changed to GET after redirecting. Meanwhile the data in the request body was discarded.
During the redirection, the delegate methods webView:didReceiveServerRedirectForProvisionalNavigation:, webView:decidePolicyForNavigationResponse:decisionHandler and webView:didReceiveAuthenticationChallenge:completionHandler: were all not called. Just webView:decidePolicyForNavigationAction: was called, two times.
What is weird is that once I set the websiteDataStore property of the config to nonPersistentDataStore, or remove the app and reinstall, the redirecting does not happen for the same request.
WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc] init];
config.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
self.webView = [[WKWebView alloc] initWithFrame:webviewFrame configuration:config];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];
However, it isn't a good idea to use nonPersistentDataStore in my project because there are some login-related business.
I want to know why the redirection happens, and is related to the websiteDataStore.
Do any other resolutions exist?
Finally I found that it is due to the HSTS mechanism. My post request was redirected forcibly to use the https and meanwhile the body data was lost. Someone had posted a question on Apple Developer fourms but no one answered. Here is the link. Is there any way to disable this mechanism for my WKWebView instance?

Download progress is not display in NSProgress when try download file from GoDaddy server url

I want to donwload mp3 file from goDaddy server and display donwload process in NSPorgress.
That's why I'm using Heikowi/HWIFileDownload this demo to refrence.
Demo is working fine with start, stop,cancel,pause and resume functionality.
But in this demo I can't able to download url from GoDaddy server.
How can I solve this?
I'm using this below url:
http://starindustries.co/assets/mp3/Exotic.mp3
I just found the solution that make your user class you just need to change following code in Download.m find start Method
Replace following existing line of code:
NSURLRequest *request = [NSURLRequest requestWithURL:self.url];
With:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.url];
[request setValue:#"identity" forHTTPHeaderField:#"Accept-Encoding"];
That's it your issue going to be solve. you getting actual length instead of -1

Remove UIWebView's internal cache

I'm showing a web app in an UIWebView, and sometimes the content of pages will change. After content have been changed the app clears the cache. But when I go to a page I've previously visited the UIWebView doesn't send a HTTP GET request, but loads from cache even though I've disabled cache like so:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
Initally I'm loading a request with cachePolicy cachePolicy:NSURLRequestReturnCacheDataElseLoad.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:myURLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10.0]];
UIWebView have some kind of internal cache. Already visited pages will be loaded from this internal cache instead of going through NSURLCache and also there's no request sent.
Is there any way to clear the internal cache of UIWebView? I'm even recreating the UIWebView but the cache is still there.
It appears that what's happening here is that it reloads the actual HTML file, but does not necessarily reload the resources within that page.
A possible solution I've seen is to append a query parameter on to the end of the URL. For example:
NSString *testURL = [NSString stringWithFormat:#"%#?t=%#", url, randQuery];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:testURL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10.0]];
where you generate a random alphanumeric string as your randQuery query parameter, or keep a persistent count and just count up.
This should force the UIWebView to load from the remote resource.
I had the same issue and setting HTTPShouldHandleCookies property to NO fixed my problem.
For example:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strurl]];
[request setHTTPShouldHandleCookies:NO];
[webView loadRequest: request];
Hope this help.

ios - Restkit and SSL certificate error

I have a tomcat server that uses a self signed SSL certificate and is running a web service. I am trying to connect to the web service with Restkit. However, I am getting an error related to certificate validity. Here is my code:
NSURL *url=[NSURL URLWithString:baseURL];
RKClient *client = [RKClient clientWithBaseURL:url];
client.disableCertificateValidation=YES;
RKRequest *request = [client requestWithResourcePath:[NSString stringWithFormat:#"/addEvent?deviceID=%#&eventID=%#",deviceID,eventID]];
request.disableCertificateValidation=YES;
request.delegate=self;
RKResponse *response = [request sendSynchronously];
This request fails with the following error:
2013-01-09 15:11:53.931 Mobile_ACPL[5761:907] The certificate for this server is invalid. You might be connecting to a server that is pretending to be “notify.acpl.lib.in.us” which could put your confidential information at risk.
I get this error even though I have set disableCertificateValidation to YES. How can I get this working?
EDIT: I attempted adding the certificate as shown here: https://github.com/RestKit/RestKit/pull/131
I still get the same result.
EDIT 2: It looks like the error message is being set at this line in RKRequest.m:
payload = [NSURLConnection sendSynchronousRequest:_URLRequest returningResponse:&URLResponse error:&error];
NSURLConnection does not cater for authentication challenges in synchronous calls. You need to make asynchronous calls for this to work.
In my case, I was setting disableCertificateValidation on RKClient but I was using a RKObjectManager which used a different RKClient. The following line, placed after the RKObjectManager initialization, did the trick:
[RKObjectManager sharedManager].client.disableCertificateValidation = YES;
if you are using RestKit using
client.allowsInvalidSSLCertificate = YES;
won't work, instead do this:
if you added rest kit manually to your project, click on RestKit.xcodeproj go to project > Build Settings > Preprocessor Macros
and add _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_=1
thats finished.

Google App Engine. Google app engine seeing POST requests as a GET

I'm having the strangest issue right now with google app engine. I'm sending a POST request from iOS and google app engine instead invokes the GET handler.
I've sandboxed this one situation for testing and can't get it figured out. I have an iOS app that just sends a request. And I've commented out everything on GAE except for the service. The service only logs a parameter and returns.
The iOS app I've tried using two different ways of sending the request. Neither works.
iOS Code:
/*
NSURL * url = [NSURL URLWithString:#"http://beermonster-gngrwzrd.appspot.com/TestParameter"];
ASIFormDataRequest * _fdrequest = [[ASIFormDataRequest alloc] initWithURL:url];
[_fdrequest setPostValue:#"hello" forKey:#"testkey"];
[_fdrequest startAsynchronous];
*/
NSURL * __url = [NSURL URLWithString:#"http://beermonster-gngrwzrd.appspot.com/TestParameter"];
NSMutableURLRequest * __request = [NSMutableURLRequest requestWithURL:__url];
[__request setHTTPMethod:#"POST"];
NSString * post = [NSString stringWithFormat:#"testkey=hello"];
[__request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendSynchronousRequest:__request returningResponse:nil error:nil];
My App engine handler:
class TestParameter(webapp.RequestHandler):
def post(self):
logging.debug(self.request.get("testkey"))
self.response.out.write(self.request.get("testkey"))
print self.request.get("testkey")
def get(self):
logging.debug("get")
logging.debug(self.request.get("testkey"))
self.response.out.write(self.request.get("testkey"))
The output in the GAE logs shows the "get" code path which isn't correct.
Any ideas why POST requests would come into GAE as a GET? Is there some configuration in GAE that I missed?
Thanks!
Check the entry in app.yaml for the script that handles "/TestParameter". Does it specify "secure: always"? If it does and you make a non-secure connection you will get a 302 redirecting to the secure version.
To fix this either make your post over HTTPS or remove "secure: always" from the entry in app.yaml.
From what I can tell if you want to send POST requests to GAE. Make sure you do it on https. If you make the request on a non-https attempt, it sends back a 302 redirect to the https version of the request. But if whatever you're using to send the request doesn't correctly handle 302's it might resend the request incorrectly.

Resources