Execute a function just after the view loads, iphone - iphone

I am creating a tabbar application. One of the tabs is for an rss feed, which is a navigation application. but when i click the tab bat button, it is taking a while to load the view of that tab. It is because the application is waiting for the feed to be loaded from the server. Is there any way to load the view before the loading of that feed takes place. As of now, i'm giving the request in the viewDidLoad method. Thats what is creating the problem. To which part shall i move the code so that the view is loaded instantaneously when clicking the tabbar button.

I recommend this great article on this subject on iCodeBlog, it's a very elegant way of doing this. If you submit your rss feed loading as an NSOperation, it will take place nicely in the background without blocking your main thread.

use:
[self performSelector:#selector(performRSS:) withObject:<nil afterDelay:0.3f];
or
[NSThread detachNewThreadSelector:#selector(performRSS:) toTarget:self withObject:nil];
and place RSS feed related code in a separate function named "performRSS".

I also think that the problem is more that you don't use the HTTP request asyncronously (as Apple recommends). See this document. http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html
It worked for me in my applications.

Related

Loading background data and launch image

I have read the apple guidelines and I know it says you shouldn't do that but hear me out as I would like to know if what I am doing is bad practice.
When my application loads up, in the app delegate, a web call is made which sets up the order of the tabs, as well the content within it. Web call is like this
WebCalls *wc = [[WebCalls alloc] init];
[wc setWebCallDidFinish:^(NSString * json) {
// set up tab order here, as well as stores the JSON in a file on the phone
// Also code here to download images and cache them on phone
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
}
[wc getData:phoneNumber];
Now this code works great but the problem is what will happen when app starts is
Launch image shows for a second (which is not very long, sometimes it's half a second or less so just annoying)
Screen goes black for about 2 seconds while json is parsed and images downloaded etc
Then first tab controller is shown
What I want is a seamless transition between the splash screen and the first screen so the user never sees black screen.
What I was thinking of doing is something like this
Change iPhone splash screen time
In the answer given, the guy pushes a view forward to be the splash screen. Would it be bad practice to push that view forward, and then in that screen do the web calls which gets json data, and downloads images, then dismiss the view and have the tabcontroller view become main view?
Or how else would I prevent this delay? Is it bad practice to have a large enough web call like this in AppDelegate?
If this is bad practice to push a view forward while doing background loading, what else would you recommend? Would it be better if I just make the tabController the main rootViewController first and do the webCall in the first tab shown instead, then update the tabs when this web call finished? I was considering this one, but the tab order could be in any order after the web call is made, so not sure what tab will be shown first.
Would be grateful for your input
In the answer given, the guy pushes a view forward to be the splash screen. Would it be bad practice to push that view forward, and then in that screen do the web calls which gets json data, and downloads images, then dismiss the view and have the tabcontroller view become main view?
This is the way to do it. It's generally bad practice to download stuff from applicationDidFinishLaunching, what happens if the phone is not connected to the internet?
Present a simple view controller (using presentModalViewController:controller animated:NO with a UIActivityIndicator and a label describing what's going on, and then dismiss it when loading finishes (or it it fails, just display an error and deny access to the app). Remember to also check for airplane mode and notify the user.

synchronously load html content in UIWebView

im using a UIWebView to show html content in my app, the app contains two arrows to navigate between topics just like a RSS reader app, but when the user hits up or down arrow, the next topic doesn't show up until the data come back and the user still able to interact with the UI which is a bit confusing,
My question: how to block the UI when user moves to the next/back topic ? in other words how to make loadHTMLString:baseURL: works as a synchronous calling ? thanks !
You can let the load happen asynchronously, but set the web view's userInteractionEnabled property to NO. (then back to YES, on the didFinishLoad callback).
Or you could put up a clear colored view (with userInteractionEnabled set to NO) above the web view that has an activity indicator and button that lets the user cancel the load.
An even better idea would be to place two other web views offscreen and start loading them for page N-1 and N+1. When the user presses a page arrow, swap frames with the corresponding prefetched web view.
Try this - https://github.com/gavrix/UISynchedWebView-demo
You don't actually want to block the UI. If you do that, there is a very high probability that Apple will reject your app once you send it in for app store submission. Anything that even remotely makes the application feel unresponsive will weigh heavily on you. Instead, create a background thread using GCD or performSelectorInBackground, handle your loading in that, and then once the loading is done, make all of the information available to your UIWebView all at once and alert it to render the display.
If you are in a pinch and have a UIPageViewController and still want to use a UISynchedWebView to ensure that a page has loaded before you run javascript, you can run a block on the main thread's event queue. Still has a slight delay while the javascript runs but won't cause recursion in the run loop.
dispatch_async(dispatch_get_main_queue(), ^{
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://google.com"]]]; // self.webView is a UISynchedWebView
NSLog(#"url:%#", [self.webView stringByEvaluatingJavaScriptFromString:#"window.location.href;"]); // shows google.com instead of about:blank
});

iPhone SubView Loading and Waiting

My iPhone app has a sub view on its Welcome View Controller. The sub view parse data from a website and load data accordingly. Welcome View Controller has a continue button to go to the next view controller. But until the sub view load its data I cannot go to the next view controller.
Can anyone suggest me any solution on this. Thanks in Advance.
In you are using NSURLConnection/NSURLRequest to retrieve the data, I would suggest two approaches:
modify the code your class that retrieves the data so that the request is made asynchronous; this will make it non-blocking and the user will be able to move next without waiting; when moving next, you'll have the option of canceling the request, so to save bandwidth;
perform the request in a separate thread; you can do that either using NSTask or GCD dispatch_asinc like shown below; in this case anyway, you have to be aware of the fact that your separate thread may not modify the UI (i.e., use UIKit), because this can only be done from the main thread. So, in your thread, you update the data, but then issue a refresh of the UI on the main thread (by using performSelectorOnMainThread).
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{ [self SENDREQUEST]; });
As Vince pointed, you should retrieve the data from a separated thread (create a worker class to do that for you). When the classes finishes his job, you should tell the Welcome view, that the data is ready. You could achieve that, by using a protocol, or NSNotification.

Web view is loading after the transition animation

i am using a web view and alsousing the drag event to load next or previous page on drag right or left event simultaneously but the web loads the next page after fininshing the curl up animation.
i want to load page data during the animation,
it takes one second to load the new page after animation finishes.
i have checked the loading timing and sequence, web view is loading page and finish loading before starting animation but curlup animation is actually locking the page.
can any one please help me.
You might try to load the next and previous pages on a background thread, so they will be already loaded when the user tries to reach them...
U might want to create an method to get the views (Eg.getView) that takes an NSURL as argument...
and for every swipe you call:
NextView= [self performSelectorInBackground:#selector(getView:) withObject:nextURL];
PrevView= [self performSelectorInBackground:#selector(sgetView:) withObject:prevURL];

UIWebView overlapping

hai good afternoon to everyone, I am developing an iPhone web application. In my application I have loaded two web pages in a single UIWebView. After this process has completed, one web page should hide or overlap others whenever they get the user-interaction. However, I don't need this yet. User-interaction also should remain so-far.
You can't have two different webpages in one UIWebView. Instead, you should try to create two UIWebView objects and add them as a subview in a container view.
You can set these UIWebView objects' frame, userInteractionEnabled, hidden properties according to your needs.
Also, you should look at delegate methods of UIWebView. Those delegate methods are called when a page is finished loading or when there is an error exc.
You can read the documentation to learn more about delegate methods:
UIWebViewDelegate documentation