iphone - uiactivityindicatorview does not hide - iphone

i know this is a very common question, i read most of the questions asked in stackoverflow but still couldn't figure out how to remove the uiactivityindicatorview from the view.
please find below the code
#implementation FeedBackViewController
#synthesize m_activity,webView;
-(void)viewDidLoad{
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://equinox.library.pitt.edu/limesurvey/index.php?sid=87435&lang=en"]]];
}
- (void)dealloc {
[m_activity release];
[webView release];
[super dealloc];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[m_activity stopAnimating];
[m_activity removeFromSuperview];
m_activity = nil;
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
NSLog(#"in webViewDidFinishLoad")
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
m_activity.center = self.view.center;
[self.view addSubview: m_activity];
[m_activity startAnimating];
}
the header file
#interface FeedBackViewController : UIViewController<UIWebViewDelegate>{
IBOutlet UIWebView *webView;
IBOutlet UIActivityIndicatorView *m_activity;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#property (nonatomic, retain) UIActivityIndicatorView *m_activity;
Also, i have checked the box in the IB that says, hide when stopped. I put NSLog(#"in webViewDidFinishLoad") in the webViewFinishLoad method, seems like it does not hit that method for some reason.
and for
NSLog(#"m_activity = %#",m_activity);
in the viewDidLoad method, it gives a message as below in the console
> m_activity = <UIActivityIndicatorView:
> 0x4d3d920; frame = (150 79; 20 20);
> hidden = YES; opaque = NO; autoresize
> = RM+BM; layer = <CALayer: 0x4d35ce0>>

Your webViewDidFinishLoad function is probably not being called. It may happen if the web view fails to open the url. In any case, you should also implement webView:didFailLoadWithError: method and stop the activity indicator there too.
-edit-
To receive events from UIWebView you need to set the controller as the UIWebView's delegate. Just implementing the UIWebViewDelegate is not sufficient. You can do this by updating viewDidLoad method as follows:
-(void)viewDidLoad{
[super viewDidLoad];
webView.delegate = self;
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://equinox.library.pitt.edu/limesurvey/index.php?sid=87435&lang=en"]]];
}

You should add NSLog(#"in webViewDidFinishLoad") lines to your code and watch the console to see if the code hits those points. Since this is a very simple example, perhaps m_activity is not connected to the actual object? Try to see if you get an address or a null when you add this to the end of your viewDidLoad:
NSLog(#"m_activity = %#",m_activity);

Related

UIWebView is not loading url

I know this is a common question but none of the answers fix my problem. I have a UIWebView in my app. I have everything set up correctly; the delegate is set, the webView is a property of my view controller. It is set up to load an html string upon loading the viewController. When the html string loads, in the webViewDidFinishLoad method it checks if this is the first time it loaded, and if it is it is told to load a request with a url. The string i use for the url is a property of my viewController. That string is exactly what it is supposed to be. There is just something preventing the webView from actually loading the request. Any ideas would be very much appreciated. Thank you
#interface WebViewController : UIViewController <UIWebViewDelegate, UIActionSheetDelegate, UIPrintInteractionControllerDelegate, MFMailComposeViewControllerDelegate, NSURLConnectionDelegate>
#property (nonatomic, assign) BOOL firstLoad;
#property (nonatomic, retain) UIWebView *webView;
#property (nonatomic, retain) NSString *prefixURLString;
#property (nonatomic, retain) NSString *suffixURLString;
#property (nonatomic, retain) UIActionSheet *actionSheet;
- (id)initWithTitle:(NSString *)newTitle andSuffixURL:(NSString *)suffix;
- (void)reload;
- (void)addActionButton;
- (void)showActions:(UIBarButtonItem *)sender;
- (void)dismissActionSheet;
#end
#implementation WebViewController
#synthesize webView;
#synthesize prefixURLString;
#synthesize suffixURLString;
#synthesize firstLoad;
#synthesize actionSheet;
- (id)initWithTitle:(NSString *)newTitle withPrefixURL:(NSString *)prefix andSuffixURL:(NSString *)suffix
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
// Custom initialization
[self setPrefixURLString:prefix];
[self setFirstLoad:YES];
[self setTitle:newTitle];
[self setSuffixURLString:suffix];
[self setActionSheet:nil];
}
return self;
}
- (id)initWithTitle:(NSString *)newTitle andSuffixURL:(NSString *)suffix
{
return [self initWithTitle:newTitle withPrefixURL:#"https://customer.stld.com/flatroll/ios/%#" andSuffixURL:suffix];
}
- (void)reload
{
if(self.prefixURLString && self.suffixURLString)
{
NSString *fullURLString = [NSString stringWithFormat:self.prefixURLString, [self.suffixURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:fullURLString]]];
}
}
- (void)dealloc
{
[suffixURLString release]; suffixURLString = nil;
[prefixURLString release]; prefixURLString = nil;
[actionSheet release]; actionSheet = nil;
[super dealloc];
}
- (void):(UIWebView *)aWebView didFailLoadWithError:(NSError *)error
{
//NSLog(#"WebViewController:DidFailLoadWithError: %#", [error localizedFailureReason]);
[self setTitle:#"Loading Error"];
[self.webView loadHTMLString:[NSString stringWithFormat:#"<html><head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head><body>Loading error:<br />%#</body></html>", [error localizedFailureReason]] baseURL:nil];
}
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
if(self.firstLoad)
{
[self setFirstLoad:NO];
[self reload];
[self addActionButton];
}
}
#pragma mark - View lifecycle
- (void)loadView
{
UIWebView *newWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
[newWebView setDelegate:self];
[newWebView setScalesPageToFit:YES];
[newWebView setUserInteractionEnabled:YES];
[self setWebView:newWebView];
[self setView:newWebView];
[newWebView release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.webView loadHTMLString:#"<html><head><meta name=\"viewport\" content=\"width=device-width, user-scalable=yes\" /></head><body><br />Gathering data and generating report.<br />Depends on data and parameters you have requested, this could take time. Please wait...</body></html>" baseURL:nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
[self.webView setDelegate:nil];
[self setWebView:nil];
[self setView:nil];
}
I had the same problem, I'm not entirely sure why, but I guess it has to do something with viewControllers life cycle. Anyway try load URL in viewDidAppear method. It fixed it for me. Hope it will help
It turns out my problem had nothing to do with the UIWebView. I just had to change the credential persistence in the URLConnectionDelegate I had set up for my app. I had it set as NSURLCredentialPersistanceNone but I changed it to NSURLCredentialPersistenceSession. I hope this can help anyone else out there who had the exact problem as I did

iPhone - UIActivityIndicatorView does not hide when stopped

I have a custom UIWebView written like this :
.h
#interface MiniWebViewController : UIViewController {
NSString* destinationURL;
UIWebView* webView;
UIActivityIndicatorView* activityIndicatorView;
}
#property (nonatomic, retain) NSString* destinationURL;
#property (nonatomic, retain) IBOutlet UIWebView* webView;
#property (nonatomic, retain) IBOutlet UIActivityIndicatorView* activityIndicatorView;
- (void) run;
#end
.m
#synthesize destinationURL;
#synthesize webView;
#synthesize activityIndicatorView;
- (id) initWithFrame:(CGRect)frame {
if (self = [super initWithNibName:#"MiniWebView" bundle:nil]) {
self.destinationURL = #"";
self.view.frame = frame;
self.activityIndicatorView.center = self.webView.center;
}
return self;
}
- (void) run {
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.destinationURL]]];
}
It is called an initied from another ViewController :
- (void) aFunction {
MiniWebViewController* oneViewController = [[MiniWebViewController alloc] initWithFrame:CGRectMake(/*Some Rect*/];
oneViewController.webView.tag = i;
oneViewController.destinationURL = /*SomeURL*/;
oneViewController.webView.delegate = self;
[self.view addSubview:oneViewController.view]; /* the Web view is inside this one */
[oneViewController run];
}
- (void) webViewDidFinishLoad:(UIWebView *)webView {
int webViewID = webView.tag;
MiniWebViewController* webViewController = [self.webViews objectAtIndex:webViewID];
[webViewController.activityIndicatorView stopAnimating];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
int webViewID = webView.tag;
MiniWebViewController* webViewController = [self.webViews objectAtIndex:webViewID];
[webViewController.activityIndicatorView startAnimating];
}
Into IB hidesWhenStopped is also checked. Everything is linked. The style of the indicator is set to "Large White" into IB.
When running, the indicator is not large, but small.
It is successfully started and stopped (delegate calls are triggered), but it doesn't hide when stopped.
What's the problem ? I don't see...
After a careful look in your code, you are modifying the UIActivityView in your init method. Change those so that they are in your viewDidLoad. At init, you view is not yet loaded, therefore, there is not an instance of those objects yet created in your controller.
These are the statement that need to be moved:
self.activityIndicatorView.hidesWhenStopped = YES;
self.view.frame = frame;
self.activityIndicatorView.center = self.webView.center;
This goes back to a fundamental of Objective-C: Sending a message to a nil object...returns nil. This is an annoying feature at times because there is no compile time warning, nor is there any runtime exception--a feature of the language.
Stupid I am, and XCode/cocoa really doesn't help.
I had loaded a wrong XIB name that DOES NOT exist.
So I ask to load something that does not exist, and the app stil works, even showing and using objects that does not exist. Untill one call does not work as expected. No crash...
It's nonsense.

How to make a class as a delegate for another?

I am learning objective-c and I get some trouble with delegates.
In a test application, i have a viewcontroller having a IUbutton, and another UITableViewcontroller. I want to make the TableViewController appears when I click the button.
the problem that the window object is not identified in this ViewController.
[self.window addSubview:viewController.view]; this line shows errors.
I think I should delegate the appdelagate to my viewcontroller ? how to do ?
Add this in the applicationDidFinishLaunching method of app delegate
[self.window addSubview:viewController.view];
Or if you want to access the main window in a view controller u can refer this link
Referencing AppDelegate instance variables
you cant access the window by self you need an object of appDelegate class
so code like this
yourAppDelegate *objDelegate=(yourAppDelegate *)[[UIApplication sharedApplication] delegate];
now you can access the window,
[objDelegate.window addSubview:viewController.view];
I just copy pasted this code form my project.. I have created a delegate in this class "appImageDidLoad" this delegate called when the image downloaded completely. Hope it will help you can post comment if you need any explaination
#import "MixtapeInfo.h"
#class Record;
#protocol IconDownloaderDelegate;
#interface IconDownloader : NSObject
{
MixtapeInfo *appRecord;
NSIndexPath *indexPathInTableView;
id <IconDownloaderDelegate> delegate;
NSMutableData *activeDownload;
NSURLConnection *imageConnection;
}
#property (nonatomic, retain) MixtapeInfo *appRecord;
#property (nonatomic, retain) NSIndexPath *indexPathInTableView;
#property (nonatomic, assign) id <IconDownloaderDelegate> delegate;
#property (nonatomic, retain) NSMutableData *activeDownload;
#property (nonatomic, retain) NSURLConnection *imageConnection;
- (void)startDownload;
- (void)cancelDownload;
#end
#protocol IconDownloaderDelegate
- (void)appImageDidLoad:(NSIndexPath *)indexPath;
#end
#import "IconDownloader.h"
#import "MixtapeInfo.h"
#define kAppIconHeight 48
#define TMP NSTemporaryDirectory()
#implementation IconDownloader
#synthesize appRecord;
#synthesize indexPathInTableView;
#synthesize delegate;
#synthesize activeDownload;
#synthesize imageConnection;
#pragma mark
- (void)dealloc
{
[appRecord release];
[indexPathInTableView release];
[activeDownload release];
[imageConnection cancel];
[imageConnection release];
[super dealloc];
}
- (void)startDownload
{
self.activeDownload = [NSMutableData data];
// alloc+init and start an NSURLConnection; release on completion/failure
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:
[NSURLRequest requestWithURL:
[NSURL URLWithString:appRecord.mixtape_image]] delegate:self];
self.imageConnection = conn;
[conn release];
}
- (void)cancelDownload
{
[self.imageConnection cancel];
self.imageConnection = nil;
self.activeDownload = nil;
}
#pragma mark -
#pragma mark Download support (NSURLConnectionDelegate)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.activeDownload appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// Clear the activeDownload property to allow later attempts
self.activeDownload = nil;
// Release the connection now that it's finished
self.imageConnection = nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// Set appIcon and clear temporary data/image
UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];
self.appRecord.mixtape_image_obj = image;
self.activeDownload = nil;
[image release];
// Release the connection now that it's finished
self.imageConnection = nil;
// call our delegate and tell it that our icon is ready for display
[delegate appImageDidLoad:self.indexPathInTableView];
}
#end
i would guide you through this in a tutorial.
1) Create a new project in xcode using view application template
2) Open up your view's xib file
3) Drag a UITableView and a UIButton from the library to the view.
4) Select your UITableView view and checked the hidden property.
5) Go to your view's header file and declare two IBOutlets (for your button and table), 1 IBAction (for your button's target action).
6) Link up the outlets to the respective UI objects in your Interface Builder and set the target action of the button (touch up event) to your controller.
7) In your IBAction, set the hidden property of the UITableView to NO and button's hidden property to YES.
8) Volia, you should see your table view now when you click on the button and the button would become hidden after you clicked
I skip the part on adding the tableview delegates so you will not see anything in your table. You can check them out on Apple's UITableView example.
EDIT:
In your IBAction for the button, put something similar like this
- Require a navigation controller:
[self.navigationController pushViewController:putTheNameOfYourTableViewControllerHere animated:YES];
- Present in a modal view:
[self presentModalViewController:putTheNameOfYourTableViewControllerHere animated:YES];

iPhone iOS4 low-level camera control?

Is there a way to manually set low-level still-camera settings like shutter speed, aperture, or ISO in iOS4 on the iPhone 4? I don't think it exists in the official SDK but perhaps someone has found some private APIs to allow this?
I find my iPhone 4 camera to be unusable because even in fairly decent lighting, it always insists on shooting at the slowest 1/15th a second shutter speed causing motion blur if the subject is moving at all.
Thanks!
Not directly. Please file a bug report.
Yes, there may be private APIs available, but that's of limited use.
Try this, I could be useful for you:
#interface MyViewController ()
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
#property (nonatomic, retain) IBOutlet UIToolbar *myToolbar;
#property (nonatomic, retain) OverlayViewController *overlayViewController;
#property (nonatomic, retain) NSMutableArray *capturedImages;
// toolbar buttons
- (IBAction)photoLibraryAction:(id)sender;
- (IBAction)cameraAction:(id)sender;
#end
#implementation MyViewController
- (void)viewDidLoad
{
self.overlayViewController =
[[[OverlayViewController alloc] initWithNibName:#"OverlayViewController" bundle:nil] autorelease];
// as a delegate we will be notified when pictures are taken and when to dismiss the image picker
self.overlayViewController.delegate = self;
self.capturedImages = [NSMutableArray array];
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
// camera is not on this device, don't show the camera button
NSMutableArray *toolbarItems = [NSMutableArray arrayWithCapacity:self.myToolbar.items.count];
[toolbarItems addObjectsFromArray:self.myToolbar.items];
[toolbarItems removeObjectAtIndex:2];
[self.myToolbar setItems:toolbarItems animated:NO];
}
}
- (void)viewDidUnload
{
self.imageView = nil;
self.myToolbar = nil;
self.overlayViewController = nil;
self.capturedImages = nil;
}
- (void)dealloc
{
[_imageView release];
[_myToolbar release];
[_overlayViewController release];
[_capturedImages release];
[super dealloc];
}
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType
{
if (self.imageView.isAnimating)
[self.imageView stopAnimating];
if (self.capturedImages.count > 0)
[self.capturedImages removeAllObjects];
if ([UIImagePickerController isSourceTypeAvailable:sourceType])
{
[self.overlayViewController setupImagePicker:sourceType];
[self presentModalViewController:self.overlayViewController.imagePickerController animated:YES];
}
}
- (IBAction)photoLibraryAction:(id)sender
{
[self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
}
- (IBAction)cameraAction:(id)sender
{
[self showImagePicker:UIImagePickerControllerSourceTypeCamera];
}
// as a delegate we are being told a picture was taken
- (void)didTakePicture:(UIImage *)picture
{
[self.capturedImages addObject:picture];
}
// as a delegate we are told to finished with the camera
- (void)didFinishWithCamera
{
[self dismissModalViewControllerAnimated:YES];
if ([self.capturedImages count] > 0)
{
if ([self.capturedImages count] == 1)
{
// we took a single shot
[self.imageView setImage:[self.capturedImages objectAtIndex:0]];
}
else
{
// we took multiple shots, use the list of images for animation
self.imageView.animationImages = self.capturedImages;
if (self.capturedImages.count > 0)
// we are done with the image list until next time
[self.capturedImages removeAllObjects];
self.imageView.animationDuration = 5.0; // show each captured photo for 5 seconds
self.imageView.animationRepeatCount = 0; // animate forever (show all photos)
[self.imageView startAnimating];
}
}
}
#end

I can't get UIWebViewDelegate to work

I'm trying to implement a UIWebViewDelegate in my application and I cannot for the life of me get it to work. I would really appreciate a second set of eyes on this.
I know the name MapViewController is confusing, but the WebView is controlling a map (not UIMapView).
Here's the bulk of the code:
MapViewController.h
#import <UIKit/UIKit.h>
#import <UIKit/UIWebView.h>
#interface MapViewController : UIViewController<UIWebViewDelegate> {
IBOutlet UIWebView *webView;
}
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#end
MapViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
webView.delegate = self;
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"map" ofType:#"html"]isDirectory:NO]];
[webView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSLog(#"Done loading.");
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"hi");
return NO;
}
- (void)dealloc {
webView.delegate = nil;
[webView release]
[super dealloc];
}
Thanks for any help!
Does the initial page you're loading actually display? Are you sure you connected your webview in Interface Builder?
go interface builder , connect the delegate to the file's owner , then it should work.
Open Interface Builder
Right click on the Web View object
Drag the delegate onto files owner.
Things to check:
if map.html is in the bundle. Put it on the root of the bundle, not on resources.
if it is, right click on it, and choose get info. Verify on TARGETS if the target is checked for the file.
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(#"hi");
return YES; // for all supported oreintation.
}