Creating an iPhone contact in Objective-C - iphone

I am trying to create a contact in the internal address book. My code is as follows:
in the CreateNewAccountPage.h file:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
....
#interface CreateNewAccountPage : UIViewController <UIScrollViewDelegate,
UITextViewDelegate, MainMenuDelegate, ABNewPersonViewControllerDelegate> {
....
}
- (IBAction)goToContactCreation;
...
#end
In the CreateNewAccountPage.m file:
#import "CreateNewAccountPage.h"
#implementation CreateNewAccountPage
-(void)showNewPersonViewController
{
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
picker.newPersonViewDelegate = self;
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
[self presentModalViewController:navigation animated:YES];
[picker release];
[navigation release];
}
-(IBAction)goToContactCreation {
[self showNewPersonViewController];
}
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
[self dismissModalViewControllerAnimated:YES];
}
...
#end
The error that I am getting is:
"_OBJC_CLASS_$_ABNewPersonViewController", referenced from:
objc-class-ref-to-ABNewPersonViewController in CreateNewAccountPage.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
If I comment out the lines:
ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
picker.newPersonViewDelegate = self;
...
[picker release];
I no longer receive the error. I am just trying to get a contact to be created. It doesn't have to do with accessing the contact list. I just want a button to create the contact.
Thanks so much!

Add AddressBook.framework, AddressBookUI.framework frameworks to your project.

Related

MWPhotoBrowser with button and error handeling

I'm trying to make a app that uses a photobrowser
Decided to go with the MWPhotoBrowser.
This is the code, but i cant seem to make it work:
ViewController.h
#import <UIKit/UIKit.h>
#import "MWPhotoBrowser.h"
#interface ViewController : UIViewController<MWPhotoBrowserDelegate> {
NSArray *_photos;
UISegmentedControl *_segmentedControl;
}
#property (nonatomic, retain) NSArray *photos;
- (IBAction)billede:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize photos = _photos;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
UIBarButtonItem *item = [[[UIBarButtonItem alloc] initWithCustomView:_segmentedControl] autorelease];
self.navigationItem.rightBarButtonItem = item;
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil action:nil] autorelease];
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)billede:(id)sender {
//show your photo whit url
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
{
photo = [MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"photo2l" ofType:#"jpg"]];
photo.caption = #"The London Eye is a giant Ferris wheel situated on the banks of the River Thames, in London, England.";
[photos addObject:photo];
}
self.photos = photos;
// Create browser
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
browser.displayActionButton = YES;
//browser.wantsFullScreenLayout = NO;
//[browser setInitialPageIndex:2];
// Show
if (_segmentedControl.selectedSegmentIndex == 0) {
// Push
[self.navigationController pushViewController:browser animated:YES];
} else {
// Modal
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:browser];
nc.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:nc animated:YES];
[nc release];
}
}
#pragma mark - MWPhotoBrowserDelegate
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return _photos.count;
}
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex: (NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
#end
I've tried both with ARC and wothout ARC
Without ARC i get 3 errors:
Undefined symbols for architecture i386:
"_OBJC_CLASS_$_MWPhoto", referenced from:
objc-class-ref in ViewController.o
"_OBJC_CLASS_$_MWPhotoBrowser", referenced from:
objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
With ARC i get 2 errors
Problem 1
http://i.imgur.com/pT7qW.png
Problem 2
http://i.imgur.com/E0X1m.png
I've not done it here, but i would like it alle to be wrapped in a button so i can click that and show a image inside the MWPhotoBrowser
Edit
I have upgraded my code, removed ARC from my files and i have now set the target right.
It will compile now, but everytime i triy to click the button: "billede" i get:
2012-11-26 23:32:10.955 MWPhotoBrowserTest[10405:c07] -[ViewController galleri:]: unrecognized selector sent to instance 0x947fb20
2012-11-26 23:32:10.957 MWPhotoBrowserTest[10405:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController galleri:]: unrecognized selector sent to instance 0x947fb20'
*** First throw call stack:
(0x1d34012 0x14e9e7e 0x1dbf4bd 0x1d23bbc 0x1d2394e 0x14fd705 0x434920 0x4348b8 0x4f5671 0x4f5bcf 0x4f4d38 0x46433f 0x464552 0x4423aa 0x433cf8 0x1f6ddf9 0x1f6dad0 0x1ca9bf5 0x1ca9962 0x1cdabb6 0x1cd9f44 0x1cd9e1b 0x1f6c7e3 0x1f6c668 0x43165c 0x1e2d 0x1d55)
libc++abi.dylib: terminate called throwing an exception
(lldb)
Attachment 1:
You can't add objects to NSArray, use NSMutableArray instead.
Attachment 2:
UIView has no methods "reloadData", but for exmple UITableView does.
Actually resolved this.
I've struggled for 4 days and finally got it.
I read through https://github.com/mwaterfall/MWPhotoBrowser#method-1-static-library and understood it, finally but i had a problem with User header Search Path.
I finally got it, importet MWPhotoBrowser folder into my MWPhotoBrowser"project" folder and then sat User Header Search Path to ".." instead of "../" since "../" gave me "../../"
Hope this helps anyone who might stand in the same problem as me!
Btw, thanks for the help guys! :-)

signal SIGABRT when testing on device, it works on simulator. Easy code

I'm not experienced in programming.
I was trying to do an easy iOS app (just to try some tutorials) that works as a "sendmail"
Easy interface with just 1 button that when pressed, it opens the MFMailComposeViewController window.
That's the code.
ViewController.h
#import <UIKit/UIKit.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <MessageUI/MessageUI.h>
#interface ViewController : UIViewController <MFMailComposeViewControllerDelegate>
-(IBAction)showPicker:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)showPicker:(id)sender
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
NSArray *toRecipients = [NSArray arrayWithObject:#"example#example.com"];
[picker setToRecipients:toRecipients];
[picker setSubject:#"TEST SUBJECT"];
[self presentViewController:picker animated:YES completion:nil];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
On the iPhone and iPad simulator it works like a charm.
But when testing it on the device it crashes giving signal SIGABRT and showing as evidence that part of main.m:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
with a "Thread 1 - Signal SIGABRT" near.
Any tip?
Thanks to everyone in advance.
Two things to try:
Clean the files <SHIFT+CMD+K>
Uninstall the file from your device and try with the cleaned one
Right at the beginning of showPicker: add this :
if [MFMailComposeViewController canSendMail]
{
// Your code
}
else
NSLog(#"No mail account configured on device or not supported");

MFMailComposeViewController in a separate class

I seek to create a "Utility Email sender class" that I can use in several iPhone projects.
I created MailSender header and implementation for that purpose.
MailSender.h:
#interface MailSender : NSObject<MFMailComposeViewControllerDelegate>
- (id) initWithParent:(UIViewController*) mainController;
- (void) invokeMailSender:(NSString*) to:(NSString*) subject:(NSString*) failureTitle:(NSString*) failureMessage:(NSString*) failureCancel;
#end
MailSender.m:
#import "MailSender.h"
#implementation MailSender
MFMailComposeViewController* mailer;
UIViewController* mailParentController;
- (id) initWithParent:(UIViewController*) mainController
{
if( self = [super init])
{
mailParentController = mainController;
}
return self;
}
- (void) invokeMailSender:(NSString*) to:(NSString*) subject:(NSString*) failureTitle:(NSString*) failureMessage:(NSString*) failureCancel;
{
if([MFMailComposeViewController canSendMail])
{
mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:subject];
NSArray *toRecipients = [NSArray arrayWithObjects:to, nil];
[mailer setToRecipients:toRecipients];
[mailParentController presentModalViewController:mailer animated:YES];
}
else
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:failureTitle message:failureMessage
delegate:nil cancelButtonTitle:failureCancel otherButtonTitles: nil];
[alert show];
}
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// Do nothing
[mailParentController dismissModalViewControllerAnimated:YES];
mailer = nil;
}
#end
I called the class from a View Controller (in a button touch down action) using the following instructions:
#implementation InfoViewController
MailSender *sender;
- (IBAction)openMail:(id)sender
{
sender = [[MailSender alloc] initWithParent:self];
[sender invokeMailSender:#"test#test.com" :#"123" :#"123" :#"123" :#"123"];
}
....
#end
When I run the code, I am able to show the email views correctly. However, this is then followed by a crash.
Note that I do not have a crash when using MFMailComposeViewController directly from my UIViewController (And assigning the View Controller as the delegate),
Any ideas?
Sorry I am still a new to Objective C :)
You need to retain your sender MailSender instance. It is being released after you call the invoke message.
You could do this by declaring a property named sender. E.g.
#property (strong, nonatomic) MailSender *sender;
...
#synthesize sender = _sender;
...
self.sender = [[MailSender alloc] initWithParent:self];
[self.sender invokeMailSender:#"noor#dimachk.com" :#"123" :#"123" :#"123" :#"123"];
By the way, your method declaration is a bit funny. You should name the arguments. E.g.
- (void)invokeMailSender:(NSString *)sender
to:(NSString *)to
subject:(NSString *)subject
failureTitle:(NSString *)failureTitle
failureMessage:(NSString *)failureMessage
failureCancelButtonTitle:(NSString *)failureCancelButtonTitle

Best way to pass variables between views

I am very new to xcode & Objective-C (having come from PHP) i have started to play around and am finding it very hard to pass variables between views this is what i have so far:
Game_info.h
#import <UIKit/UIKit.h>
#interface Game_Info : UIViewController {
IBOutlet UITextField *groupName;
IBOutlet UISegmentedControl *gameType;
}
#property (nonatomic,retain) IBOutlet UITextField *groupName;
- (IBAction) GameTypePicker;
- (IBAction) KeyboardHide;
- (IBAction) BackBTN;
- (IBAction) NextBTN;
#end
Game_Info.m
#import "I_Dare_YouViewController.h"
#import "Game_Info.h"
#import "Game_TDoR.h"
#implementation Game_Info
#synthesize groupName;
// Next Button
-(IBAction) NextBTN{
if ([groupName.text length] == 0) {
// Alert
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Group Name"
message:#"Please enter a group name"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil
];
[alert show];
[alert release];
}else if([groupName.text length] < 3){
// Alert
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Group Name"
message:#"Please enter a name longer than 3 characters"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil
];
[alert show];
[alert release];
}else{
Game_TDoR *screen = [[Game_TDoR alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
Game_TDoR *screen1 = [[Game_TDoR alloc] initWithNibName:nil bundle:nil];
NSLog(#"Game_Info: %#",self.groupName.text);
screen1.groupNameText = self.groupName.text;
[self presentModalViewController:screen1 animated:YES];
[screen1 release];
}
}
Then in another view / .h / .m file i am trying to get to the 'groupName' property.
Game_TDoR.m
#import "I_Dare_YouViewController.h"
#import "Game_Info.h"
#import "Game_TDoR.h"
#implementation Game_TDoR
#synthesize groupNameText,Testlbl;
- (void)viewDidLoad
{
NSLog(#"Game_TDoR: %#",self.groupNameText);
NSString *msg = [[NSString alloc] initWithFormat:#"Hello, %#",[self.groupNameText capitalizedString]];
[Testlbl setText:msg];
[msg release];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
So what i am trying to do is on the first view page (Game_info) there is an input text box and im trying to pass that to a label on another view page (Game_TDoR)
This is what comes out in the NSLog (note that the second page (Game_TDoR) comes out first in the log.
2011-07-17 00:25:34.765 I Dare You[3941:207] Game_TDoR: (null)
2011-07-17 00:25:34.774 I Dare You[3941:207] Game_Info: Name
Problem solved:
On the next button i needed to add the variable before i moved page (not the other way around - silly noobish thing to do...)
Game_TDoR *screen1 = [[Game_TDoR alloc] initWithNibName:nil bundle:nil];
NSLog(#"Game_Info: %#",self.groupName.text);
screen1.groupNameText = self.groupName.text;
[self presentModalViewController:screen1 animated:YES];
[screen1 release];
Game_TDoR *screen = [[Game_TDoR alloc] initWithNibName:nil bundle:nil];
screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:screen animated:YES];
[screen release];
If you need to pass the value of groupName.text from the Game_Info view controller to the Game_TDoR view controller, which is presented modally by the former, you can declare a property in Game_TDoR to hold the value:
1) Declare a NSString property in Game_TDoR #interface block:
#property (nonatomic,copy) NSString *groupNameText;
(Remember to synthesize or implement the accessor methods in the implementation block)
2) In NextBTN action, after you initialize your Game_TDoR instance, set the property:
Game_TDoR *screen = [[Game_TDoR alloc] init...];
screen.groupNameText = self.groupName.text;
Merely including the header file will not make the variable available. You are only including the definition of the class (class is for PHP what #interface is for Obj-C) You have to instantiate Game_Info and access the variable via the instance of Game_Info.
groupName is a member (ivar) of Game_Info. Its default visibility is #protected, so any classes outside Game_Info can't access it, unless they derive from Game_Info. To make groupName accessible, you can either create a property that exposes it, or you can make it #public. How this is done is documented in the vast documentation for Xcode.
But groupName, being an ivar (instance variable) of an object, only exists if there is in fact an instance of the Game_Info. I assume you have some globally accessible instance of Game_Info in your program, let's call it globalGameInfo. Now you can access its groupName using
UITextField *gName = [globalGameInfo groupName];
Take this in .h file in SecondViewController
NSString *strABC;
Make below function in SecondViewController.m
-(void)setString:(NSString *)strEntered{
strABC=strEntered;
}
Now In First view controller do like this:
SecondViewController.m *objSecond = [[SecondViewController.m] initwithNibName:#"secondView.xib" bundle:nil];
[objSecond setString:#"Comment Controller"];
[self.navigationController pushViewController:objSecond animated:YES];
[objSecond release];
Now, In secondViewController viewWillAppear Method write this.
-(void)viewWillAppear:(BOOL)animated{
lblUserInput.text = strABC;
}
Please check spelling mistakes as I hand written this. Hope this help.
If you are not using navigationContoller then you can do something like this.
SecondViewControler *objSecond = [[SecondViewController] initwithNibName:#"secondview.xib" bundle:nil];
[objSecond setUserInput:txtUserInput.text];
[objSecond viewWillAppear:YES];
[self.view addSubview:objSecond];
[objSecond release];

photo album on iphone

The below code is for go to photo albums.The below code shows an error. I'm adding the UIImagePickerControllerDelegates like this in my .h file:
#interface My_Pictures : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegates>
But I'm not importing anything in my .m file. Should I include any of the Frameworks? Please give me a clear explanation.
ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else {
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
[self presentModalViewController:ipc animated:YES];
You have to implement delegate like this in .h class
#interface My_Pictures : UIViewController<UIImagePickerControllerDelegate>
All the best.
UIImageWriteToSavedPhotosAlbum().