how to do JSON parsing in Registration Page - iphone

I am facing trouble in JSON parsing. When I have completed the code and run this, then the problem is occurred:
no known class method for selector 'show alert:with message'.
Following is my code:
if (!isValid) {
[AppDelegate showAlert:#"Alert!" withMessage:strMessage];
}
return isValid;
}
-(void)registerUser{
[Loading startLoading:YES];
NSString *urlString = [NSString stringWithFormat:#"URL=//%#",Access_Token];
//username, password, name, email, country
NSString *parameter = [NSString stringWithFormat:#"firstname=%#&lastname=%#email=%#&username=%#&password=%#&confirmpassword=#",fnameField.text,lnameField.text,eamilField.text,unameField.text,passField.text,cpassField];
NSConnection *conn = [[NSConnection alloc] initWithDelegate:self];
[conn sendRequest:urlString withParaMeter:parameter withMethod:#"POST" withTag:1];
[conn startAsynchronousRequest];
}
-(void)NSConnection:(NSConnection*)conn didFailWithError:(NSError*)error withTag:(int)tag{
NSLog(#"error is:- %#",[error description]);
}
-(void)NSConnection:(NSConnection*)request didSuccessWithItems:(NSDictionary*)dicData withData:(NSData*)data withTag:(int)tag{
NSLog(#"all data is:- %#",dicData);
int returnCode = [[dicData valueForKeyPath:#"process.returncode"] intValue];
NSString *strMessage = #"";
returnCode = 0;
switch (returnCode) {
case 0:
break;
case 1:
strMessage = #"User not logged in/process Id not available.";
break;
case 2:
strMessage = #"Invalid parameters passed.";
break;
case 3:
strMessage = #"Access token doesn't exist.";
break;
default:
strMessage = #"User name allready exist.";
break;
}
[AppDelegate showAlert:#"Alert!" withMessage:strMessage];
[Loading stopLoading];
[self.navigationController popViewControllerAnimated:YES];
Please help me out.

Thats because you are trying to call that method on object AppDelegate
However the AppDelegate class does not implement it.
Hence the error. Make sure it is implemented.

Two chances for this issue
You are not declared the showAlert: withMessage: method signature in your #interface
You may be declared it as a instance method, you are calling it using the Class name.
Fixes:
Declare the method signature in your #interface
Either define your method as Class method else call it using your app delegate's instance

Related

How to get user details using twitter api v1.1 (Twitter error 215)

I have used the twitter api provided by twitter,to get the details but
not able to execute it, even tried to pass the authentication data
like consumer secret key, consumer key, token but the result is same.
I am able to login and receiving twitter authentication token but not able to get user details.
Below code is used by me (I am using MGtwitter engine) :
NSMutableURLRequest *request =[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"https://api.twitter.com/1.1/users/show.json?screen_name=%#",username]]];
NSData *returnData = [ NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
NSString *returnString = [[NSString alloc]initWithData:returnData encoding:NSUTF8StringEncoding];
NSError *err = nil;
twitterLogin = [NSJSONSerialization JSONObjectWithData:[returnString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&err];
Error is shown as below:
errors = (
{
code = 215;
message = "Bad Authentication data";
} );
First, you need to Authenticate your request (Get permission).
second, see follow these steps:
1.Download FHSTwitterEngine Twitter Library.
2.Add the folder FHSTwitterEngine" to your project and #import "FHSTwitterEngine.h".
3.add SystemConfiguration.framework to your project.
Usage : 1.in the [ViewDidLoad] add the following code.
UIButton *logIn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
logIn.frame = CGRectMake(100, 100, 100, 100);
[logIn setTitle:#"Login" forState:UIControlStateNormal];
[logIn addTarget:self action:#selector(showLoginWindow:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:logIn];
[[FHSTwitterEngine sharedEngine]permanentlySetConsumerKey:#"<consumer_key>" andSecret:#"<consumer_secret>"];
[[FHSTwitterEngine sharedEngine]setDelegate:self];
and don't forget to import the delegate FHSTwitterEngineAccessTokenDelegate.
you need to get the permission for your request, with the following method which will present Login window:
- (void)showLoginWindow:(id)sender {
[[FHSTwitterEngine sharedEngine]showOAuthLoginControllerFromViewController:self withCompletion:^(BOOL success) {
NSLog(success?#"L0L success":#"O noes!!! Loggen faylur!!!");
}];
}
when the Login window is presented, enter your Twitter Username and Password to authenticate your request.
add the following methods to your code:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[FHSTwitterEngine sharedEngine]loadAccessToken];
NSString *username = [[FHSTwitterEngine sharedEngine]loggedInUsername];// self.engine.loggedInUsername;
if (username.length > 0) {
lbl.text = [NSString stringWithFormat:#"Logged in as %#",username];
[self listResults];
} else {
lbl.text = #"You are not logged in.";
}
}
- (void)storeAccessToken:(NSString *)accessToken {
[[NSUserDefaults standardUserDefaults]setObject:accessToken forKey:#"SavedAccessHTTPBody"];
}
- (NSString *)loadAccessToken {
return [[NSUserDefaults standardUserDefaults]objectForKey:#"SavedAccessHTTPBody"];
}
4.Now you are ready to get your request, with the following method(in this method I created a Twitter search for some Hashtag, to get the screen_name for example):
- (void)listResults {
dispatch_async(GCDBackgroundThread, ^{
#autoreleasepool {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// the following line contains a FHSTwitterEngine method wich do the search.
dict = [[FHSTwitterEngine sharedEngine]searchTweetsWithQuery:#"#iOS" count:100 resultType:FHSTwitterEngineResultTypeRecent unil:nil sinceID:nil maxID:nil];
// NSLog(#"%#",dict);
NSArray *results = [dict objectForKey:#"statuses"];
// NSLog(#"array text = %#",results);
for (NSDictionary *item in results) {
NSLog(#"text == %#",[item objectForKey:#"text"]);
NSLog(#"name == %#",[[item objectForKey:#"user"]objectForKey:#"name"]);
NSLog(#"screen name == %#",[[item objectForKey:#"user"]objectForKey:#"screen_name"]);
NSLog(#"pic == %#",[[item objectForKey:#"user"]objectForKey:#"profile_image_url_https"]);
}
dispatch_sync(GCDMainThread, ^{
#autoreleasepool {
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Complete!" message:#"Your list of followers has been fetched" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[av show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
});
}
});
}
That's all.
I just got the screen_name from a search Query, you can get a timeline for a user using the following methods:
// statuses/user_timeline
- (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count;
- (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID;
instead of the search method above.
Note: see the FHSTwitterEngine.h to know what method you need to use.
Note: to get the <consumer_key> and the <consumer_secret> you need to to visit this link
to register your app in Twitter.
Got the solution after MKAlatrash revert, to get the user profile follow certain steps in the code as under :
[[FHSTwitterEngine sharedEngine]getProfileImageForUsername:username andSize:FHSTwitterEngineImageSizeNormal];
jump to definition of this function and replace the if ... else if part
if ([userShowReturn isKindOfClass:[NSError class]]) {
return [NSError errorWithDomain:[(NSError *)userShowReturn domain] code:[(NSError *)userShowReturn code] userInfo:[NSDictionary dictionaryWithObject:request forKey:#"request"]];
NSLog(#"user show return %#",userShowReturn);
} else if ([userShowReturn isKindOfClass:[NSDictionary class]]) {
return userShowReturn;
NSString *url = [userShowReturn objectForKey:#"profile_image_url"]; // normal
if (size == 0) { // mini
url = [url stringByReplacingOccurrencesOfString:#"_normal" withString:#"_mini"];
} else if (size == 2) { // bigger
url = [url stringByReplacingOccurrencesOfString:#"_normal" withString:#"_bigger"];
} else if (size == 3) { // original
url = [url stringByReplacingOccurrencesOfString:#"_normal" withString:#""];
}
id ret = [self sendRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
if ([ret isKindOfClass:[NSData class]]) {
return [UIImage imageWithData:(NSData *)ret];
}
return ret;
}
That really was helpful thanks

NSDictionary isn't responding to objectForKey and valueForKey

I have the following
// this code is inside cellForRowAtIndexPath for a TableViewController
id answer = [self.answers objectAtIndex:indexPath.row];
if ([answer respondsToSelector:#selector(objectForKey)]) {
cell.textLabel.text = [answer valueForKey:#"answer_id"];
} else {
// I'm ending up here, instead of the cell.textLabel being set
[NSException raise:#"Answer is of invalid class" format:#"It should be able to respond to valueForKey, class: %#", [answer class]];
}
where self.answers is set to
// the question that gets passed here is a parsed single object
// from the `/questions` path
- (NSArray *)answersForQuestion:(NSDictionary *)question {
NSString *contents = [self loadContentsForPath:[question valueForKey:#"question_answers_url"]];
valueForKey:#"question_answers_url"]];
NSDictionary *data = [contents JSONValue];
NSArray *answers = [data valueForKey:#"answers"];
return answers;
}
- (NSString *)loadContentsForPath:(NSString *)path {
NSString *wholeURL = [#"http://api.stackoverflow.com/1.1" stringByAppendingString:path];
return [NSString stringWithContentsOfURL:[NSURL URLWithString:wholeURL] encoding:NSUTF8StringEncoding error:nil];
}
I'm doing exactly the same thing for loading questions which works just fine, but it seems
to fail on answers when I try to do [answers valueForKey:#"answer_id"].
I don't think this is a problem with the JSON parser, because it works fine for the /questions data.
When the debugger stops on the exception and when I try to right click -> Print Description on answers, I get
Printing description of answer:
<CFBasicHash 0x6ec1ec0 [0x1474b38]>{type = mutable dict, count = 13,
entries =>
1 : <CFString 0x6ec57d0 [0x1474b38]>{contents = "down_vote_count"} = <CFNumber 0x6e1dc00 [0x1474b38]>{value = +0, type = kCFNumberSInt32Type}
2 : <CFString 0x6ec4ee0 [0x1474b38]>{contents = "last_activity_date"} = <CFNumber 0x6ec5780 [0x1474b38]>{value = +1326379080, type = kCFNumberSInt64Type}
3 : <CFString 0x6ec44b0 [0x1474b38]>{contents = "community_owned"} = <CFBoolean 0x1474f68 [0x1474b38]>{value = false}
...
which to me seems like a regular hash. I tried both objectForKey and valueForKey and neither of them work, i.e.
exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x6b2a330'
when I do just
cell.textLabel.text = [answer objectForKey:#"answer_id"];
The : is a part of the method name, so you need to do:
if ([answer respondsToSelector:#selector(objectForKey:)]) {
And then use objectForKey:, not valueForKey:. The first is to access objects in the dictionary, the later is for the so-called Key-Value Coding. So it's:
id answer = [self.answers objectAtIndex:indexPath.row];
if ([answer respondsToSelector:#selector(objectForKey:)]) {
cell.textLabel.text = [answer objectForKey:#"answer_id"];
} else {
[NSException raise:#"Answer is of invalid class" format:#"It should be able to respond to objectForKey:, class: %#", [answer class]];
}
Last but not least, it looks like the object you get out of the answer dictionary is a NSNumber, not an NSString. So you might want to change the setting of the text to:
cell.textLabel.text = [[answer objectForKey:#"answer_id"] description];
Actually, stack overflow's api sends back a number. That is to say that the value stored for the key answer_id is an NSNumber in the dictionary and not NSString and you should treat it accordingly.
you can use: --
NSDictionary *answer = [self.answers objectAtIndex:indexPath.row];
if ([answer respondsToSelector:#selector(objectForKey)]) {
cell.textLabel.text = [answer valueForKey:#"answer_id"];
} else {
[NSException raise:#"Answer is of invalid class" format:#"It should be able to respond to valueForKey, class: %#", [answer class]];
}
OR
id answer = [self.answers objectAtIndex:indexPath.row];
if (answer isKindOfClass:[NSDictionary class])
{
cell.textLabel.text = [answer valueForKey:#"answer_id"];
} else {
// I'm ending up here, instead of the cell.textLabel being set
[NSException raise:#"Answer is of invalid class" format:#"It should be able to respond to valueForKey, class: %#", [answer class]];
}

help with singleton's

I am trying to create a singleton User class in my app, here's the code:
#import "User.h"
#import "Login.h"
#import "SFHFKeychainUtils.h"
// Constants
static NSString* const kDBUserCurrentUserIDDefaultsKey = #"kDBUserCurrentUserIDDefaultsKey";
// Current User singleton
static User* currentUser = nil;
#implementation User
#synthesize username = _username;
#synthesize password = _password;
#synthesize delegate = _delegate;
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
+ (NSString*)primaryKeyProperty {
return #"username";
}
+ (User*)currentUser {
if (nil == currentUser) {
id username = [[NSUserDefaults standardUserDefaults] objectForKey:#"kApplicationUserNameKey"];
if (!username) {
currentUser = [self new];
} else{
NSLog(#"CURRENT USER");
return self;
}
[currentUser retain];
}
return currentUser;
}
+ (void)setCurrentUser:(User*)user {
[user retain];
[currentUser release];
currentUser = user;
}
/**
* Implementation of a RESTful login pattern. We construct an object loader addressed to
* the /login resource path and POST the credentials. The target of the object loader is
* set so that the login response gets mapped back into this object, populating the
* properties according to the mappings declared in elementToPropertyMappings.
*/
- (void)loginWithUsername:(NSString*)username andPassword:(NSString*)password delegate:(NSObject<DBUserAuthenticationDelegate>*)delegate {
_delegate = delegate;
//[RKObjectManager sharedManager].client.username = username;
//[RKObjectManager sharedManager].client.password = password;
self.username = username;
self.password = password;
RKObjectMapping * userMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForKeyPath:#"LoginViewController"];
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:#"/account/verify.json" objectMapping:userMapping delegate:self];
}
/**
* Implementation of a RESTful logout pattern. We POST an object loader to
* the /logout resource path. This destroys the remote session
*/
- (void)logout/*:(NSObject<DBUserAuthenticationDelegate>*)delegate */{
NSError * error = nil;
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:#"kApplicationUserNameKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
[SFHFKeychainUtils deleteItemForUsername:self.username andServiceName:#"convore" error:&error];
NSLog(#"LOGGING OUT");
if ([self.delegate respondsToSelector:#selector(userDidLogout:)]) {
[self.delegate userDidLogout:self];
}
[[NSNotificationCenter defaultCenter] postNotificationName:#"DBUserDidLogoutNotification" object:nil];
}
- (void)loginWasSuccessful {
// Upon login, we become the current user
[User setCurrentUser:self];
NSError * error = nil;
// Persist the username for recovery later
[[NSUserDefaults standardUserDefaults] setValue:self.username forKey:#"kApplicationUserNameKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
[SFHFKeychainUtils storeUsername:self.username andPassword:self.password forServiceName:#"convore" updateExisting:TRUE error:&error];
// Inform the delegate
if ([self.delegate respondsToSelector:#selector(userDidLogin:)]) {
[self.delegate userDidLogin:self];
}
[[NSNotificationCenter defaultCenter] postNotificationName:#"DBUserDidLoginNotification" object:self];
}
- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response
{
NSLog(#"Loaded payload: %#", [response bodyAsString]);
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObject:(id)object
{
if ([objectLoader wasSentToResourcePath:#"/account/verify.json"]) {
Login * login = (Login *) object;
if ([login.username length] > 0)
[self loginWasSuccessful];
}
}
- (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError*)error {
if ([objectLoader wasSentToResourcePath:#"/account/verify.json"]) {
NSLog(#"Encountered an error: %#", error);
// Login failed
if ([self.delegate respondsToSelector:#selector(user:didFailLoginWithError:)]) {
[self.delegate user:self didFailLoginWithError:error];
}
}
}
- (BOOL)isLoggedIn {
return self.username != nil;
//return self.singleAccessToken != nil;
}
- (void)dealloc {
_delegate = nil;
[_password release];
[_passwordConfirmation release];
[super dealloc];
}
#end
The issue is that whenever I tried to access currentUser it always breaks down. I first called the loginWithUsernameandPassword and then tried calling the currentUser, but when I call the currentUser on logout, it gives me an error:
calling this:
if ([[User currentUser] isLoggedIn])
gives me:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[User isLoggedIn]: unrecognized selector sent to class 0x1a671c'
seems that currentUser is nil, why is this?
Quick Singleton 101 (I wish I had this when I started, lol. Everyone just pointed me to the docs which didn't help much). The name of the singleton is going to be "Singleton"
//Singleton.h
#import <Foundation/Foundation.h>
#interface SingletonManager : NSObject
{
NSDictionary* randomDictionary; //just using a dictionary for demonstrative purposes. You can make this a string or whatever you want.
}
+ (Singleton*)sharedSingleton;
#property (nonatomic, retain) NSDictionary *randomDictionary;
#end
And now the .m
//Singleton.m
#import "Singleton.h"
static Singleton *sharedSingleton = nil;
#implementation Singleton
#synthesize randomDictionary;
#pragma mark Singleton Method
+ (Singleton*)sharedSingleton
{
#synchronized(self)
{
if(sharedSingleton == nil)
{
sharedSingleton = [[super allocWithZone:NULL] init];
}
}
return sharedSingleton;
}
#end
And to set/get, first import the singleton in whatever class you need: #import "Singleton.h", then grab the singleton with Singleton *singletonManager = [Singleton sharedSingleton]; and then you can do whatever you need to as necessary. i.e. to get the description of the NSDictionary you would call [[singletonManager randomDictionary] description];
Now this is using ARC, so if you are not you'd just have to make sure you manage your memory correctly. Enjoy.
You need to get the singleton object before you can call a method on it.
if ( [[User currentUser] isLoggedIn] ) {
// Magic happens here
}
You aren't coding your singleton properly.
+ (User *) currentUser {
#synchronized (self) {
if (currentUser == nil) {
currentUser = [[self alloc] init];
}
return currentUser;
}
}
The answer is really a combo of the two answers from XCodeDev and Matthieu Cormier. You need to "protect" your init the way the code sample says so new versions of the object are not created. Otherwise, its not a real singleton. More info on Singleton pattern.
Also, just because its a singleton doesn't mean you can access it with just class methods after you initialize it. You still need to get the instance you initialized, otherwise you cannot do operations that require certain values only in the instance.

Problem with UITextView

I have a strange problem with trying to pass a string from one viewcontroller to another view controller if the string originates from a UITextview instead of UITextfield. Both UITextview.text and UITextfield.text are of type NSString.
The following code takes either Textfield.text or Textview.text depending on the fieldType and puts it into a string called aString.
NSString *aString = [[NSString alloc] init];
if (fieldType == 3) {
aString = textView.text;
} else {
aString = textField.text;
}
When I examine aString on either cases, I can see that it has successfully assigned the text into aString.
I then pass the string to the other view controller using this code.
[delegate updateSite:aString :editedFieldKey :fieldType];
[self.navigationController popViewControllerAnimated:YES];
This works fine if aString originated from textfield.text but nothing happens except the view controller is popped if aString was from textview.text
This is the code that takes aString and does stuff with it, however it doesn't even execute the first line of code "NSLog(#"Returned object: %#, field type:%#", aString,editedFieldKey);" if aString was from textview.text Any help will be appreciated.
-(void)updateSite:(NSString *)aString :(NSString *)editedFieldKey :(int)fieldType {
NSLog(#"Returned object: %#, field type:%#", aString,editedFieldKey);
switch (fieldType) {
case 0: //string
[aDiveSite setValue:aString forKey:editedFieldKey] ;
NSLog(#"String set %#",[aDiveSite valueForKey:editedFieldKey] );
break;
case 1: //int
[aDiveSite setValue:[NSNumber numberWithInt:aString.intValue] forKey:editedFieldKey];
NSLog(#"Integer set");
break;
case 2: //float
NSLog(#"Saving floating value");
[aDiveSite setValue:[NSNumber numberWithFloat:aString.floatValue] forKey:editedFieldKey];
NSLog(#"Float set");
break;
case 3: //Text view
[aDiveSite setValue:aString forKey:editedFieldKey];
NSLog(#"Textview text saved");
default:
break;
}
[self.tableView reloadData];
}
Uhm, sounds strange but I think your syntax is wrong:
[delegate updateSite:aString :editedFieldKey :fieldType];
should be
[delegate updateSite:aString editedFieldKey:fieldType];
and the method name:
-(void)updateSite:(NSString *)aString :(NSString *)editedFieldKey :(int)fieldType;
should be:
-(void)updateSite:(NSString *)aString (NSString *)editedFieldKey :(int)fieldType;
Can you try it?
This is what I've changed it to for editing textfield
[self.delegate editingViewController:self didTypeText:aString forKey:editedFieldKey forFieldType:fieldType];
and for textview I've created a separate controller that deals with it (and it works)
[self.delegate editTextViewController:self didEditText:textView.text forKey:editedFieldKey];

MFMailComposeViewController problem

This problem is probably not specific to MFMailComposeViewController, but that is where I am having the problem...
I am building the NSString
"myEmailString" for the messageBody of
the MFMailComposeViewController and
storing it in an iVar before
displaying the
MFMailComposeViewController as a
modal view controller.
I pass the string into the MFMailComposeViewController, then present it as a modal view controller.
When the modal view controller is dismissed, my iVar becomes invalid,
and the app crashes when I release the emailString iVar in dealloc
Code below, what am I doing wrong?
-(void)buildEmailMessage {
int mySection;
int myRow;
NSString *buildString = [NSString stringWithFormat:#"<b><p>Ten Essentials Check List</b><br />%#</p>", [myList valueForKey:#"listName"]];
for (mySection = 0; mySection < [[fetchedResultsController sections] count]; mySection ++) {
NSString *sectionName = [NSString stringWithFormat:#"<p><b>%# Group</b></p><ul>", [[[fetchedResultsController sections] objectAtIndex:mySection] name]];
buildString = [buildString stringByAppendingString:sectionName];
id <NSFetchedResultsSectionInfo> sectionInfo = [[fetchedResultsController sections] objectAtIndex:mySection];
for (myRow = 0; myRow < [sectionInfo numberOfObjects]; myRow ++) {
// Get the managedObject
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:myRow inSection:mySection];
NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:indexPath];
//Get the related Item object
Item *item = [managedObject valueForKey:#"item"];
NSString *itemName = [NSString stringWithFormat:#"<li>%#</li>", item.itemName];
buildString = [buildString stringByAppendingString:itemName];
}
buildString = [buildString stringByAppendingString:#"</ul>"];
}
myEmailString = [NSString stringWithString:buildString];
NSLog(#"email string = :\n%#", myEmailString);
[self showPicker];
}
#pragma mark -
#pragma mark Send Mail
-(void)showPicker {
// This code can run on devices running iPhone OS 2.0 or later
// The MFMailComposeViewController class is only available in iPhone OS 3.0 or later.
// So, we must verify the existence of the above class and provide a workaround for devices running
// earlier versions of the iPhone OS.
// We display an email composition interface if MFMailComposeViewController exists and the device can send emails.
// We launch the Mail application on the device, otherwise.
NSLog(#"Checking OS for MFMailComposeViewController");
Class mailClass = (NSClassFromString(#"MFMailComposeViewController"));
if (mailClass != nil)
{
// We must always check whether the current device is configured for sending emails
if ([mailClass canSendMail])
{
[self displayComposerSheet];
}
else
{
[self launchMailAppOnDevice];
}
}
else
{
[self launchMailAppOnDevice];
}
}
// Displays an email composition interface inside the application. Populates all the Mail fields.
-(void)displayComposerSheet {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
picker.navigationBar.barStyle = UIBarStyleBlack;
[picker setSubject:#"Here is your gear check list!"];
// Attach an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:#"Checkmark_icon" ofType:#"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:#"image/png" fileName:#"Checkmark_icon"];
// Fill out the email body text
//***** NOTE: This is where I pass the value from my iVar *****
// into the MFMailComposeViewController
//
NSString *emailBody = [NSString stringWithString:myEmailString];
[picker setMessageBody:emailBody isHTML:YES];
NSLog (#"DIsplaying Composer Sheet");
[self presentModalViewController:picker animated:YES];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
//message.hidden = NO;
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
NSLog (#"Result: canceled");
break;
case MFMailComposeResultSaved:
NSLog (#"Result: saved");
break;
case MFMailComposeResultSent:
NSLog (#"Result: sent");
break;
case MFMailComposeResultFailed:
NSLog (#"Result: failed");
break;
default:
NSLog (#"Result: not sent");
break;
}
[self dismissModalViewControllerAnimated:YES];
// ***** NOTE: Line below was added to fix the invalid iVar problem *****
myEmailString = #"";
}
#pragma mark -
#pragma mark Workaround
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice {
NSString *recipients = #"mailto:first#example.com?cc=second#example.com,third#example.com&subject=Here is your gear check list!";
NSString *body = myEmailString;
NSString *email = [NSString stringWithFormat:#"%#%#", recipients, body];
email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
- (void)dealloc {
[managedObjectContext release];
[fetchedResultsController release];
[tableview release];
[myList release];
[myEmailString release];
[super dealloc];
}
how is your ivar declared? is it declared as a property? in any case, it is not automatically retained for you.
Either you need to do
myEmailString = [[NSString stringWithString:buildString] retain];
or
self.myEmailString = [NSString stringWithString:buildString];
if you have myEmailString declared as
#property (nonatomic, retain) NSString *myEmailString
Think about it: if all ivars were automatically retained for you, then how would you have a variable that you didn't want to retain? That's why it doesn't work that way.
when you are creating the myEmail string in buildEmailMessage you are never retaining the string. Thus after leaving the function it is autoreleased. Your retain count then when dealloc is called will be 0, which will cause the crash. If you want to keep the variable you will need to have the line as follows
myEmailString = [[NSString stringWithString:buildString] retain];
then you can call [myEmailString release] safely
stringWithString: creates a new string and autoreleases it before returning it to you. Unless you retain the returned string, you don't need to release it in your dealloc method.
You should be retaining your string before storing it in your iVar:
myEmailString = [[NSString stringWithString:buildString] retain];
It becomes invalid without this due to it being autoreleased later during the execution of your program. This will also ensure it's still allocated when your destructor is called, preventing release crashing.