Posting to Facebook from Sharekit - iphone

I am newbie to sharekit. I am trying to post a simple text: #"Hello" to Facebook, but facing problems in doing so.. It leads me to the login page. I login and then it closes and comes back to the main screen. But when i check my timeline, it is not added..
My ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController{
UIButton *btn;
IBOutlet UIToolbar *toolbar;
}
#property (nonatomic,strong) UIButton *btn;
- (IBAction)btnShareClicked:(id)sender;
#end
My ViewController.m
#import "ViewController.h"
#import "SHK.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize btn;
- (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.
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
self.toolbarItems = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(btnShareClicked:)] ,
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
nil
];
}
return self;
}
- (IBAction)btnShareClicked:(id)sender {
NSString *text = #"Hello";
SHKItem *item = [SHKItem text:text];
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
// For Toolbar
//[actionSheet showFromToolbar:toolbar];
[actionSheet showInView:self.view];
}
#end
I have included the MessageUI,Security,SystemConfiguration frameworks... Need some guidance on this..

First You should try to Check whether you are using the updated ShareKit API in Your Application.if Not then Add That Updated One from The SHareKIt Sites.
Here is Link for The ShareKit Site
Here Is the Code See,Called WhenEVer You want to share text Over the Facebook.
-(void)shareOnFacebook{
SHKItem * ietm1= [[SHKItem alloc] init];
[ietm1 setText:#"Hello"];
SHKFacebook *shkFacebook = [[SHKFacebook alloc] init]
[shkFacebook setSHKItem:item];
}
//see whether you have these Classes in your `ShareKit`.
I hope it may Helpful to you.

Related

Making UIPicker pop up when textfield is tapped

I made a application with the templet of a Single View Application. Then I added a label and connected it to the .h file of my ViewController. Then I made a picker, filled it, then set it (and a toolBar I made) to the textfield. But when I tap the textfield, the picker is just all black. If this made no sense, the code will explain it all.
.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController {
}
#property (weak, nonatomic) IBOutlet UITextField *habitField;
#property (weak, nonatomic) NSArray *PickerData;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSArray *array = [[NSArray alloc] initWithObjects:#"1",#"2",#"3", nil];
self.PickerData = array;
UIToolbar *toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleBlackOpaque;
[toolBar sizeToFit];
[toolBar setBackgroundImage:[UIImage imageNamed:#"red_navigation_bar.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self
action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(releasePicker)];
UIPickerView *Picker = [[UIPickerView alloc] init];
doneButton.image = [UIImage imageNamed:#"button.png"];
[toolBar setItems:#[flexSpace, doneButton] animated:YES];
self.habitField.inputAccessoryView = toolBar;
[self.habitField setInputView:Picker];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [self.PickerData count];
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [self.PickerData objectAtIndex:row];
}
#end
The simulator looks like this
As i see you forgot to set delegate for picker ( UIPickerViewDelegate )
Picker.delegate = self;
Remember to add :)
#interface ViewController : UIViewController<UIPickerViewDelegate> {
}
Cheers

Presenting Modalviewcontroller for Info Button when pressed on UIToolbar programmatically

I want is on main window to present the created modalViewController view when the infobutton is pressed. But when I press Info button on the main window nothing happens.
In the mainviewcontroller.h file I have following code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#interface imageviewViewController : UIViewController{
AVAudioPlayer *audioPlayer;
}
#property (nonatomic, retain) UIToolbar *toolbar;
#property (nonatomic, assign) UIBarButtonSystemItem currentSystemItem;
#property (nonatomic, retain) AVAudioPlayer *audioPlayer;
#end
In the mainviewcontroller.m file have following code:
#import "imageviewViewController.h"
#import "Infoviewcontroller.h"
#implementation imageviewViewController
#synthesize toolbar;
#synthesize currentSystemItem;
#synthesize audioPlayer;
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(Infobuttonpressed)];
// flex item used to put space in between buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *toolbarItems = [NSArray arrayWithObjects: settingsButton, flexItem, systemItem, flexItem, systemItem1,flexItem, systemItem2, flexItem, systemItem3, flexItem, infoItem, nil];
[toolbar setItems:toolbarItems];
[settingsButton release];
[systemItem release];
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[infoItem release];
[flexItem release];
[super viewDidLoad];
}
- (void) Infobuttonpressed: (id) sender
{
Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
[self presentModalViewController:myView animated:YES]; // present view modally
[self.navigationController pushViewController:myView animated:YES]; // add to navigation stack
[myView release];
}
In the Infoviewcontroller.h file have following code:
#import <UIKit/UIKit.h>
#interface Infoviewcontroller : UIViewController <UITextViewDelegate>
{
UITextView *textView;
}
#property (nonatomic, retain) UITextView *textView;
#property (nonatomic, assign) UINavigationBar *navBar;
#end
Then in the infoviewcontroller.m file have the following code:
#import "Infoviewcontroller.h"
#implementation Infoviewcontroller
#synthesize textView;
#synthesize navBar;
-(void)dealloc
{
[textView release];
[navBar release];
[super dealloc];
}
-(void)setupTextView
{
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame] autorelease];
self.textView.textColor = [UIColor redColor];
self.textView.font = [UIFont fontWithName:#"System Bold" size:13];
self.textView.delegate = self;
self.textView.backgroundColor = [UIColor whiteColor];
self.textView.textAlignment = UITextAlignmentCenter;
self.textView.text = #"This is UITextView\nThis is UITextView\nThis is UITextView\nThis is UITextView";
[self.view addSubview: self.textView];
}
- (void)viewDidLoad
{
[super viewDidLoad];
navBar = [[UINavigationBar alloc] init];
UINavigationItem *navItem = [[[UINavigationItem alloc] initWithTitle:#"ModalViewControllerTest"] autorelease];
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(dismissView:)] autorelease];
navItem.rightBarButtonItem = done;
navBar.items = [NSArray arrayWithObject:navItem];
[self.view addSubview:navBar];
}
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:nil
action:#selector(Infobuttonpressed)];
Should be
UIBarButtonItem *infoItem = [[UIBarButtonItem alloc]
initWithTitle:#"Info"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(Infobuttonpressed:)];
Spot the difference? First, you're missing a target (should be self, not nil). Second, the colon at the end of the selector is part of the signature, you were missing it so it was not calling your method (which is InfoButtonPressed:(id)sender).
As an aside, methods should start with a lower case name.
you dont need both pushModalViewController and pushViewController
Assuming your views are already controlled within an instance of a UINavigationController pushModalViewController on its own will work.
edit - remove the reference to pushViewController. Does it now work?
-(void) Infobuttonpressed: (id) sender;
{ Infoviewcontroller *myView = [[Infoviewcontroller alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myView];
UIViewController *pushController = [[UIViewController alloc] init];
[self addSubview:pushController.view];
[pushController presentModalViewController:navController animated:YES];
[myView release];
}
The target of your infoItem is set to nil. You should set this to an instance of the class where the Infobuttonpressed is defined. I assume that is the mainviewController?
Also, the selector to call is specified as: #selector(Infobuttonpressed), while the method is called "Infobuttonpressed:", that is, with a parameter. That will likely result in a runtime error once you fix the target.
Then, there are some peculiarities in the way you handle things. As Nik already answered, you need to choose whether you want to present the view modally (with presentModalViewController), or present it as part of the navigation stack (with pushViewController), you cannot do both.
If you present it as part of the navitation stack, then it already has a navigation bar, and so you should not add one yourself. Just set the properties of the self.navigationItem.
Have a look at the View Controller Programming Guide for more info.

Can't dismiss modal view when tap a button

In "FirstViewController" I declare a button which present the modal view "InfoViewController".
In "InfoViewController", I declare a toolbar with a "modalViewButton" UIButton which dismiss the modal view. But the "OK" UIButton doesn't work. I don't know why.
Here's FirstViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface FirstViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
Here's FirstViewController.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize infoViewController;
- (IBAction)modalViewAction:(id)sender
{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[infoViewController release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* modalViewButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[modalViewButton addTarget:self
action:#selector(modalViewAction:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *modalBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:modalViewButton];
self.navigationItem.leftBarButtonItem = modalBarButtonItem;
[modalBarButtonItem release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Here's InfoViewController.h
#import <UIKit/UIKit.h>
#interface InfoViewController : UIViewController
{
}
-(IBAction)infoDismissAction:(id)sender;
#end
Here's the InfoViewController.m
#import "InfoViewController.h"
#implementation InfoViewController
- (IBAction)infoDismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
UILabel *infoLabel = [[UILabel alloc] init];
infoLabel.frame = CGRectMake(50, 100, 100, 40);
infoLabel.textAlignment = UITextAlignmentCenter;
infoLabel.text = #"About";
[self.view addSubview:infoLabel];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"OK"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(infoDismissAction:)];
UIBarButtonItem* infoTitle = [[UIBarButtonItem alloc] initWithTitle:#"About"
style:UIBarButtonItemStylePlain
target:self action:nil];
NSArray *barButtons = [[NSArray alloc] initWithObjects:flexibleSpace,flexibleSpace,infoTitle,flexibleSpace,doneButton,nil];
[toolBar setItems:barButtons];
[self.view addSubview:toolBar];
[toolBar release];
[infoTitle release];
[doneButton release];
[barButtons release];
[infoLabel release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
I would solve this issue with a delegate method.
First make a protocol in your modalViewController
#protocol ModalViewDelegate <NSObject>
- (void)didDismissModalView;
#end
And set a delegate property in the same modalVC:
id<ModalViewDelegate> dismissDelegate;
Then make a buttonActionMethod that calls the delegate in the modalVC:
- (void)methodCalledByButton:(id)sender
{
// Call the delegate to dismiss the modal view
[self.dismissDelegate didDismissModalView];
}
Now your modalVC is done you have to prepare the mainVC calling the modalVC:
You have to make your MainViewController comform to the delegate:
#interface MainViewController : UIViewController <ModalViewDelegate>
At the place you alloc your ModalViewController you have to set the delegate property you made in your modalViewController:
self.myModalViewController.dismissDelegate = self;
Now the MainViewController listens to the delegate and the only thing you need to do is implement the delegateMethod.
-(void)didDismissModalView
{
[self dismissModalViewControllerAnimated:YES];
}
Now your ModalVC will dismiss on a buttonpress (at least when you call the method properly)
Hope this all makes sense.
Good luck.
You can only dismiss currently displayed modal view, so in your method infoDismissAction: you should do one of following
1) [self dismissModalViewControllerAnimated:YES];
2) Send to parent view controller message that current modal view should be dismissed and send reference to that view.
Second approach is better as it is more safe.
In your -infoDismissAction try to call [self dismissModalViewControllerAnimated:YES];
Here the best sample code for the model view for iphone and ipad also.
The popups have a number of configurable items. They can be animated to either slide or popup onto the display. Once visible, they can be dismissed either by tapping the screen or after a programmed delay. The background and text colors can also be adjusted however you like.
Download the sample code from here.
The current answers are deprecated. Here is the updated code:
[self dismissViewControllerAnimated:NO completion:nil];

Bad Exception on dealloc?

I having a problem with a Bad Exception that I could not locate at first, but now have it pinned down on a [super dealloc];, but I have no idea why this happens.
Here is my code :
EditingViewController.h
#interface EditingViewController : UIViewController
{
NSManagedObject *editedObject;
NSString *editedFieldKey;
NSString *editedFieldName;
}
#property (nonatomic, retain) NSManagedObject *editedObject;
#property (nonatomic, retain) NSString *editedFieldKey;
#property (nonatomic, retain) NSString *editedFieldName;
- (IBAction)cancel;
- (IBAction)save;
#end
EditingViewController.m
#import "EditingViewController.h"
#implementation EditingViewController
#synthesize editedObject, editedFieldKey, editedFieldName;
#pragma mark -
#pragma mark View lifecycle
- (void)viewDidLoad
{
self.title = NSLocalizedString(editedFieldName, nil);
// Configure the save and cancel buttons.
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:#selector(save)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancel)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (IBAction)save
{
[self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)cancel
{
// Don't pass current value to the edited object, just pop.
[self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc
{
[editedObject release];
[editedFieldKey release];
[editedFieldName release];
//[super dealloc];
}
#end
As you can see, I commented the [super dealloc];, which is causing the Bad Exception, but this is obviously not a good solution.
Any idea what I am doing wrong ?
Thanks
The properties might have never been used, so they haven't been initialized in any way
- (void)dealloc
{
self.editedObject = nil;
self.editedFieldKey = nil;
self.editedFieldName = nil;
[super dealloc];
}

Why Isn't My Info Button Working?

My Info button is showing up when I run my app but it doesn't do anything, no response when I click it.
In my ProfileViewController file:
- (void)viewDidLoad
{
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
infoButton.frame = CGRectMake(290.0, 10.0, 15.0, 15.0);
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:infoButton];
[super viewDidLoad];
}
I also have the following two methods to load the about view (the screen that loads up when the button is clicked):
- (IBAction) toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
[self.navigationController presentModalViewController:theController animated:TRUE];
}
- (IBAction) toggleCreditsClosed:(id)inSender
{
[self.navigationController dismissModalViewControllerAnimated:TRUE];
}
EDIT:
I am adding my full implementation file here:
#import "ProfileViewController.h"
#import "AboutViewController.h"
#implementation ProfileViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (IBAction)toggleCreditsOpen:(id)inSender
{
UIViewController *theController = [[UIViewController alloc] initWithNibName:#"AboutViewController" bundle:nil];
[self.navigationController presentModalViewController:theController animated:YES];
}
- (IBAction)toggleCreditsClosed:(id)inSender
{
[self.navigationController dismissModalViewControllerAnimated:TRUE];
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
UIButton *infoButton = [[UIButton buttonWithType:UIButtonTypeInfoDark] retain];
infoButton.frame = CGRectMake(290.0, 10.0, 15.0, 15.0);
//[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchUpInside];
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];
[self.view addSubview:infoButton];
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Using your provided code works for me, if self.navigationController is non-nil, and there exists an AboutViewController.xib in your main bundle. Otherwise, I don't see anything readily wrong.
If you don't have a navigation controller, the proper line in toggleCreditsOpen: would be:
[self presentModalViewController:theController animated:YES];
Edit:
If you want to dismiss the modal view later, its usually a good idea to do this with a delegate. Instead of
UIViewController *theController = [[UIViewController alloc] initWithWhatever];
you can do
AboutViewController *theController = [[AboutViewController alloc] initWithWhatever];
where AboutViewController has a delegate. Probably something like id<DismissModalProtocol> delegate;. Then, your view controller would implement this #protocol, and before it calls presentModalViewController:animated:, it'll set itself as the delegate. So very roughly, it would look something like this:
// AboutViewController.h
#protocol DismissModalProtocol;
#interface AboutViewController : UIViewController {
id<DismissModalProtocol> delegate;
}
#property id<DismissModalProtocol> delegate;
#end
#protocol DismissModalProtocol <NSObject>
- (void)dismissController:(UIViewController *)viewController;
#end
// ProfileViewController.h
#import "AboutViewController.h"
#interface ProfileViewController : UIViewController <DismissModalProtocol>
#end
// ProfileViewController.m
#implementation ProfileViewController
- (void)dismissController:(UIViewController *)viewController {
[self dismissModalViewControllerAnimated:YES];
}
- (void)toggleCreditsOpen:(id)sender {
AboutViewController *controller = [[AboutViewController alloc] init];
controller.delegate = self;
[self presentModalViewController:controller animated:YES];
}
#end
I don't know at all why this isn't working, but if I were to take a guess, I'd say it's in the [self.navigationController presentModalViewController:theController animated:TRUE];
statement. From what I know, it should be:
[self.navigationController presentModalViewController:theController animated:YES];
See if it works after you do that. I'm not sure, but that could be the problem with your code.
hi
you are took a mistake to define your button..
just assign retain property at the end of button
like
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark] retain];
and also give
[infoButton addTarget:self action:#selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];