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 .
Related
I am using AFNetworking to download a pdf (that will change on a weekly basis) and save it into the documents directory with this code:
//Get the PDF
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.somewebsiteaddress/CurrentEdition1.pdf"]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"CurrentEdition1.pdf"];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"Successfully downloaded file to %#", filePath);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
[operation start];
I then want to read that saved file in another section of the app in a UIWebView (after it has downloaded) and use this code:
//Now create Request for the file that was saved in your documents folder
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:#"Documents"]];
NSString *filePath = [resourceDocPath stringByAppendingPathComponent:#"CurrentEdition1.pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webVieweedition setUserInteractionEnabled:YES];
[webVieweedition setDelegate:self];
[webVieweedition loadRequest:requestObj];
I have used a few single page pdf documents to test with - loading them up on the server and then seeing if they are downloaded and then viewed when changed. Problem is, this seems to only be working about 50% of the time. I'll put a new file up and sometimes the correct one will be shown in the UIWebView and sometimes it will show the previous one and not the new one. I am waiting until I see the download completed message before I try to go to the UIWebView (although I know clients won't do that, but that's a whole other question). Anyway, I'm new to XCode and have just been a web html guy. This has had my head spinning for two days. Using Storyboards, ARC, XCode 4.6.2.
If i understood right, sometimes you see the same pdfs in app, although you changed them on webserver?May be the reason is cache, try construct request this way, ignoring caching
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://www.somewebsiteaddress/CurrentEdition1.pdf"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10];
As ios documentation says, [webView stopLoading] method should be used in order to stop webview load task.
As far as I see, this methods runs asynchronously, and does NOT stop currently processing load request immediately.
However, I need a method which will force webview to immediately stop ongoing task, because loading part blocks the main thread, which could result in flicks on animations.
So, is there a way to succeed this?
This worked for me.
if (webText && webText.loading){
[webText stopLoading];
}
webText.delegate=nil;
NSURL *url = [NSURL URLWithString:#""];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webText loadRequest:requestObj];
Don't use the UIWebView to download your html document directly.
Use async download mechanism like ASIHTTPRequest to get your html downloaded by a background thread.
When you get the requestFinished with a content of your html then give it to the UIWebView.
Example from the ASIHTTPRequest's page how to create an asynchronous request:
- (IBAction)grabURLInBackground:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://allseeing-i.com"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
// Use when fetching binary data
NSData *responseData = [request responseData];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
}
Use the responseString to your UIWebView's loadHTMLString method's parameter:
UIWebView *webView = [[UIWebView alloc] init];
[webView loadHTMLString:responseString baseURL:[NSURL URLWithString:#"Your original URL string"]];
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.
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
In my app i have to upload the video to the web server using POST method. For this iam using UIImagePickerController. But when i am selecting video from my ImagePickerController it is going on by showing "compressing Video". I have written picker allowsEditing to NO also and the video saved in photo library is of duration just 50 seconds only. But still i am getting the same. Immediate response is greatly appreciated.
Try this:-
NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
NSString *str = [NSString stringWithFormat:#"%#/uploadVideoIphone.php",appUrl];
NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
NSString *filePath = [urlvideo path];
[request addFile:filePath forKey:#"uploadfile"];
[request setRequestMethod:#"POST"];
[request setDelegate:self];
[request setTimeOutSeconds:200];
[request startSynchronous];
NSLog(#"responseStatusCode %i",[request responseStatusCode]);
NSLog(#"responseString %#",[request responseString]);
[self dismissModalViewControllerAnimated:YES];