I'm trying to create a New User using Parse.com as my backend. I was able to successfully register the user's info. But, my problem is saving the user's profile picture along with it. After the user selects the picture using the camera or photo library, I am presenting an image editor (https://github.com/heitorfr/ios-image-editor) using a .xib file. This all works fine with no problems. Then the image is placed in the UIImageView box and the user proceeds to fill in the Username, email and password and they click signup. The problem is after they sign up and all the info gets placed in the table via Parse, the image container seems to be empty. When I click on the image file, the picture is not there. I can't seem to figure out what I'm doing wrong.
View Did Load
- (void)viewDidLoad {
[super viewDidLoad];
self.addPhotoImageView.contentMode = UIViewContentModeScaleAspectFill;
// Create add photo popup
self.addPhotoActionSheet = [[UIActionSheet alloc] initWithTitle:#"Select source" delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Take photo", #"Choose photo", nil];
// Create image picker
self.imagePicker = [[UIImagePickerController alloc] init];
self.imagePicker.delegate = self;
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
self.imagePicker.editing = YES;
}
Image Picker Controller Delegate Methods
// Switches to the image editing view
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self.imagePicker dismissViewControllerAnimated:NO completion:nil];
if (self.imageEditorViewController == nil) {
self.imageEditorViewController = [[HFImageEditorViewController alloc] initWithNibName:#"ImageEditor" bundle:nil]; }
self.imageEditorViewController.sourceImage = image;
self.imageEditorViewController.rotateEnabled = NO;
self.imageEditorViewController.checkBounds = YES;
[self.imageEditorViewController reset:NO];
__weak typeof(self) weakSelf = self;
self.imageEditorViewController.doneCallback = ^(UIImage *editedImage, BOOL canceled) {
if (!canceled) {
[weakSelf.addPhotoImageView setImage:editedImage];
}
// Hide editor
[weakSelf.imageEditorViewController dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:self.imageEditorViewController animated:NO completion:nil]; }
- (void) imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:nil]; }
UIActionSheetDelegate methods
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0:
self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1:
self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
default:
return;
}
[self presentViewController:self.imagePicker animated:YES completion:nil]; }
Here is IBAction signup code:
- (IBAction)signup:(id)sender {
NSData *imageData = UIImageJPEGRepresentation(self.chosenImage, 0.5f);
PFFile *imageFile = [PFFile fileWithName:#"Profileimage.jpg" data:imageData];
NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *email = [self.emailField.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([username length] == 0 || [email length] == 0 || [password length] == 0) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Oops" message:#"Make sure you fill in all the fields!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else {
PFUser *newUser = [PFUser user];
newUser[#"ProfilePic"] = imageFile;
newUser.username = username;
newUser.email = email;
newUser.password = password;
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:[error.userInfo objectForKey:#"error"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
}
}
There's no indication from the Parse.com API docs that any information other than password and username are set when using - signUpInBackgroundWithBlock:
You will have to sign up the user first, then have a nested call within the PFBooleanResultBlock of the signUp method to perform
else {
PFUser *newUser = [PFUser user];
newUser.username = username;
newUser.email = email;
newUser.password = password;
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:[error.userInfo objectForKey:#"error"] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else {
newUser[#"ProfilePic"] = imageFile;
[newUser saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (! error) {
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
}
}];
}
You need to set it up to where you are sending a file to the database, since Parse will store images as such. Here's how I save images in my projects.
.h file:
#property (nonatomic, weak) IBOutlet UITextField *username
#property (nonatomic, weak) IBOutlet UITextField *email;
#property (nonatomic, strong) IBOutlet UIImageView *photo;
#property (nonatomic, strong) IBOutlet UIButton *addUserPhoto;
.m file in your save method
NSString *username = _username.text;
NSString *email = _email.text;
PFObject *newUser = [PFObject objectWithClassName:#"User"];
[newUser setObject:username forKey:#"username"];
[newUser setObject:email forKey:#"email"];
// image
NSData *imageData = UIImageJPEGRepresentation(_photo.image, 0.8);
NSString *filename = [NSString stringWithFormat:#"%#.png", _name.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[newUser setObject:imageFile forKey:#"profileImage"];
Replace _Photo with whatever you named the image view. It will set the uploaded image to this view.
Related
I have a problem.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[self dismissViewControllerAnimated:YES completion:^{
if (image == nil){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Try again !" message:#"Please try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[self presentViewController:self.imagePicker animated:NO completion:nil];
}else{
NSData *fileData;
NSString *fileName;
NSString *fileType;
fileData = UIImagePNGRepresentation(image);
fileName = #"profilePic.png";
fileType = #"Image";
PFFile *file = [PFFile fileWithName:fileName data:fileData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"An error occurred!"
message:[NSString stringWithFormat:#"Please try sending your message again. %#",error.userInfo]
delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}else{
[[PFUser currentUser]setObject:file forKey:#"profilePic"];
[[PFUser currentUser] saveInBackgroundWithBlock:^(BOOL succeed, NSError* error){
if (!error) {
NSLog(#"New profile pic loaded.");
[self.activity stopAnimating];
[self.reloadPicBtn setHidden:NO];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Try again !" message:#"Please try again. Failed to load your new profile pic :(" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
}
}];
}
}];
}
}];
}
Well, reloadPicBtn is shown, but if I call a function which reloads a picture, It fails. And when I try to understand WTF is going on using NSLog it logs out a string. So code has been executed. But there is no result on screen. The function is the same which is called in viewDidLoad. But it works when it is called from viewDidLoad. And when it is called again there is no result.
That reload function:
- (void) loadProfilePic{
PFQuery* query = [PFUser query];
[query getObjectInBackgroundWithId:[PFUser currentUser].objectId block:^(PFObject* object, NSError* error){
if(!error){
PFFile* imageFile = object[#"profilePic"];
[imageFile getDataInBackgroundWithBlock:^(NSData* data, NSError* error){
if (!error) {
self.activity.hidden = NO;
[self.activity startAnimating];
self.profilePic.image = [UIImage imageWithData:data];
NSLog(#"Profile pic shown");
}
else{
NSLog(#"Error 2: %#",error);
}
}];
}else{
self.profilePic.image = [UIImage imageNamed:#"profile#2.png"];
NSLog(#"Fail 1 : %#",error);
}
}];
}
Please help me.
It prints out profile pic shown. But it doesn't even use network to download an image.
Ive seen many posting on here with phenomenal answers by the community. Unfortunately none have been able to help or guide me to what i need help with. Im trying to "fetch" the image that the "current user" uploaded to parse on the same "UIImageView"
below is the example of how i uploaded the photo
myaccount.h
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)UploadPhoto:(id)sender;
- (IBAction)selectPhoto:(id)sender;
myaccount.m
- (IBAction)UploadPhoto:(UIButton *)sender {
PFObject *User = [PFUser currentUser];
NSData *imageData = UIImageJPEGRepresentation(_imageView.image, 0.8);
NSString *filename = [NSString stringWithFormat:#"%#.png", _username.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[User setObject:imageFile forKey:#"ProfilePicture"];
// Show progress
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = #"Uploading";
[hud show:YES];
// Upload Profile Picture to Parse
[User saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
[hud hide:YES];
if (!error) {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Upload Complete" message:#"Successfully uploaded profile picture" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
// Dismiss the controller
//[self dismissViewControllerAnimated:YES completion:nil];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Upload Failure" message:[error localizedDescription] delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}];
}
i honestly have no idea where to go from here! the photo uploads perfectly to the class on parse.com... but i cannot get it to show everytime the app viewdidloads this controller.
i tried
- (void)viewDidLoad {
[super viewDidLoad];
PFFile * myPFFile = [[PFUser currentUser] objectForKey:#"ProfilePicture"];
[myPFFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:data];
// image can now be set on a UIImageView
}
}];
but i get unused variable for "image" on UIImage *image = [UIImage imageWithData:data];
what am i doing wrong. Someone please help me with any codes I'm missing. I've been at this for 3 months now and its stopping me from moving forward.... PPPPLLZZZ help
What am I missing? Do you put the image inside an imageView?
(thats why you get 'unused variable for "image"')
in your case:
[imageView setImage:image];
If I save text without having any text in the field I get this error message in Parse.com: Update Failure - The operation couldn't be completed(Parse error 122.) If I press OK and then try to dismiss the view with Cancel(a button item) the app crashes. I think a valid file name at Parse.com has to contain at least 1 character. Maybe I can do do something to stop the user from saving when not enter text? Any ideas?
This my code:
- (IBAction)save:(id)sender {
// Create PFObject with profile information
PFUser *profile = [PFUser currentUser];
[profile setObject:nameTextField.text forKey:#"name"];
[profile setObject:titleTextField.text forKey:#"title"];
[profile setObject:locationTextField.text forKey:#"location"];
// Profile image
NSData *imageData = UIImageJPEGRepresentation(profileImageView.image, 0.8);
NSString *filename = [NSString stringWithFormat:#"%#", nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:#"profileimageFile"];
// Show progress
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = #"Updating";
[hud show:YES];
// Upload profile to Parse
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0)
[hud hide:YES];
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
[[[UIAlertView alloc] initWithTitle:#"Profile Information" message:#"Fill in atleast one field" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil]show];
[hud hide:YES];
}
else {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Successfully updated profile" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[hud hide:YES];
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"profile" sender:self];
}
}];
}
- (IBAction)Cancel:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"profile" sender:self];
}
1
Test if your one letter theory is true. Change:
NSString *filename = [NSString stringWithFormat:#"%#", nameTextField.text];
To:
NSString *filename = [NSString stringWithFormat:#"file%#", nameTextField.text];
2
Or just avoid it if it's blank. So this:
NSString *filename = [NSString stringWithFormat:#"%#", nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:#"profileimageFile"];
Becomes:
if (nameTextField.text) {
NSString *filename = [NSString stringWithFormat:#"%#", nameTextField.text];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:#"profileimageFile"];
}
3
Also, what is this:
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0)
It's doesn't appear to be connected to anything?
4
You call this twice in quick succession, and then again right after the file saves. Is there something in the method that makes the repetitive calls necessary?
[hud hide:YES];
5
Your if statement doesn't appear to be connected to anything:
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0)
I'm assuming you want:
if(nameTextField.text.length==0 && titleTextField.text.length==0 && locationTextField.text.length==0) {
[hud hide:YES];
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
[[[UIAlertView alloc] initWithTitle:#"Profile Information" message:#"Fill in atleast one field" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil]show];
[hud hide:YES];
}
else {
// Show success message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message:#"Successfully updated profile" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
[hud hide:YES];
[self dismissViewControllerAnimated:YES completion:nil];
[self performSegueWithIdentifier:#"profile" sender:self];
}
}];
}
I am new to iOS
I just want to get info such as gender, city, e-mail, and date of birth
but from code which I posted below ,I just got an idea like how to post data on Facebook,
now I want to fetch user details form Facebook
.h
#import <UIKit/UIKit.h>
#import <Social/Social.h>
#interface ViewController : UIViewController
- (IBAction)PostFB:(id)sender;
- (IBAction)PostTW:(id)sender;
#end
.m
//
// ViewController.m
// FaceBookFirstApp3
//
// Created by hits1 on 27/01/14.
// Copyright (c) 2014 hits1. All rights reserved.
//
#import "ViewController.h"
#import <Social/Social.h>
#interface ViewController ()
#end
#implementation ViewController
- (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.
}
- (IBAction)PostFB:(id)sender {
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:#"First post from my iPhone app"];
[self presentViewController:controller animated:YES completion:Nil];
}
}
- (IBAction)PostTW:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"Great fun to learn iOS programming at appcoda.com!"];
[tweetSheet addURL:[NSURL URLWithString:#"http://www.appcoda.com"]];
[tweetSheet addImage:[UIImage imageNamed:#"socialsharing-facebook-image.jpg"]];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
}
#end
please, help me Out to Fetch at least one field(city (or) gender (or) phone-number) form Gmail or Facebook
You can use the below code to get user's gender, city, e-mail, and date of birth
In .h file add,
#import <Accounts/Accounts.h>
#property (nonatomic, retain) ACAccountStore *accountStore;
#property (nonatomic, retain) ACAccount *facebookAccount;
-(void)get;
-(void)attemptRenewCredentials;
- (void) getuserdetails;
in .m file
- (void) getuserdetails
{
self.accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSString *key = #"your app id";
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,#[#"email"],ACFacebookPermissionsKey, nil];
[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
self.facebookAccount = [accounts lastObject];
NSLog(#"facebook account =%#",self.facebookAccount);
[self get];
} else {
dispatch_async(dispatch_get_main_queue(), ^{
// NSLog(#"%#",e.description);
if([e code]== ACErrorAccountNotFound)
{
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:#"Account not found"
message:#"Please setup your Facebook account in Settings App" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[alt show];
}
else
{
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:#"Access Denied"
message:#"" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[alt show];
}
});
NSLog(#"error getting permission %#",e);
}
}];
}
-(void)get
{
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:nil];
request.account = self.facebookAccount;
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {
if(!error)
{
list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSLog(#"Dictionary contains: %#", list );
NSLog(#"EmailID %#",[list objectForKey:#"email"]);
NSLog(#"Birthday %#",[list objectForKey:#"birthday"]);
NSLog(#"Gender %#",[list objectForKey:#"gender"]);
NSLog(#"City %#",[[list objectForKey:#"location"] objectForKey:#"name"]);
if([list objectForKey:#"error"]!=nil)
{
[self attemptRenewCredentials];
}
dispatch_async(dispatch_get_main_queue(),^{
});
}
else{
//handle error gracefully
NSLog(#"error from get%#",error);
//attempt to revalidate credentials
}
}];
}
-(void)attemptRenewCredentials{
[self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){
if(!error)
{
switch (renewResult) {
case ACAccountCredentialRenewResultRenewed:
NSLog(#"Good to go");
[self get];
break;
case ACAccountCredentialRenewResultRejected:
{
NSLog(#"User declined permission");
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:#"Access Denied"
message:#"You declined permission" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[alt show];
break;
}
case ACAccountCredentialRenewResultFailed:
{
NSLog(#"non-user-initiated cancel, you may attempt to retry");
UIAlertView* alt = [[UIAlertView alloc] initWithTitle:#"Access Denied"
message:#"non-user-initiated cancel, you may attempt to retry" delegate:self cancelButtonTitle:nil otherButtonTitles:#"Ok",nil];
[alt show];
break;
}
default:
break;
}
}
else{
//handle error gracefully
NSLog(#"error from renew credentials%#",error);
}
}];
}
-(void)accountChanged:(NSNotification *)notif//no user info associated with this notif
{
[self attemptRenewCredentials];
}
- (void)sessionStateChanged:(NSNotification*)notification
{
if (FBSession.activeSession.isOpen)
{
[[FBRequest requestForMe] startWithCompletionHandler:
^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error)
{
if (!error)
{
//for geting the user detail.
NSString *fbSelectDetails = [#"" stringByAppendingFormat:#"https://graph.facebook.com/me? fields=id,name,email,first_name,last_name,gender,picture,link,birthday,quotes,education&access_token=%#",FBSession.activeSession.accessToken];
NSMutableDictionary *userDetails = [NSMutableDictionary dictionaryWithDictionary:[DataLayer getJSONContentFromURL:fbSelectDetails]];
NSString *uid = [userDetails valueForKey:#"id"];
NSString *userName= user.name;
NSString *userEmail= [userDetails valueForKey:#"email"];
NSString *userFirstName= [userDetails valueForKey:#"first_name"];
NSString *userLastName= [userDetails valueForKey:#"last_name"];
NSString *userGender= [userDetails valueForKey:#"gender"];
NSString *userLink= [userDetails valueForKey:#"link"];
NSString *userDob= [userDetails valueForKey:#"birthday"];
NSMutableDictionary *imgUrl = [NSMutableDictionary dictionaryWithDictionary:[userDetails valueForKey:#"picture"]];
NSString *userImgUrl= [[imgUrl valueForKey:#"data"] valueForKey:#"url"];
}];
}
}
It's a sample Twitter application that I made folllowing the tutorial in Apple's developer site. But I don't know what I did wrong for this to happen.
Interface:
#interface TWTViewController : UIViewController {
NSString* output;
}
#property (nonatomic, copy) NSString* output;
- (IBAction)doTweet:(id)sender;
- (IBAction)getTimeline:(id)sender;
#property (weak, nonatomic) IBOutlet UILabel *outputLabel;
#property (weak, nonatomic) IBOutlet UIButton *tweetButton;
#end
Implementation:
#implementation TWTViewController
#synthesize output = _output;
#synthesize outputLabel;
#synthesize tweetButton;
...
- (IBAction)doTweet:(id)sender {
TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];
[twitter setInitialText:#"It's really that simple!"];
[twitter addImage:[UIImage imageNamed:#"twitter.png"]];
[self presentViewController:twitter animated:YES completion:nil];
twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {
if(res == TWTweetComposeViewControllerResultDone) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Success!" message:#"Your Tweet was posted succesfully." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
else if(res == TWTweetComposeViewControllerResultCancelled) {
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Your Tweet was not posted." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
[self dismissModalViewControllerAnimated:YES];
};
}
- (IBAction)getTimeline:(id)sender {
ACAccountStore* store = [[ACAccountStore alloc] init];
ACAccountType* twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[store requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
NSArray* twitterAccounts = [store accountsWithAccountType:twitterAccountType];
if([twitterAccounts count] > 0) {
ACAccount* account = [twitterAccounts objectAtIndex:0];
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"1" forKey:#"include_entities"];
NSURL* url = [NSURL URLWithString:#"http://api.twitter.com/1/statuses/home_timeline.json"];
TWRequest* request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
[request setAccount:account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if(error != nil) {
self.output = [error localizedDescription];
self.outputLabel.text = self.output;
}
else {
NSError* jsonError;
NSArray* timeline = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&jsonError];
if(jsonError == nil) {
self.output = [timeline componentsJoinedByString:#"|"];
self.outputLabel.text = self.output;
}
else {
self.output = [jsonError localizedDescription];
self.outputLabel.text = self.output;
}
}
}];
}
}
}];
}
#end
Here's the ZIP file containing the whole project: http://www.mediafire.com/?yi4x3d6qn1x4p4r
Any help would be greatly appreciated.
Check ALL of your connections in IB. I know it sounds stupid but it gets me all the time...
The "Get Timeline" button is currently set to fire both doTweet: and getTimeline:. Right-click the Get Timeline button in IB and you'll see both listed under Sent Events->Touch Up Inside. Click the little X next to doTweet:, and you should be all good.
#Szwedo's advice is good; always check your connections and actions in IB.