Add Pop-up for database update - iphone

I'm a new iphone developer and getting ready to launch my first app. I have an reference utility app that I developed with help from an experienced developer to provide a process to upload database update to SQLite from access. When the updates occur, I have a popup window appearing which prompts the user to accept the data update or not. However, if they accept the update I want a popup to display during the update process. What do I need to do to ad this to this code?
if( [elementName isEqualToString:#"is_update_availableResult"] )
{
if([soapResults isEqualToString:#"yes"])
{
soapResults = [[NSMutableString alloc] init];
NSString *strMessage=#"An update is available. Select OK to update or cancel to load later. Please wait while system loads data before using app. ";
altView=[[UIAlertView alloc] init];
altView.title=#"MY APP";
altView.delegate=self;
[altView addButtonWithTitle:#"OK"];
[altView addButtonWithTitle:#"Cancel"];
[altView setCancelButtonIndex:0];
altView.message=strMessage;
[altView show];
}

Use the below code to implement the progress indication for your data base operation.
Declare UIAlertView* myAlert in your class header file.
myAlert = [[UIAlertView alloc] initWithTitle:#"Updating database…" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
[myAlert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(myAlert.bounds.size.width / 2, myAlert.bounds.size.height - 50);
[indicator startAnimating];
[myAlert addSubview:indicator];
[indicator release];
Once your database operation is completed dismiss the UIAlertView
-(void) OperationCompleted
{
[myAlert dismissWithClickedButtonIndex:0 animated:YES];
}

Related

Objective C - Using Reachability to constantly check for network connection during download

I am using this piece of code to check for a network connection within my application and, if a connection exists, pull data and display it:
if([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {
errorView = [[UIAlertView alloc]
initWithTitle: #"Network Error"
message: #"No Network connection availible!"
delegate: self
cancelButtonTitle: #"OK" otherButtonTitles: nil];
[errorView show];
}
else
{
HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
[self.navigationController.view addSubview:HUD];
HUD.delegate = self;
HUD.labelText = #"Performing Initial Download";
HUD.minSize = CGSizeMake(135.f, 135.f);
[HUD showWhileExecuting:#selector(pullAndDisplayData) onTarget:self withObject:nil animated:YES];
}
However, I would like to adapt this code so that it constantly checks for internet connection throughout the download process, and if I lose connection, to stop the download and display and appropriate alert message to the user. Can anyone advise as to how I may go about this?
Thanks,
Tysin
You need to add an observer for notification name
kReachabilityChangedNotification
and then call
[[Reachability reachabilityForInternetConnection] startNotifier];
When reachability changes, the notification will be posted, and then you can perform any actions you need.

UIAlertView from Tab Button - No Internet Connection

Looking for some help again...
I've currently got 2 sections of my app that need internet connection, 1 is a photo gallery through Flickr and the other a Twitter Feed, I was wondering if this is the correct code for the UIAlertView... I can get it working from a button but thats all!
{
-(void)didTap_roundedRectButton1:(id)sender forEvent:(UIEvent *)event {
UIAlertView *alertView = [[UIAlertView alloc] init];
alertView.title = #"You are not connected to the Internet!";
alertView.message = #"Please check your connection and try again...";
[alertView addButtonWithTitle:#"Ok"];
[alertView show];
[alertView release];
}
i think you need to use this example as i have used it to check is there any network is available and device is connected to any one of the network it is alos available on the apple's developer example site
example link is this
You should try this:
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"You are not connected to the Internet!"
message:#"Please check your connection and try again..."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alertView show];
[alertView release];
and can you post the code how do you create the UIButton?

MFMailComposeViewController usage and Apple aproval process

In my app, I have a logs mechanism, which offer the possibility to the customer to send the logs via mail.For this, I integrated in my app the Apple MFMailComposeViewController. In case that the customer use a device with low OS version (2.x) or an e-mail account isn't presented on the device, I pushed some UIAlertsView with some suggestive messages for users. Can somebody please take a look over my below code, and reply if there is something that could lead to a rejection by Apple?
BOOL canSendmail = [MFMailComposeViewController canSendMail];
if (!canSendmail) {
NSMutableString* osVersion = [NSMutableString stringWithString:[[UIDevice currentDevice] systemVersion]];
EventsLog* logs = [EventsLog getInstance];
if ([osVersion characterAtIndex : 0] == '2' || [osVersion characterAtIndex : 0] == '1' ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Email", #"")
message:NSLocalizedString(#"Failed to send E-mail.For this service you need to upgrade the iPhone OS to 3.0 version or later", #"")
delegate:self cancelButtonTitle:NSLocalizedString(#"OK", #"") otherButtonTitles: nil];
[alert show];
[alert release];
[logs writeEvent : #"Cannot send e-mail - iPhone OS needs upgrade to at least 3.0 version" classSource:#"LogsSessionDetailViewController#sendEmail" details : (#" device OS version is %#",osVersion)];
return;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Email", #"")
message:NSLocalizedString(#"Failed to send E-mail.Please set an E-mail account and try again", #"")
delegate:self cancelButtonTitle:NSLocalizedString(#"OK", #"") otherButtonTitles: nil];
[alert show];
[alert release];
[logs writeEvent : #"Cannot send e-mail "
classSource:#"LogsSessionDetailViewController#sendEmail" details : #"- no e-mail account activated"];
return;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Email", #"")
message:NSLocalizedString(#"The data you are sending will be used to improve the application. You are free to add any personal comments in this e-mail", #"")
delegate:self cancelButtonTitle:NSLocalizedString(#"Cancel", #"") otherButtonTitles: nil];
[alert addButtonWithTitle:NSLocalizedString(#"Submit", #"")];
[alert show];
[alert release];
Many thanks,
Alex.
I won't say about appstore admission/rejection but your code must crash on iPhone OS 2.x - you call
BOOL canSendmail = [MFMailComposeViewController canSendMail];
without checking if this call is possible (MFMailComposeViewController class is not available on 2.x system). Also manual checking of OS version is not a good practice. Instead you must at first check if MFMailComposeViewController present in current runtime:
if ( !NSClassFromString(#"MFMailComposeViewController") ){
// Put code that handles OS 2.x version
return;
}
if (![MFMailComposeViewController canSendMail]){
// Put code that handles the case when mail account is not set up
return;
}
//Finally, create and send your log
...
P.S. Do not forget that you must set linkage type for MessageUI framework as 'weak' in target settings - you application will crash on old systems on start if you linkage type will be 'required' (default value).

problem with logging in twitter through my iphone application

i am creating a music application in which i am integrating twitter in my application
When a user clicks on particular song and then he click on the twitter tab the login page of twitter should be displayed. after entering the username and password he should be directed to the page where he can enter his comment and then post it.the problem is that when i click on twitter tab the login page is diplayed but when i enter he username and password and click on submit it does not redirect me to the page where i can post my comment. Here is my code:
(IBAction) Submit: (id) sender
{
// g=text3.text.integerValue;
u=text1.text;
p=text2.text;
int flag;
NSLog(#"Username,%d",u);
//if((u!=#"") || (p!=#""))
//if((text1.text.length >= 20 && range.length !=0) ||(text2.text.length >= 8 && range.length!=0))
if([text1.text length] != 0 || [text2.text length]!=0)
{
TwitterRequest * t = [[TwitterRequest alloc] init];
t.username =[NSString stringWithFormat: #"%#",u];
//NSString *username=[NSString stringWithFormat:#"%d",u];
t.password= [NSString stringWithFormat: #"%#",p];
//NSLog(#"Password,%d",p);
//#"ajeetyaday";
//t.username= #"ajeetyaday";
//t.password = #"gopalpur";
//NSString *name1 =[NSString stringWithFormat:#"%d",g]
//twitterMessageText.text=#" hi i am ajeet";
//[twitterMessageText resignFirstResponder];
loadingActionSheet = [[UIActionSheet alloc] initWithTitle:#"Posting To Twitter..." delegate:nil
cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[loadingActionSheet showInView:self.view];
[t statuses_update:twitterMessageText.text delegate:self requestSelector:#selector(status_updateCallback:)];
//[TwitterRequest statuses_update:twitterMessageText.text delegate:self requestSelector:#selector(status_updateCallback:)];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Clicked on Submit" message:#"Please inter Username and Password" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
I think u r in wrong way!!
U simply store user name and password in a global variable,and push to posting page
(IBAction) Submit: (id) sender
{
user_name=uname.text;
password=password.text;
Twitter *Controller = [[Twitter alloc] init];
[self.navigationController pushViewController:Controller animated:YES];
[Controller release];
}

UIAlertView Pops Up Three Times per Call Instead of Just Once

I am getting odd behavior from an NSAlert in two different parts of my program. The behavior is:
Alert appears and then spontaneously disappears.
Alert reappears and then remains until dismissed by user i.e. normal behavior.
Alert reappears again.
This behavior only occurs the first time the method that displays the alert is called. After that first time, it behaves normally.
Here is the code for the one of the parts in which the behavior occurs:
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
Or if you prefer, with a bit more context:
- (IBAction)locateMe {
NSLog(#"About to check location");
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locMan.distanceFilter = 1609; //1 mile
[locMan startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation * )oldLocation {
if (newLocation.horizontalAccuracy >= 0) {
CLLocation *airportLocation = [[[CLLocation alloc] initWithLatitude:51.500148 longitude:-0.204669] autorelease];
CLLocationDistance delta = [airportLocation getDistanceFrom: newLocation];
long miles = (delta * 0.000621371) + 0.5; //metres to rounded mile
if (miles < 3) {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
[locMan stopUpdatingLocation];
} else {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are not in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
[locMan stopUpdatingLocation];
}
}
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"Error." message:error.code delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locMan release];
locMan = nil;
}
Any ideas? Thanks.
Edit---------
The other place this happens is:
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:#"Unable to download feed from web site (Error code %i )", [parseError code]];
NSLog(#"error parsing XML: %#", errorString);
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
For context the first case is in the AppDelegate and the second in the view controller for the 1st tab view. The second problem occurs every time the xml is reloaded when there is no internet connection. The first one only occurs the first time the function is called.
Edit-----
If I move the alert it works. Unfortunatly this is not where I want it!
- (IBAction)locateMe {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
/*
NSLog(#"About to check location");
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locMan.distanceFilter = 1609; //1 mile
[locMan startUpdatingLocation];*/
}
Update:
I set some NSLog entries and discovered that despite the addition of [locMan stopUpdatingLocation] the didUpdateToLocation function was running multiple times.
I guess the spontaneous disappearance happens because the alert view is called again and the programme clears the first instance to make way for the second automatically.
Any ideas as to why [locMan stopUpdatingLocation] doesn't work would be appreciated but in the mean time I just moved the declaration of the locationAlert out of the function (so it is global), set it in the initial locate me function and use the following the first time it is called:
[locationAlert show];
locationAlert = nil;
That way it works perfectly.
You're not turning off your location manager when you first show the alert. As the location is refined by the device (ie, the accuracy is increased), your callback will be (potentially) called multiple times. You should use [locMan stopUpdatingLocation] after your alert display.
I set some NSLog entries and discovered that despite the addition of [locMan stopUpdatingLocation] the didUpdateToLocation function was running multiple times.
I guess the spontaneous disappearance happens because the alert view is called again and the programme clears the first instance to make way for the second automatically.
Any ideas as to why [locMan stopUpdatingLocation] doesn't work would be appreciated but in the mean time I just moved the declaration of the locationAlert out of the function (so it is global), set it in the initial locate me function and use the following the first time it is called:
[locationAlert show];
locationAlert = nil;
That way it works perfectly.
I think the NSAlert disappearing on its own is the key to solving this.
It's simple to explain why an alert displays unexpectedly i.e. it's just been called unexpectedly. However, it's not so common to programmatically dismiss an alert. Whatever is causing it to disappear is most likely triggering the display again.
To debug I suggest:
(1) Looking in your code for the NSAlert – dismissWithClickedButtonIndex:animated: method and see if somehow you're actually dismissing the alert programmatically.
(2) I believe (someone double-check me on this) that an alert view is added as a subview to whichever base view is currently on screen. It might be that the base view is disappearing for some reason and taking the alert view with it. If the view disappears and then reappears rapidly enough, it might not be obvious when the alert is frontmost. (Edit: see Ed Marty's comment below.)
(3) Since this happens in two separate pieces of the app, compare both to find a common element or structure. That common element might be the cause.
An odd problem.
Edit01: Updated for additional info
If locMan isan instance variable, it should be defined as a property and you should access it every time withself.locMan By accessing it directly, you lose your automatic retention management.
I encountered the same exact issue with the alert dialog appearing momentarily, reappearing, and finally appearing again after being dismissed. I was making a string comparison before deciding to show the alert view:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if([string isEqualToString:#"OK"]) {
NSLog(#"(Settings)Registration Successful");
statusField.text = #"Registration successful!";
[settingsActivity stopAnimating];
}
else {
NSLog(#"(Settings)Registration Failure");
[settingsActivity stopAnimating];
UIAlertView * regFail = [[[UIAlertView alloc] initWithTitle:#"Registration Error!" message:#"Please check your email address and try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[regFail show];
}}
To correct this behavior I simply verified the returned string rather than just showing the alert:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if([string isEqualToString:#"OK"]) {
NSLog(#"(Settings)Registration Successful");
statusField.text = #"Registration successful!";
[settingsActivity stopAnimating];
}
else if([string isEqualToString:#"Error"]) {
NSLog(#"(Settings)Registration Failure");
[settingsActivity stopAnimating];
UIAlertView * regFail = [[[UIAlertView alloc] initWithTitle:#"Registration Error!" message:#"Please check your email address and try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[regFail show];
}
I also got the Same problem while working on Location Manager. Here i checked with Nslog but it is executing multiple times, finally i fount that i am creating multiple objects and using Sharedinstance for same ViewController that contains Location Manger but i am not releasing the object, so at perticular location how many objects if we create that many times the location detects.So while working on LocationManger check handling objects thoroughly to reduce these type of problems.