I am developing an application, it gets data from accessory via UART port. I am having a problem when my application runs for a long time, it uses much more memory, after the iPhone into sleep mode, and wake up again, my application can not open the session with accessory after the accessory has been authenticated completely. After I debug class EAAccessoryManager, I see two identical devices, they are all my accessory. Go to Settings / General / About, I found that iphone show two identical devices there.
But my problem occurs only on the iPhone 3G (version iOS 4.1), The iPhone 3GS (iOS 4.1) does not have this problem. I guess because my program uses too much memory so I can not get accessoryDidDisconnect events. Please give me some advices. thank for your answer.
-(EASession*) openSessionForProtocol: (NSString*)protocolString
{
NSArray* accessories = [[EAAccessoryManager sharedAccessoryManager] connectedAccessories];
EAAccessory* accessory = nil;
EASession *session = nil;
for(EAAccessory* obj in accessories){
if([[obj protocolStrings] containsObject:protocolString]){
accessory = obj;
break;
}
}
if(accessory){
[accessory setDelegate:self];
session = [[EASession alloc] initWithAccessory:accessory forProtocol:protocolString];
if(session){
NSString *msg = #"";
for(EAAccessory* obj in accessories){
msg = [msg stringByAppendingFormat:#"\n%#",[obj name]];
}
NSString *openSession = [NSString stringWithFormat:#"The number of devices is: %d.%#",[accessories count],msg];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"OpenSession" message:openSession delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
[[session inputStream] setDelegate:self];
[[session inputStream] scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[[session inputStream] open];
[[session outputStream] setDelegate:self];
[[session outputStream] scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[[session outputStream] open];
[session autorelease];
iRemoteAppDelegate *appDelegate = (iRemoteAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate SetApplicationRotation:TRUE];
}
}
return session;
}
- (void)accessoryDidDisconnect:(EAAccessory *)accessory
{
//[HardwareController performSelectorOnMainThread:#selector(UpdateStringOnMessage:) withObject:#"Can not connect hardware module.\nPlease check hardware again." waitUntilDone:YES];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Accessory is unpluged!" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
[[serialSession inputStream] removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[[serialSession outputStream] removeFromRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[serialSession release];
self.serialSession = nil;
iRemoteAppDelegate *delegate = (iRemoteAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate setUserCancel:NO];
AllowedEmitSignal = TRUE;
[delegate UpdateAboutHardwareDisconnect];
[delegate SetApplicationRotation:FALSE];
}
- (void)accessoryDidConnect:(NSNotification *) notification
{
iRemoteAppDelegate *appDelegate = (iRemoteAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate setUserCancel:NO];
[self OpenPort];
AllowedEmitSignal = TRUE;
[appDelegate UpdateAboutHardwareDisconnect];
appDelegate.CallNumber = appDelegate.CallNumber+1;
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message" message:[NSString stringWithFormat:#"Accessory is attached!%d",appDelegate.CallNumber] delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
-(void)OpenPort
{
int i =0;
[self initAllVariable];
iRemoteAppDelegate *delegate = (iRemoteAppDelegate *)[[UIApplication sharedApplication] delegate];
for (;self.serialSession==nil && i<2; i++) {
self.serialSession = [self openSessionForProtocol:PROTOCOLSTRING];
}
}
If Settings / General / About shows two identical devices for iPhone 3G, this means the iPhone 3G has failed to detect the "the off" state of accessory when it sleeps. You have to ensure the accessory also went to sleep (low power state) when the iPhone 3G sleeps, within certain milliseconds after your accessory receives bytes from 3G telling it's changing its power state.
Cannot tell too much secret here. But from my experience, iPhone 3G has much difference electrical signal behavior with 3GS. Your problem has no relationship with iOS app code. I strongly suggest you to use CRO / Logic Analyzer to debug the detect pins state and commands sent from iPhone 3G just before it goes to sleep.
Related
From my object that handles the web service connection, when the network fails, I pass an alert to the view controller that uses the web service object.
WebServiceObject:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:[NSString stringWithFormat:#"Connection failed! You must be connected to a Wifi source to download data. Please reconnect to a Wifi source and try again later."] delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
NSDictionary *alertDict = [NSDictionary dictionaryWithObjectsAndKeys:alert, #"AlertView", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:DisplayAlertNotification object:self userInfo:alertDict];
ViewController:
- (void)displayAlert:(NSNotification *)notification {
NSDictionary *dict = [notification userInfo];
if ([[dict objectForKey:#"AlertView"] isKindOfClass:[UIAlertView class]]) {
UIAlertView *alert = [dict objectForKey:#"AlertView"];
NSNumber *theTag = [dict objectForKey:#"AlertTag"];
NSLog(#"%i", [theTag integerValue]);
alert.tag = [[dict objectForKey:#"AlertTag"] integerValue];
[alert show];
}
}
- (void)removeAlert:(NSNotification *)notification {
NSDictionary *dict = [notification userInfo];
if ([[dict objectForKey:#"AlertTag"] isKindOfClass:[NSNumber class]]) {
NSNumber *theTag = [dict objectForKey:#"AlertTag"];
UIAlertView *alert = (UIAlertView *)[self.view viewWithTag:[theTag integerValue]];
// Not sure why but my alert is nil at this point
[alert dismissWithClickedButtonIndex:0 animated:YES];
}
}
I also use the removeAlert method in the same way to remove the alert programmatically. The goal of this is so if the network failed, but the user didn't click Ok yet, and then the network came back on, I would dismiss the Network Failed alert, and show the Network Resumed alert. It works except after it dismisses the alert and shows the Network Resumed, once the user clicks Ok on the Network Resumed, the original Network Failed comes back up just once. If the user clicked Ok while the Network Failed was presented, it never comes back up.
Am I dismissing the alert correctly this way? Thanks.
Edit: I can get it to work by just saving a reference in the WebServiceObject and dismissing it that way.
you set the alert to nil, so it do nothing
alert = nil;
[alert dismissWithClickedButtonIndex:0 animated:YES];
I am calling URL on my Search button event to load data. I have put the code for Loading indicator Alert view by taking NSThread. I am using 3.2 xcode with 4.3 iOS. Every thing run smooth but on search button it shows Loading indicator and then crashing and showing following in console
Program received signal: “EXC_BAD_ACCESS”.
warning: Unable to read symbols for /Developer/Platforms/iPhoneOS.platform/DeviceSupport/4.3.2 (8H7)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib (file not found).
Code under the Search Button click event:
- (IBAction) searchButton {
if([addressField.text length]==0)
{
UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:#"Alert" message:#"Please Tap on 'Show Me' & choose the 'Radius' first!!!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] autorelease];
[myAlert show];
}
else
{
[NSThread detachNewThreadSelector:#selector(updateFilterProgress) toTarget:self withObject:nil];
appDelegate = (MapTutorialAppDelegate *)[[UIApplication sharedApplication] delegate];
CLLocationCoordinate2D location;
float radius = [[arrayNo objectAtIndex:[pickerView selectedRowInComponent:0]] floatValue];
NSString *url = [NSString stringWithFormat:#"http://....url...../hespdirectory/phpsqlsearch_genxml.php?lat=%f&lng=%f&radius=%f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude,radius];
NSLog(#"%#", url);
NSURL *URL = [NSURL URLWithString:url];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
{
if([appDelegate.markers count] == 0){
UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:#"Alert" message:#"No results fond!!!" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] autorelease];
[myAlert show];
}
else
{
resultButton.userInteractionEnabled = YES;
for (int i = 0; i < [appDelegate.markers count]; i++)
{
marker *aMarker = [appDelegate.markers objectAtIndex:i];
location.latitude = [aMarker.lat floatValue];
location.longitude =[aMarker.lng floatValue];
AddressAnnotation *annob = [[AddressAnnotation alloc] initWithCoordinate:location];
annob.title = aMarker.name;
annob.subTitle = aMarker.address;
[mapView addAnnotation:annob];
[annob release];
CLLocationCoordinate2D ausLoc = {location.latitude,location.longitude}; //for zoom in the showroom results region
MKCoordinateSpan ausSpan = MKCoordinateSpanMake(0.108889, 0.169922);
MKCoordinateRegion ausRegion = MKCoordinateRegionMake(ausLoc, ausSpan);
NSLog(#"No Errors");
mapView.region = ausRegion;
}
}
}
else
NSLog(#"Error Error Error!!!");
[addressField resignFirstResponder];
}
}
And for NSThread to Show Loaading indicator while loading data at the back.
- (void) updateFilterProgress{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
Reachability *r = [Reachability reachabilityWithHostName:#"www.google.com"];
NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN))
{
UIAlertView *myAlert = [[[UIAlertView alloc] initWithTitle:#"No Internet Connection" message:#"This app require an internet connection via WiFi or cellular network to work." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil] autorelease];
[myAlert show];
}
else{
UIAlertView *alertMe = [[[UIAlertView alloc] initWithTitle:#"Loading..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alertMe show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
// Adjust the indicator so it is up a few pixels from the bottom of the alert
indicator.center = CGPointMake(alertMe.bounds.size.width / 2, alertMe.bounds.size.height - 50);
[indicator startAnimating];
[alertMe addSubview:indicator];
[indicator release];
[alertMe release];
for (int i = 200; i > [appDelegate.markers count]; i--)
{
marker *aMarker = [appDelegate.markers objectAtIndex:i];
[alertMe dismissWithClickedButtonIndex:0 animated:YES];
}
}
[pool release]; }
Is there anything remaining in my code. Pleas correct me....
The variable alertMe is autoreleased and therefore you can't just send a release message to it. Remove the line [alertMe release]; and it will run perfectly.
You are probably accessing a released object. To see which one it is, set NSZombiesEnabled - this will show you which already released object you try to access, and you should be able to identify your problem.
Please have a look here to see how to enable the zombies in XCode 4:
http://42games.net/quick-note-on-setting-nszombieenabled-environment-variable-in-xcode-4/
I have the following code:
//View guest list
-(IBAction) guestList:(id) sender{
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"myURL"]];
//Waits a set peroid of time
wait(20000);
//Guest list is availible
if (connected != NULL){
CHARLIEAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate displayView:6];
}
//No network connection availible
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Network Connection!" message:#"Cannot establish internet connection." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
And I get the following warnings:
//Waits a set peroid of time
wait(20000);
Gives me -
warning: passing argument 1 of 'wait' makes pointer from integer without a cast
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://photostiubhart.comoj.com/testconnection.php"]];
Gives me -
warning: 'stringWithContentsOfURL:' is deprecated (declared at /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSString.h:384)
I've done my testing and the code SEEMS to work fine, even though there are these warnings. Is there a way to remove these warnings at all? Or do they not matter?
I'm using xCode version 3.2.6
Thanks,
Jack
use
NSError* error;
NSString* connected = [NSString stringWithContentsOfURL:TheUrl encoding:NSASCIIStringEncoding error:&error];
You should use the method -
+(id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error
for example -
NSString* text = [NSString stringWithContentsOfURL:TheUrl encoding:NSASCIIStringEncoding error:&error];
Hope it will help you.
Try doing it this way:
-(IBAction)guestList:(id) sender{
NSURL *requestUrl = [NSURL URLWithString:#"http://photostiubhart.comoj.com/testconnection.php"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:requestUrl];
NSData *loadTest = [NSData dataWithContentsOfURL:requestUrl];
if (loadTest == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Network Connection!" message:#"Cannot establish internet connection." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
} else {
CHARLIEAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate displayView:6];
}
}
I use the MFMessageComposeViewController for sending in App sms. in iPhone 4.0, if there is no SIM card, the app exits. it just gives a pop up message "no sim card installed".
The delegate callback MessageComposeResultSent. But application exits. Is there any way to prevent it from exiting? or how to check if there is any SIM card in the phone?
Code snippets below:
/* Open the system sms service, copying the sms text in system clipboard. */
- (void) sendSMSAsURLRequest {
NSString *phoneNumber = friend.phoneMobile;
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
NSString *textUTIType = (NSString *)kUTTypeUTF8PlainText; // add MobileCoreServices.framework for this type.
[pasteBoard setValue:[self buildSMSText] forPasteboardType:textUTIType];
NSString *urlString = [NSString stringWithFormat:#"sms:%#", phoneNumber];
NSURL *url = [[NSURL alloc] initWithString: urlString];
[[UIApplication sharedApplication] openURL: url];
[url release];
}
-(void) sendInAppSMS {
MFMessageComposeViewController *controller = [[[MFMessageComposeViewController alloc] init] autorelease];
controller.delegate = self;
if([MFMessageComposeViewController canSendText])
{
NSString *smsText = [self buildSMSText];
controller.body = smsText;
controller.recipients = [NSArray arrayWithObjects:friend.phoneMobile, nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
switch (result) {
case MessageComposeResultCancelled:
NSLog(#"Cancelled");
break;
case MessageComposeResultFailed:{
NSString *alertString = NSLocalizedString(#"Unknown Error. Failed to send message", #"");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:alertString delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
break;
}
case MessageComposeResultSent:
NSLog(#"SMS sent");
break;
default:
break;
}
[self dismissModalViewControllerAnimated:YES];
}
To Detect Sim Card is installed or not use following Code :
#import CoreTelephony;
CTTelephonyNetworkInfo *networkInfo = [CTTelephonyNetworkInfo new];
CTCarrier *carrier = [networkInfo subscriberCellularProvider];
if (!carrier.isoCountryCode) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"No SIM Card Installed" message:nil delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
else{
//Paste Your code here
}
The work around I am using now is, a flag in the app delegate,
- (void)applicationWillResignActive:(UIApplication *)aNotification {
if (shouldExitApp) {
exit(0);
}
}
In the SMS sending view controller,
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
((LuupAppDelegate *)[[UIApplication sharedApplication] delegate]).shouldExitApp = NO;
And set the flag again, when in the SMS sending view controller,
- (void) viewDidAppear:(BOOL)animated {
((LuupAppDelegate *)[[UIApplication sharedApplication] delegate]).shouldExitApp = YES;
[super viewDidAppear:animated];
}
I am using a private MBProgressHUD
Now I am using the indicator view on my add button in which I am calling my addrecord service .
UIWindow *window = [UIApplication sharedApplication].keyWindow;
HUD = [[MBProgressHUD alloc] initWithWindow:window];
// Add HUD to screen
[window addSubview:HUD];
// Regisete for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;
// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:#selector(addingToFavorites) onTarget:self withObject:nil animated:YES];
the adding to favorites method :
NSURL *url = [NSURL URLWithString:urlstring];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
[request setHTTPMethod:#"GET"];
//[request setTimeoutInterval:10];
//NSURLResponse *response = nil;
// NSError *error = nil;
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSData *data1= [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
if(data1 == nil)
{
doneFlag = NO;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Alert"
message:#"The network is not available.\n Please check the Internet connection."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
doneFlag = YES;
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Confirmation"
message:#"Added To favorites"
delegate:nil
cancelButtonTitle:#"OKAY"
otherButtonTitles:nil];
[alert show];
alert = nil;
[alert release];
}
[request release];
This is all running fine except the instruments gives leak of the uialertview may be it is conflicting with the mbprogreshud.
So I thought to remove the alert from the calling method and put it in the caller the method like this:
the caller method now :
UIWindow *window = [UIApplication sharedApplication].keyWindow;
HUD = [[MBProgressHUD alloc] initWithWindow:window];
// Add HUD to screen
[window addSubview:HUD];
// Regisete for HUD callbacks so we can remove it from the window at the right time
HUD.delegate = self;
// Show the HUD while the provided method executes in a new thread
[HUD showWhileExecuting:#selector(addingToFavorites) onTarget:self withObject:nil animated:YES];
//it should wait for the above line to be executing ******* then to exexute the be //below condition but how ?
if (doneFlag == NO) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Alert"
message:#"The network is not available.\n Please check the Internet connection."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
} else {
[favoritesButton setTitle:#"Remove" forState:UIControlStateNormal];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Confirmation"
message:#"Added To favorites"
delegate:nil
cancelButtonTitle:#"OKAY"
otherButtonTitles:nil];
[alert show];
alert = nil;
[alert release];
}
the adding to favorites method :
NSURL *url = [NSURL URLWithString:urlstring];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0];
[request setHTTPMethod:#"GET"];
//[request setTimeoutInterval:10];
//NSURLResponse *response = nil;
// NSError *error = nil;
[[NSURLCache sharedURLCache] setMemoryCapacity:0];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
NSData *data1= [NSURLConnection sendSynchronousRequest:request
returningResponse:nil error:nil];
if(data1 == nil)
{
doneFlag = NO;
}
else
{
doneFlag = YES;
}
[request release];
In the launching of the progresshud thread is detaching something like this :
[NSThread detachNewThreadSelector:#selector(launchExecution) toTarget:self withObject:nil]
Now My question is that If I follow the first scenario . How can I assure the the alertview leak will not come
Or If I am following the second scenario How can I assure the if condition will be executed after completing this line executed :
[HUD showWhileExecuting:#selector(addingToFavorites) onTarget:self withObject:nil animated:YES];
Regarding the first scenario, it is in general a bad idea to do UI updates from threads other than the applications main thread. UIKit is NOT thread safe and doing threaded UI updates can cause all sorts of strange things to happen. Now, I'm not sure if this is the cause for the leak but I would avoid showing an UIAlertView in addingToFavorites. Use performSelectorOnMainThread or the second scenario described below.
Regarding the second scenario, move everything below the showWhileExecuting call to the hudWasHidden delegate method. At this point you can be sure that your code was fully executed and the doneFlag was set.
To use performSelectorOnMainThread, define a new method, put your code in it and than call performSelectorOnMainThread.
I.e.,
- (void)showAlert {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Alert" message:#"The network is not available.\n Please check the Internet connection." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
Call with,
[self performSelectorOnMainThread:#selector(showAlert) withObject:nil waitUntilDone:NO];
I would go with the second scenario though.
Other answers notwithstanding, you were creating the UIAlertView leak with this sequence:
[alert show];
alert = nil;
[alert release];
The last two lines should be swapped:
[alert show];
[alert release];
alert = nil;