How to post a photos on facebook through iPhone app? - iphone

I am trying to post image on facebook but not successful yet,
my codes are:
- (void)postToWall{
int im = 1;
NSData *myimgData;
myimgData = [NSData dataWithContentsOfFile:saveImagePath];
//pstimg = myimgData;
NSArray *chunks = [pstimg componentsSeparatedByString: #"."];
NSString *atch= [chunks objectAtIndex: 0];
NSString *filePath = [[NSBundle mainBundle] pathForResource:atch ofType:#"jpg"];
img = [[UIImage alloc] initWithContentsOfFile:filePath];
//start
FBDialog* dialog = [[[FBStreamDialog alloc] init] autorelease];
NSString *str = #"Hello";
str = [str stringByReplacingOccurrencesOfString:#" " withString:#"+"];
dialog.cMessage=str;
dialog.userMessagePrompt = #"Enter your message:";
[dialog show];
NSData * findata;
//edited from here
if(im==1)
{
findata = myimgData;
}
else
{
findata = (NSData *)img;
}
NSMutableDictionary * param = [NSMutableDictionary dictionaryWithObjectsAndKeys:
img, #"picture",
nil];
FBRequest *uploadPhotoRequest =[FBRequest requestWithDelegate:self] ;
[uploadPhotoRequest call:#"facebook.photos.upload" params:param dataParam:myimgData];
[img release];
}
But it not posted.

To post a photo on Facebook you need to use the iOS SDK from Facebook:
https://github.com/facebook/facebook-ios-sdk
There you'll find sample app with authentication and much more, like posting a photo.

Related

Change program from getting music from iTunes to local file using URL

I am currently trying to make an app stream raw data from mic over mulipeer connectivity.
I have been using this tutorial as a base https://robots.thoughtbot.com/streaming-audio-to-multiple-listeners-via-ios-multipeer-connectivity
Now however I am struggling with changing the URL from itunes library to my local file.
I am no advanced programmer and this is some kind of summer project.
When the program is getting music from itunes library it uses this code:
- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
[self dismissViewControllerAnimated:YES completion:nil];
if (self.outputStreamer) return;
self.song = mediaItemCollection.items[0];
NSMutableDictionary *info = [NSMutableDictionary dictionary];
info[#"title"] = [self.song valueForProperty:MPMediaItemPropertyTitle] ? [self.song valueForProperty:MPMediaItemPropertyTitle] : #"";
info[#"artist"] = [self.song valueForProperty:MPMediaItemPropertyArtist] ? [self.song valueForProperty:MPMediaItemPropertyArtist] : #"";
MPMediaItemArtwork *artwork = [self.song valueForProperty:MPMediaItemPropertyArtwork];
UIImage *image = [artwork imageWithSize:self.albumImage.frame.size];
if (image)
info[#"artwork"] = image;
if (info[#"artwork"])
self.albumImage.image = info[#"artwork"];
else
self.albumImage.image = nil;
self.songTitle.text = info[#"title"];
self.songArtist.text = info[#"artist"];
[self.session sendData:[NSKeyedArchiver archivedDataWithRootObject:[info copy]]];
NSArray *peers = [self.session connectedPeers];
if (peers.count) {
self.outputStreamer = [[TDAudioOutputStreamer alloc] initWithOutputStream:[self.session outputStreamForPeer:peers[0]]];
[self.outputStreamer streamAudioFromURL:[self.song valueForProperty:MPMediaItemPropertyAssetURL]];
[self.outputStreamer start];
But I want it to get music from the recorder:
NSArray *pathComponents = [NSArray arrayWithObjects:
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject],
#"MyAudioMemo.m4a",
nil];
NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:nil];
player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:nil];
I have been struggling with this for a while now and would appreciate any kind of help!

Attach image to message composer in ios 6

How to attach an image to native message composer in ios6? I want to implement the same sharing via message function we can see in default photos app.
Thanks
For Mail:
MFMailComposeViewController *mailController = [[MFMailComposeViewController alloc] init];
NSData *exportData = UIImageJPEGRepresentation(pic ,1.0);
[mailController addAttachmentData:exportData mimeType:#"image/jpeg" fileName:#"Photo.jpeg"];
[self presentModalViewController:mailController animated:YES];
The only way is through email currently. Unless you want to create a own MMS gateway to allow your app to support MMS..
For Message:
After reading up, instead of using MFMessageComposeViewController,you could UIApplication sharedApplication.
Example:
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
pasteboard.persistent = YES;
NSData *data = UIImageJPEGRepresentation(pic ,1.0);
pasteboard.image = [UIImage imageWithData:data];
NSString *phoneToCall = #"sms: 123-456-7890";
NSString *phoneToCallEncoded = [phoneToCall stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
NSURL *url = [[NSURL alloc] initWithString:phoneToCallEncoded];
[[UIApplication sharedApplication] openURL:url];
Longpress and click on Paste and message will be pasted...
Hope this helps... pic refers to the UIImage you are passing...
{
NSMutableDictionary * attachment = [[NSMutableDictionary alloc]init];
[attachment setObject: UIImageJPEGRepresentation(imageView.image,0.5) forKey:#"attachmentData"];
[attachment setObject: #"productImage.jpeg" forKey:#"attachmentFileName"];
[attachment setObject: #"jpeg" forKey:#"attachmentFileMimeType"];
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject: #"subject" forKey:#"subject"];
[params setObject: #"matterText" forKey:#"matterText"];
[params setObject: [[NSMutableArray alloc]initWithObjects:attachment, nil] forKey:#"attachments"];
}
#pragma mark - Sharing Via Email Related Methods
-(void)emailInfo:(NSMutableDictionary*)info
{
if (![MFMailComposeViewController canSendMail])
{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Email Configuration"
message:#"We cannot send an email right now because your device's email account is not configured. Please configure an email account from your device's Settings, and try again."
delegate:nil
cancelButtonTitle:nil otherButtonTitles:#"OK", nil];
[alert show];
return;
}
MFMailComposeViewController *emailer = [[MFMailComposeViewController alloc] init];
emailer.mailComposeDelegate = self;
NSString * subject = [info objectForKey:#"subject"];
NSString * matterText = [info objectForKey:#"matterText"];
if(subject)
[emailer setSubject:subject];
if(matterText)
[emailer setMessageBody:matterText isHTML:NO];
NSMutableArray * attachments = [info objectForKey:#"attachments"];
if (attachments)
{
for (int i = 0 ; i < attachments.count ; i++)
{
NSMutableDictionary * attachment = [attachments objectAtIndex:i];
NSData * attachmentData = [attachment objectForKey:#"attachmentData"];
NSString * attachmentFileName = [attachment objectForKey:#"attachmentFileName"];
NSString * attachmentFileMimeType = [attachment objectForKey:#"attachmentFileMimeType"];
[emailer addAttachmentData:attachmentData mimeType:attachmentFileMimeType fileName:attachmentFileName];
}
}
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
emailer.modalPresentationStyle = UIModalPresentationPageSheet;
}
[self.navigationController.topViewController presentViewController:emailer animated:YES completion:nil];
}
I think you cant attach an image in the message composer. It is only possible in mail composer.

sharing text on "facebook Page" in ios from our app

I have a Facebook page, and i want to share some text on that page just like my status as we share on our "me/feed" from our application.
Please someone suggest how i should go about it?
for iOS 6.0 you can do something like that:
NSString *text1 = [NSString stringWithFormat:#"%# %#", NSLocalizedString(#"Check out this nice'... ", #""), self.title];
NSString *text2 = [NSString stringWithFormat:#"%# %#", NSLocalizedString(#"in", #""), self.restaurant.title];
NSString *text3 = NSLocalizedString(#"Sent from FoodRock Menus App", #"");
NSURL *appURL = [NSURL URLWithString:#"http://testappstorelink.com"];
NSArray *activityItems = [NSArray arrayWithObjects:text1, text2, text3, appURL, photo, nil];
UIActivityViewController *actVC = [[UIActivityViewController alloc] initWithActivityItems:activityItems
applicationActivities:nil];
actVC.excludedActivityTypes = [NSArray arrayWithObjects:UIActivityTypeAssignToContact, UIActivityTypeCopyToPasteboard,
UIActivityTypePrint, UIActivityTypeSaveToCameraRoll, nil];
actVC.completionHandler = ^(NSString *activityType, BOOL completed){
DLog(#"ACTIVITYTYPE: %# FINISHED: %#", activityType, completed ? #"YES" : #"NO");
if (completed) {
NSString *socialString = #"";
if ([activityType containsSubstring:#"PostToTwitter"]) {
socialString = #"Twitter";
} else if ([activityType containsSubstring:#"PostToFacebook"]) {
socialString = #"Facebook";
} else if ([activityType containsSubstring:#"Message"]) {
socialString = #"Message";
} else if ([activityType containsSubstring:#"Mail"]) {
socialString = #"Mail";
}
[[FRNWAchievementsController controller] reportSocialShareEvent:socialString menuItem:self];
}
};
return actVC;
Here are some code snippets to help you out, but you should also have a look at the Facebook developer examples which are pretty good.
// To log in to Facebook with permission to post...
m_Facebook = [[Facebook alloc] initWithAppId:YOUR_FACEBOOK_APPID
andDelegate:self];
m_FacebookPermissions = [[NSArray
arrayWithObjects:/*#"read_stream",*/ #"publish_stream", nil] retain];
[m_Facebook authorize:m_FacebookPermissions];
And in some other method after the fbDidLogin callback tells you you're logged in:
// After successful login…
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
WHAT_YOU_WANT_TO_SAY, #"message",
nil];
[m_Facebook requestWithGraphPath:#"/me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];

Error With Sending mail (kSKPSMTPPartMessageKey is nil)

I'm trying to send mail in iPhone using "SKPSMTPMessage" and I added the libraries,
In my class I added the following code:
- (IBAction)sendMail:(id)sender
{
// if there are a connection
if ([theConnection isEqualToString:#"true"]) {
if ([fromEmail.text isEqualToString:#""] || [toEmail.text isEqualToString:#""]) {
UIAlertView *warning = [[UIAlertView alloc] initWithTitle:#"تحذير" message:#"لم يتم ادخال جميع المجالات" delegate:self cancelButtonTitle:#"موافق" otherButtonTitles:nil, nil];
[warning show];
}else {
SKPSMTPMessage *test_smtp_message = [[SKPSMTPMessage alloc] init];
test_smtp_message.fromEmail = fromEmail.text;
test_smtp_message.toEmail = toEmail.text;
test_smtp_message.relayHost = #"smtp.gmail.com";
test_smtp_message.requiresAuth = YES;
test_smtp_message.login = #"ebookmsg#gmail.com";
test_smtp_message.pass = #"myPass";
test_smtp_message.wantsSecure = YES;
NSString *subject= #"Suggest a book for you";
test_smtp_message.subject = [NSString stringWithFormat:#"%# < %# > ",fromEmail.text, subject];
test_smtp_message.delegate = self;
NSMutableArray *parts_to_send = [NSMutableArray array];
NSDictionary *plain_text_part = [NSDictionary dictionaryWithObjectsAndKeys:
#"text/plain\r\n\tcharset=UTF-8;\r\n\tformat=flowed", kSKPSMTPPartContentTypeKey,
[messageBody.text stringByAppendingString:#"\n"], kSKPSMTPPartMessageKey,
#"quoted-printable", kSKPSMTPPartContentTransferEncodingKey,
nil];
[parts_to_send addObject:plain_text_part];
// to send attachment
NSString *image_path = [[NSBundle mainBundle] pathForResource:BookCover ofType:#"jpg"];
NSData *image_data = [NSData dataWithContentsOfFile:image_path];
NSDictionary *image_part = [NSDictionary dictionaryWithObjectsAndKeys:
#"inline;\r\n\tfilename=\"image.png\"",kSKPSMTPPartContentDispositionKey,
#"base64",kSKPSMTPPartContentTransferEncodingKey,
#"image/png;\r\n\tname=Success.png;\r\n\tx-unix-mode=0666",kSKPSMTPPartContentTypeKey,
[image_data encodeWrappedBase64ForData],kSKPSMTPPartMessageKey,
nil];
[parts_to_send addObject:image_part];
test_smtp_message.parts = parts_to_send;
Spinner.hidden = NO;
[Spinner startAnimating];
ProgressBar.hidden = NO;
HighestState = 0;
[test_smtp_message send];
}
}else {
UIAlertView *alertNoconnection = [[UIAlertView alloc] initWithTitle:#"تحذير" message:#"لا يوجد شبكة " delegate:self cancelButtonTitle:#"الغاء" otherButtonTitles:nil, nil];
[alertNoconnection show];
}
}
but when I tried to send it gives me the following Exception:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString appendString:]: nil argument'
and it highlighted this line in SKPSMTPMessage.m
[message appendString:[part objectForKey:kSKPSMTPPartMessageKey]];
and I Can't understand what is nil exactly
Can Anyone help me in this issue?
Thanks in Advance.
I found the solution, the problem was because the image_data was null and I replaced it by:
NSString *image_path = [NSString stringWithFormat:#"%#/%#",[[NSBundle mainBundle] bundlePath],[NSString stringWithFormat:#"%#.jpg",BookCover]];
NSString *imgLink = [NSString stringWithFormat:#"http://iktab.com/global/modules/bookstore/files/book_cover_photo/%#",BookCover];
NSString *urlString = [imgLink stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
NSURL *url = [NSURL URLWithString:urlString];
NSData* image_data = [NSData dataWithContentsOfURL:url];

Load more pages in TTLauncherView

In my implementation of TTLauncherView, only loads the first page. Why?
I have 47 items in array, 47 items div 9 items by page, I should have 6 pages.
Thanks for helping.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSMutableString *jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *results = [jsonString JSONValue];
NSArray *photos = [[results objectForKey:#"photosets"] objectForKey:#"photoset"];
launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
launcherView.backgroundColor = [UIColor whiteColor];
launcherView.delegate = self;
launcherView.columnCount = 3;
launcherView.persistenceMode = TTLauncherPersistenceModeNone;
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
for (NSDictionary *photo in photos)
{
NSString *iconURLString = [NSString stringWithFormat:#"http://farm%#.static.flickr.com/%#/%#_%#_s.jpg",
[photo objectForKey:#"farm"], [photo objectForKey:#"server"], [photo objectForKey:#"primary"], [photo objectForKey:#"secret"]];
NSDictionary *title = [photo objectForKey:#"title"];
NSString *itemTitle = [title objectForKey:#"_content"];
TTLauncherItem *itemMenu = [[[TTLauncherItem alloc] initWithTitle:itemTitle
image:iconURLString
URL:nil
canDelete:NO] autorelease];
[itemArray addObject:itemMenu];
}
launcherView.pages = [NSArray arrayWithObject: itemArray];
[self.view addSubview:launcherView];
}
As I recall, TTLauncherView doesn't break up the TTLauncherItem's into pages automatically. You need an array of arrays. All of the launcher item's in the first array will be on the first page, all the launcher item's in the second array will be on the second page etc. It has been a long time since I've used it, but I think that's how it worked.
My modified code with the hint of #Darren
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSMutableString *jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *results = [jsonString JSONValue];
NSArray *photos = [[results objectForKey:#"photosets"] objectForKey:#"photoset"];
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
NSMutableArray *pageArray = [[NSMutableArray alloc] init];
NSNumber *countPage = [[NSNumber alloc] initWithInt:0];
for (NSDictionary *photo in photos)
{
NSString *iconURLString = [NSString stringWithFormat:#"http://farm%#.static.flickr.com/%#/%#_%#_s.jpg",
[photo objectForKey:#"farm"], [photo objectForKey:#"server"], [photo objectForKey:#"primary"], [photo objectForKey:#"secret"]];
NSString *photoCount = [photo objectForKey:#"photos"];
NSDictionary *title = [photo objectForKey:#"title"];
NSString *itemTitle = [title objectForKey:#"_content"];
TTLauncherItem *itemMenu = [[[TTLauncherItem alloc] initWithTitle:itemTitle
image:iconURLString
URL:nil
canDelete:NO] autorelease];
itemMenu.badgeValue = photoCount;
[itemArray addObject:itemMenu];
int value = [countPage intValue];
countPage = [NSNumber numberWithInt:value + 1];
if (countPage == [NSNumber numberWithInt:9]){
countPage = [NSNumber numberWithInt:0];
[pageArray addObject:itemArray];
itemArray = [[NSMutableArray alloc] init];
}
}
[pageArray addObject:itemArray];
launcherView = [[TTLauncherView alloc] initWithFrame:self.view.bounds];
launcherView.backgroundColor = [UIColor blackColor];
launcherView.delegate = self;
launcherView.columnCount = 3;
launcherView.persistenceMode = TTLauncherPersistenceModeNone;
launcherView.pages = pageArray;
[self.view addSubview:launcherView];
}