UIWebView does not initially load certain URLs - ios

Having such a bizarre issue with the UIWebView and I haven't been able to find a solution online. I have an iPad app that has a web view in it. Upon first install and run of the app, I try to load an education website. The web view just hangs and times out. I kill the app, I run it again, and it loads just fine. This one works fine which is also part of the education website. I'm pulling my hair out!
Some code:
-(void)viewDidAppear:(BOOL)animated
{
NSURL *websiteUrl = [NSURL URLWithString:#"http://my.tac.edu.au"];
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL: websiteUrl];
[self.webView loadRequest:urlRequest];
}
Works fine with www.tac.edu.au. I don't know what it is about that URL that makes it hang only when the app is first installed. You stop it, run it again (without uninstalling) and it comes up just fine. Any help would be appreciated.

:)
instead of view did appear , write the code in view will appear
-(void)viewWillAppear:(BOOL)animated
{
NSURL *websiteUrl = [NSURL URLWithString:#"http://my.tac.edu.au"];
NSURLRequest *urlRequest = [[NSURLRequest alloc]initWithURL: websiteUrl];
[self.webView loadRequest:urlRequest];
self.webview.delegate = self;
}
- (void)webViewDidStartLoad:(UIWebView *)webView{
//Start Activity Indicator
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//End activity indicator
}

Related

iOS view PDF from a given url inside of an app

I want to open and show a pdf file from a given url (somewhere in the internet) and view it in my iOS application. Could you please tell me what are the possible approaches to this problem, and list their advantages and disadvantages / limitations? I've heard about UIWebView, are there any other options?
My suggestion is using the webview.
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *url = [NSURL URLWithString:#"https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ProgrammingWithObjectiveC.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webview loadRequest:request];
}
If you want to more information please refer the follwing link as well
https://developer.apple.com/library/ios/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html

iOS xcode Alternative to UIWebview didFinishLoad

I have a webview that when loaded, the user is logged in by a POST request. After they are logged in, I want them to be taken to a webpage. My POST request is a URL as follows:
- (void)viewDidLoad {
[super viewDidLoad];
[_scoreWebView setDelegate:self];
NSMutableURLRequest *detailRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:#"myrul"]];
[detailRequest setHTTPMethod:#"POST"];
NSString *sendInfo =[NSString stringWithFormat:#"action=login&EMAIL=email&PASSWORD=password", nil];
NSData *infoData = [sendInfo dataUsingEncoding:NSUTF8StringEncoding];
[detailRequest setHTTPBody:infoData];
[_scoreWebView loadRequest:detailRequest];
}
This log in process works fine. However, after I send the user to my webpage it is launching webviewdidfinishload infinitely. I know that it fires each time something is loaded. Is there an alternate solution to redirecting the user to my page after log in? Also, I have three different pages that the user could be redirected to based on their input, this is just one of them for simplicity. This is my finishload method:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//Check here if still webview is loding the content
if (webView.isLoading)
return;
else //finished
NSLog(#"finished loading");
NSLog(#"%# in calendar", _thisScriptUniqueID);
NSString *fullURL=[NSString stringWithFormat:#"myurl/%##score", _thisScriptUniqueID];
NSLog(#"%#", fullURL);
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_scoreWebView loadRequest:requestObj];
}
Is there a different method that could be used to take the user to the page, or would it be possible to include both in the viewDidLoad?
After a lot of research, I decided that the functionality would work a lot better on the server side. I made it so that the same URL logs the user in and brings them to the desired page at the same time.

Internet data doesn't update correctly

I've got this weird issue with my apps that uses internet connection. The app doesn't seem to update the data from internet in the right way. I've got a UIWebView with a simple chat. When I write a message in the chat with safari or in my web browser everything works fine. When writing something in the chat in the app, the data doesn't update, even when reloading the page.
It updates totally un-logically as well. Even when restarting the app the new data isn't shown, then all of a sudden it shows.
Had the same issue with another app too, which was not handled by a UIWebView. Any idea what it might be?
Example code:
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSString *username = [[Database singletonDataBase] currentUserName];
NSString *urlAddress = [NSString stringWithFormat:#"%#?name=%#", #"http://webapi.com/index.php", username];
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestObj];
}
In your app delegates, didFinishLoading method, set the NSURLCache to not cache the data:
// Remove and disable all URL Cache, but doesn't seem to affect the memory
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
If that doesn't work, you can also try setting the cache policy on the NSURLRequest:
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval: 10.0];
I finally found the answer. Its nothing wrong with my code, as I thought. The problem lies on the server side. And, as they say in this thread:
NSURLConnection is returning old data
It seems to be badly configured server-side or proxy. Which makes the cache act all wierd. I tried another server and everything worked just fine!
Hope this helps someone with the same issue, cause it sure wasted me ALOT of time.

Completing NSURLRequest upon app launch

So I have a tabbed iOS app, and two of the view controllers in the app each have webViews in them. Nothing else. When these views are opened, they then call the NSURLRequest I have coded in the viewDidLoad method (as we all know). Very typical, basic, simple code.
What I am trying to do but haven't been able to figure out, is how to have these requests called and completed upon app launch, as opposed to being triggered when the view controller is viewed for the first time. It just takes too long to load.
I'm not very experienced with threads and blocks so any advice would help! I do know that all the UI stuff needs to be called on the main thread/queue. I have made an attempt at using another thread (commented out in view controller 2), so any further explanation as to what I already have would be great as well.
VC 1
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"https://soundcloud.com/vanguardsf"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_music loadRequest:request];
}
VC 2
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect screen = [[UIScreen mainScreen]applicationFrame];
_webView = [[UIWebView alloc]initWithFrame:screen];
_webView.delegate = self;
[self.view addSubview:_webView];
NSString *netGym = #"http://www.netgym.com/login.asp";
NSURL *url = [NSURL URLWithString:netGym];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
NSLog(#"%#", request );
// if (!requestQueue) {
// requestQueue = dispatch_queue_create("come.requestNetgym.load", NULL);
// }
// dispatch_queue_t requestQueue = dispatch_queue_create("come.requestNetgym.load", NULL);
// dispatch_async(requestQueue, ^{
// NSString *netGym = #"http://www.netgym.com/login.asp";
// NSURL *url = [NSURL URLWithString:netGym];
// NSURLRequest *request = [NSURLRequest requestWithURL:url];
// [_webView loadRequest:request];
// NSLog(#"%#", request );
// });
}
You don't have to use GDC to send asynchrounus NSURLRequests - they are asynchronous if You don't use them synchronously on purpose.
You can start the requests in this method:
https://developer.apple.com/library/ios/documentation/uikit/reference/uiapplicationdelegate_protocol/Reference/Reference.html#//apple_ref/occ/intfm/UIApplicationDelegate/application:willFinishLaunchingWithOptions:
This is first place in application when You can execute a code just after launching is finished. You will save some time (little I suppose - this looks like a simple app).
I think that better solution is to cache the downloaded website data. In this case You will download the data first time You enter the app and the second time You will display downloaded content. In the background You will check if website has changed. If so You update the content.

How to load a UIWebView when click a button in another view in ios

I want to create an ios application which incuding a UIWebview. I saw so many tutorials that saying how to load webview when start the application. But what I want is when user click on a link in one View,the web page should be loaded in seperate View. This is the code in my button click event.
`
-(IBAction)visitWeb:(id)sender{
web *wb=[[web alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:wb animated:YES];
[wb release];}
This is in web
`
- (void)viewDidLoad {
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
[super viewDidLoad]; }
But it not display anything. Just display the empty UIWebView What is the reason for it.plz give me a solution with the code.
Thanks,

Resources