I have an IBAction, triggered by a UIButton.
In this IBAction I connect my app to server, returning a Plist.
During this time (between my apps connect to server, and the server returns that Plist) I want to show a UIActivityIndicator.
The problem is, when my IBAction is triggered, the apps connect first to server, an then shows the UIActivityIndicator.
My IBAction in pseudocode
- (IBAction) loginMe: (id) sender
{
// show activity indicator
// connect to server, and catch data
}
Your "connect to server" action is likely synchronous. The action "show activity monitor" is incorrectly labeled. What you're really doing is "mark activity monitor for display next time drawing occurs."
What you need to do is to make sure that loginMe: returns so that the UI can update. The server connection logic needs to continue asynchronously. Generally this is done with NSURLConnection. You can find explanation in the URL Loading System Programming Guide.
Also check out MBProgressHUD here:
https://github.com/jdg/MBProgressHUD
It wraps the indicator and allows for additional customization based on how you connect synchronously or asynchronously.
Related
I am currently using NSTimer to load a "Please Wait" alert view in my application while it contacts a server. I want to get rid of this NSTimer and change it so that it runs the "Please Wait" alert view in a loop until it gets a response from the server.
How would I do this?
A loop would be bad (blocking the main thread), but if you use NSURLConnection object you get a connectionDidFinishLoading: message in the delegate.
You pop up the alert when you start the connection.
You dismiss the alert in the connectionDidFinishLoading: of your delegate.
And also when an error occurs.
Though I agree with #Radu when it's about user experience.
Set up a delegate for the NSURLConnection. Your delegate will then receive messages at interesting points in the connection, such as when data is received and when the connection is finished.
try this page. It helps manage cases where there might be more than one network event going on.
In my app I have a login view. When I enter login details and click login button, the app freezes for some time and then continues. I know it's freezing because it is communicating with remote server but how can I prevent the freeze thing and show a nice looking status animation instead?
As you are communication with server it will takes some time, so meanwhile you can add some animation on alertview.
But by calling simply alertview it will not work.
So you have to proceed in the following steps:
1) On Submit Button Click first add Custom Indication View
2) Start Animation View
3) Call NSThread for server communication like
[NSThread detachNewThreadSelector:#selector(yourActionName) toTarget:self withObject:nil];
4) Remove Animation View
For more information go through this article:
http://iphonedevcentral.blogspot.com/2010/08/safe-threaded-design-and-inter-thread.html
Your API call is blocking the GUI thread. You can circumvent this by moving the remote API request call in a NSOperation call, and pop up a loading screen until the API call returns.
in iphone,can I get the push notification's custom properties when the app is closed or background running?
When an app(not running) received a push, system generate an alert with two buttons: "Close" and "View". If the user taps View, the application is launched. my question is can i change the action of the "View" button, such as open a web link?
I'm not sure that understood what you need...
Think isn't possible... Only things you can change is button (and body) text. More info you can find in Table 3.2. One of the solution could be, to automatically open a web link, when application is launched from push notification.
If the action button is tapped, the system launches the application and the application calls its delegate’s application:didFinishLaunchingWithOptions: method (if implemented); it passes in the notification payload (for remote notifications) or the local-notification object (for local notifications). If the application icon is tapped, the application calls the same method, but furnishes no information about the notification.
So I am trying to create a check that tries to connect to the WWW. when it fails it needs to then retry several times before the application gives up and quits. Each time it retries the user is propted with an UIAlertView with the options to Retry or Cancel.
So here is the problem.
I have a chain of actions in an NSOperationQueue, all the operations should fail with no connection. I"m using the NSoperation Queue so that the UI dosn't lock and the data is being processed in the background.
inside an NSInvocationOperation my method will hit [AlertView show], however this is not truly modal.
My operation then returns and continues through the chain of NSOperations, as there seems to be no way to return them with an Error value to stop additional processing. Eventually the UI catches up, displays the Modal AlertView, but i have no context of what has happened.
I am sure this is a common requirement. any ideas how to achieve this?
If I understand you correctly, you want a modal version of UIAlertView, but only modal within the calling thread/NSOperation? A few problems with this:
You should probably only call interface operations from the main thread (easily addressed using performSelectorOnMainThread:)
Modal dialogs are not really part of the OS; you'll need to address this programatically.
I am uploading a bunch of files from the iPhone to a web service. I want to have a UIAlertView on screen with a UIActivityIndicatorView inside. I got that to work fine. However, I want to update the title of the UIAlertView as each file gets uploaded. ("Uploading file 1...", "Uploading file 2...", etc.)
I know that you can't just set the title in a loop with synchronous web requests as the UI run loop will never get called. I tried using NSTimer to fire off the web requests but since each request duration is unpredictable, that doesn't work. (The message could update before the request is actually finished.)
I really want to upload each file synchronously, one at a time, as the iPhone bandwidth is pretty limited. I just can't figure out a mechanism to say 'once this synchronous operation finishes, let the UI update for a tick, then do another synchronous operation.'
OK here's the approach I took to solve this:
Create the UIAlertView
Start an asynchronous web request to post the item, set the delegate to self
In the connectionDidFinishLoading: method, check to see if there are more items to post. If there are, update the title and kick off another async request. If not, dismiss the alert.
It means you have to maintain a few class variables to track which request is uploading but it works perfectly and doesn't involve any complicated threading or anything else.
did you try this?
alertView.title = #"new title";
[alertView setNeedsDisplay];