would like to be able to load a website without loading it in Safari (for server call purposes)
I tried theses two methods but they don t work at all :
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.google.com"] encoding:NSStringEncodingConversionAllowLossy error:nil];
and
NSURL *URL = [NSURL URLWithString:#"http://www.google.com"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:URL];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
but both these options don t load the website unfortunatley , any reasons ?
If you are trying to display the webpage, you should check out the UIWebView class or WebView class on OS X.
You may find the following sample code to be useful, PrintWebView (OS X), UICatalog (iOS).
If you are trying to download the HTML source of the webpage, then you really don't want to be using stringWithContentsOfURL:. It will block the main thread, for more information see Synchronous Networking on the Main Thread.
Related
I am extremely confused with the NSUrlSession and the API. This is my first time trying to use an API so please explain this in the simplest form possible.
I found an API which gets the weather, I have made a string for the weather location. then did all the NSUrl / nsurlrequest. My goal is to output everything so I can see the keys of that API. Heres what I have so far but all It displays is 'Program ended with exit code 0'
I don't really know what is happening during the NSUrlsession because I learned how to use API with the NSUrlConnection via a youtube video.
NSString *location = #"London";
NSString *weatherString = [NSString stringWithFormat:#"https://api.openweathermap.org/data/2.5/weather?q=%#", location];
NSURL *weatherURL = [NSURL URLWithString:weatherString];
NSURLRequest *request = [NSURLRequest requestWithURL:weatherURL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error){
NSDictionary *weatherDictionary = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:nil];
NSLog(#"%#", [weatherDictionary description]);
}];
It's hard to tell from this snippet, but one or more of the following problems are likely causing your issues:
You are retaining a reference to that task somewhere, right?
From the documentation for dataTaskWithRequest, you need to call [task resume] to actually start the task.
That URL won't work, because the api.openweathermap.org site doesn't support HTTPS. You'll need to change it to http, and possibly add an exception in the app's Info.plist to allow non-secure connections (they're disabled by default for new apps).
After you fix all that, you'll need an API key for the request to actually succeed.
I have a Cocoa application. It runs fine via XCode 6, but when I run it manually via Finder, it behaves very strange: it seems that only the static XIB loads, no other code gets executed.
Do I need to sign it in order to work? I also tried archiving. For any clues, this is the code that executes first:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
[request setHTTPMethod:verb];
NSData *data = [qs dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [NSString stringWithFormat:#"%#", responseData];
Yes, there are HTTP requests made first in applicationDidFinishLaunching for the design to change.
Thank you!
EDIT: Forgot to mention that I use dispatch_queue_t and dispatch_async for those requests, so I am not blocking the main thread.
I was actually having problems with some inexistent files.
Let's say I have some data from a webpage:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:myurl]];
[request setHTTPMethod:#"GET"];
NSData* returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSString* html = [[NSString alloc] initWithData:returnedData encoding:NSUTF8StringEncoding];
I also have a local javascript script that runs some queries on the data and modifies the DOM. At the moment, in order to achieve this, I am taking the HTML received above, creating a webview and loading this into it:
[NSString stringWithFormat:#"<script>%#</script>%#", myscript, html];
I am then, on the webview didFinishLoadForFrame event, calling
NSString* modifiedhtml = [sender stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"]]
This leaves me with my modifiedhtml, with which I can do as I please. However- this doesn't seem elegant at all, especially since creating webviews is only permitted on the main thread and this whole process would ideally run in a background thread, also, very occasionally the script throws an error and the webview load event is not called so I have a growing number of lines just dedicated to handling a timeout for this occurrence. It is, all in all, a big ugly mess.
In an ideal world I would apply this javascript on the first pass without creating a webview at all. I've been trying all sorts of things like:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:myurl]];
[request setHTTPMethod:#"GET"];
NSData* returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSString* html = [[NSString alloc] initWithData:returnedData encoding:NSUTF8StringEncoding];
NSString* modifiedhtml = [request stringByEvaluatingJavaScriptFromString: myscript]];
where the script is set to return the page but to no avail.
Are there any suggestions as how to handle this more elegantly and efficiently than I am currently?
To avoid the use of a web view and assuming that your html has a head tag you could do something like this:
NSArray *tempArray = [html componentsSeparatedByString:#"<head>"];
NSString *modifiedHtml = [NSString stringWithFormat:#"%#<head>%#%#", [tempArray objectAtIndex:0], yourScriptString, [tempArray objectAtIndex:1]];
It will insert your script into the existing head tag. You should complete the code with usual sanity checks.
I am trying to display web pages using UIwebview on iPad.
I used following code :-
NSString *urlAddress=#"http://google.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSLog(#"The value of url in screen 2 is %#",url);
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[web loadRequest:requestObj];
web is an object of type uiwebview .
This works fine with certain links but when i tried to open below link
http://deverpids.ltisap.com:8000/sap(bD1lbiZjPTgwMA==)/bc/bsp/sap/zbbdashboard/demo.htm#infohttp://deverpids.ltisap.com:8000/sap(bD1lbiZjPTgwMA==)/bc/bsp/sap/zbbdashboard/demo.htm#info
It does not display anything .Read few articles and taught this could be related to a proxy issue
How to deal with proxy setting in UIWebView?
so tried to use this but no success . code used is as below :
NSURL *url = [NSURL URLWithString:#"http://google.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request startSynchronous];
NSError *error = [request error];
if (!error) {
NSString *response = [request responseString];
NSLog(#"error is %#",response);
}
But now nothing loads up in webview .how do i tackle this .
I am trying to find a way to load a website (for server call purposes) but I don t want it to open neither in safari nor in a UIWebView . I usually use this code for opening in safari
[[UIApplication sharedApplication]
openURL:[NSURL URLWithString:#"http://www.google.com"]];
and for the UIWebViews
[web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:thelink]]];
I tried to use the UIWebView without actually having a physical one but the page didn't load (I know that because the server call were not made at all)
thanks
You should use NSURLRequest, for example:
NSString * url = #"http://myapi.mysite.com/api.php";
NSMutableURLRequest * aRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
NSHTTPURLResponse * httpResponse = nil;
NSError * urlError = nil;
NSData * responseData = [NSURLConnection sendSynchronousRequest:aRequest returningResponse:&httpResponse error:&urlError];
Where responseData is the body of the response, you can use httpResponse to check status codes etc...
NSURLConnection is your friend:
NSURL *URL = [NSURL URLWithString:#"http://www.google.com"];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:URL];
// configure req to have post data and such
NSURLResponse *response = nil;
NSError *error = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
// do something with data
Just use
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://www.google.com"] encoding:NSStringEncodingConversionAllowLossy error:nil];
if connected is != NULL then everything worked fine