Completing NSURLRequest upon app launch - ios

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.

Related

Convert a UIWebView request onto a String on the initialization of the App on XCode

My goal with this code is to simply show the html code from the url request on a Label as the app is initialized:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://192.168.25.242:8090"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
sleep(5);
self.myLabel.text = [_webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];
}
I think the url request take some time to end, so I used sleep function trying to wait for it to end and then convert the webview results into string and load on the label... But is shows nothing. When I create a button to "self.myLabel.text = [_webView stringByEvaluatingJavaScriptFromString:#"document.body.innerHTML"];" after any webview load, it does work, and the label gets the html from the webview correctly.
What should I do?
The sleep() function won't work because the webView won't be given time to load. Instead, set _webView.delegate = self; and implement -webViewDidFinishLoad:, where you should set the label text.

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.

UIWebView does not load full content from an url

I have a UIWebView where i load a url. But it does not load full content. I have checked all possible forums for solution but couldn't find a breakthrough yet. Any help would be really appreciated? You can try out the below url yourself. Basically, the url is supposed to contain html5 content/player.
-(void)loadPlayerUrl
{
activityIndicatorView.hidden = NO;
NSURL* nsUrl = [NSURL URLWithString:#"http://watch.nimbletv.com/tv"]
NSURLRequest* request = [NSURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
[self.playerWebView loadRequest:request];
}
However, this works perfectly in safari or any other browser.
You should create ViewControllerWebPage
Then
- (void)viewDidLoad
{
[super viewDidLoad];
self.screenName = #"Info Ekran";
NSURL *mobileCreaURL = [NSURL URLWithString:#"http://mobilecrea.com/"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:mobileCreaURL];
[mobileCrea loadRequest:myRequest];
}
Also create new view, select your new custom class (ViewControllerWebPage) and than create UIWebView

iOS 7 UIWebView inconsistent Loading

In my project I have 2 UIWebViews that are IBOutlets and created in the main.storyboard.
One I load by calling a method on ViewDidAppear. It appears perfectly and hits all it's delegate methods. We will call this ViewA
The other is supposed to be Hidden and appear and load when the user taps a button. We will call this ViewB. But once I submit the request, nothing happens, none of the delegate methods get called.
ViewA loads perfectly. If I try to load ViewB on ViewDidAppear, it also loads.
EDIT: Moving the ViewA and ViewB load method calls to the ButtonPress method calls both successfully.
EDIT: Code
-(void)loadCarouselWebView //ViewA
{
NSString *urlString = [NSString stringWithFormat:#"%#%#",[[JSONReader sharedInstance] baseURLToLoad:environmentURLPublicKey],[[JSONReader sharedInstance] URLToLoad:kCarouselURLkey]];
NSURL *url = [NSURL URLWithString: urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.carouselWebView.delegate = self;
[self.carouselWebView loadRequest: requestObj];
}
-(void)loadFullWebView:(int)index //ViewB
{
NSString *gameURL;
switch (index) {
case 1:
gameURL = game1Constant;
break;
case 2:
gameURL = game2Constant;
break;
case 3:
gameURL = game3Constant;
default:
break;
}
NSString *urlString = [NSString stringWithFormat:#"%#%#",[[JSONReader sharedInstance] baseURLToLoad:environmentURLKey],[[JSONReader sharedInstance] URLToLoad:gameURL]];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.fullWebView.delegate = self;
[self.fullWebView loadRequest: requestObj];
}
- (IBAction)gameButtonPushed:(id)sender {
[[self fullWebView] setHidden:NO];
[[self backNavButton] setHidden:NO];
[self loadFullWebView:[sender tag]];
[self loadCarouselWebView];//Added for debugging
}
EDIT: ONLY Loading ViewB on button tap works. If I add Load ViewA on ViewDidAppear then ViewB will not load.
In a surprise twist, there is nothing wrong with the UIWebView.
It was my fault for not reading the code carefully enough.
Someone coded a one time counter into the shouldStartLoadingRequest delegate method that I missed. So only one request was allowed to load.
Thats like 2 days burned there, great stuff! >:E

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