iOS - Integrating Facebook framework - ios

In my iOS app, I successfully integrated latest facebook framework.
Its working fine if "facebook" app is not on my iphone. If the app is not on my iphone, during authentication, its opening the browser and authenticating properly.
But if the facebook app is there on my iphone, after authentication and when returning to my app, its crashing with following error:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SBJsonParser errorTrace]: unrecognized selector sent to instance 0x2d9f60'
I have seen the samples provided by facebook and implemented the same in my app. Can some one point out me what might be the issue?

This is a bit of a stab in the dark but I think you might be calling the method errorTrace on an instance of SBJsonParser :)
What's probably happening is you're calling errorTrace on something that you have released early. Go through the code path triggered when facebook reopens your app and find a call to errorTrace. Set a breakpoint just before it and have a look around to see what's wrong.

If you want only share something in iOS 6.0 so you use social and account framework and do the job by iPhone internal login using following code
-(void)facebook_Share_in_IOS6.0{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) //check if Facebook Account is linked
{
mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social plattform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#"Test",mySLComposerSheet.serviceType]]; //the message you want to post
// [mySLComposerSheet addImage:yourimage];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Attention" message:#"First Set your facebook account. To post your answer to facebook" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successfull";
break;
default:
break;
} //check if everything worked properly. Give out a message on the state.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}];
}

Related

Tweeting using Twitterkit fails when Twitter app is not installed in iOS 11

I am following the documentation of twitter and below is the example code they have given to check logged in user session and tweet using the TWTRComposer.
// Check if current session has users logged in
if ([[Twitter sharedInstance].sessionStore hasLoggedInUsers]) {
TWTRComposerViewController *composer = [TWTRComposerViewController emptyComposer];
[fromController presentViewController:composer animated:YES completion:nil];
} else {
[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
if (session) {
TWTRComposerViewController *composer = [TWTRComposerViewController emptyComposer];
[fromController presentViewController:composer animated:YES completion:nil];
} else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"No Twitter Accounts Available" message:#"You must log in before presenting a composer." preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
}
}];
}
Now, for the case when Twitter app is not installed, the flow goes to the else part given below:
else {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"No Twitter Accounts Available" message:#"You must log in before presenting a composer." preferredStyle:UIAlertControllerStyleAlert];
[self presentViewController:alert animated:YES completion:nil];
}
Twitterkit should open webview here to ask for twitter login, which is not happening. Anyone have any clue? Any help would be greatly appreciated.
Thanks & Cheers.
Got it fixed after a lot of trials. And the fix is such a stupid thing:
Go to your app in twitter dashboard: https://apps.twitter.com/ > Go to app settings > Go to Callback URL and fill with any working url. And voila,
it will load the webview if the Twitter app is not installed.
Hope it helps someone.

When should I renew an ACAccount? Or, how to check if the credential is expired. (Facebook)

Recently I was assigned to survey how to use iOS framework ACAccount and Social to implement facebook post function. It is quite simple to gain access of the account configured in setting.
if(!_accountStore)
_accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookTypeAccount = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Check if there is any faceboook account
NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
if (![accounts count]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"alert" message:#"No facebook account configured in setting." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
[_accountStore requestAccessToAccountsWithType:facebookTypeAccount
options:#{ACFacebookAppIdKey: #"FACEBOOK-APP-ID", ACFacebookPermissionsKey: #[#"email"]}
completion:^(BOOL granted, NSError *error) {
if(granted){
NSArray *accounts = [_accountStore accountsWithAccountType:facebookTypeAccount];
_facebookAccount = [accounts lastObject];
NSLog(#"Success");
}else{
// ouch
NSLog(#"Failed, Error: %#", error);
dispatch_async(dispatch_get_main_queue(), ^{
NSString *message = [NSString stringWithFormat:#"App access denied, please grant access in Setting->Facebook. Error message::%#", error];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Alert" message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
});
}
}];
Once the app gain access to facebook, it can post message by using SLComposeViewController:
- (IBAction)postButtonPressed:(id)sender {
if (!_facebookAccount) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"alert" message:#"login first" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
return;
}
SLComposeViewController *fbController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
[fbController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Posted....");
}
break;
}};
[fbController setInitialText:#"Test message"];
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
} else {
NSLog(#"no facebook setup");
}
Here comes my question. I found that there is a method in ACAccountStore which is used to renew expired credential of an ACAccount:
- (void)renewCredentialsForAccount:(ACAccount *)account completion:(ACAccountStoreCredentialRenewalHandler)completionHandler;
But I don't even know how to check whether the credential is expired so that I can renew it. Anyone got an idea about this?
Oh, by the way, we just want to use the native Social framework to do simple work such as post some message or picture. So, if not needed, we are not going to use the facebook SDK.
If you know anything about how to check the credential is valid or not, please leave a comment or submit an answer, thank you:)
Updates 2013.11.20 11:10 AM
I learn something from my experiments to this issue..
One is not able to get certain type of accounts from account store before he gains access to them, so I should not check account count before request for access.
Renew notification called when the app using ACAccount is in background after facebook account changed. Currently, I only saw changes of access right triggers the notification.
If the user changes password, the system will pop out an alert when the user attempt to post something, which ask the user to change password.
I think monitor notifications of account change is enough to handle the changes. I'll accept the first answer.
You should renew the user acc everytime it is out of sync. This may happen if the user has changed his password or when the acc session has expired.
Yo can know you are in that scenario using the following notification:
ACAccountStoreDidChangeNotification
I don't think there is active way , best is to write renew function and call
renewCredentialsForAccount of the framework

Facebook posting

here is what I did
Created an app on facebook
installed the FB SDK with all the requirements
logged in the user via a login button
now I am trying to post with the code provided here from this threat
How to Post to Facebook on iOS 6 in Objective-C using ACAccountStore class
But nothing happens. Am I missing a step?
This is the code I am using
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) //check if Facebook Account is linked
{
mySLComposerSheet = [[SLComposeViewController alloc] init]; //initiate the Social Controller
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social plattform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#"Test",mySLComposerSheet.serviceType]]; //the message you want to post
[mySLComposerSheet addImage:yourimage]; //an image you could post
//for more instance methodes, go here:https://developer.apple.com/library/ios/#documentation/NetworkingInternet/Reference/SLComposeViewController_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40012205
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"Action Cancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successfull";
break;
default:
break;
} //check if everything worked properly. Give out a message on the state.
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
Your setInitialText method is incorrect.
If you use stringWithFormat to post the serviceType, you need to add it to the method with "%#", otherwise there isn't anything there to say it to appear.
In short, just change :
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#"Test",mySLComposerSheet.serviceType]];
to :
[mySLComposerSheet setInitialText:[NSString stringWithFormat:#"Test %#",mySLComposerSheet.serviceType]];

Tweet a link from iOS app

I am creating a program in xCode that basically lets you post some something to your twitter. Everything works except that i am trying to figure a way to post a hypertext that will lead to say google.
Forexample i want the words wwww.google.com to lead to google when i click it..Any help is appreciated. Thanks
This is the code
-(void)tweetTapped{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"Post to Twitter :) www.google.com"];
[self presentViewController:tweetSheet animated:YES completion:nil];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You can't send a tweet right now, make sureyour device has an internet connection and you haveat least one Twitter account setup"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
You can't use hypertext in a tweet, but if you include a URL it should be automatically linked for you by Twitter. Twitter will generally use a URL shortener too. So just use a format like this:
NSString *tweet = #"This is a link to http://google.com";

How to Change "via iOS" on native Facebook sharing in IOS6

Hi i am using following code to share message using IOS6 native lib
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:#"Good morning all"];
//[mySLComposerSheet addImage:[UIImage imageNamed:#"img1.png"]];
[mySLComposerSheet addURL:[NSURL URLWithString:#"http://www.google.com/"]];
[self.superViewController presentViewController:mySLComposerSheet animated:YES completion:nil];
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output = #"";
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"ACtionCancelled";
break;
case SLComposeViewControllerResultDone:
output = #"Post Successfull";
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook Message" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
}
it is successfully posted But in my facebook it is showing via IOS but i want to display my application name. I followed all steps when i am creating app id on facebook developer zone
How to change IOS to my application name?
I also tried this code http://www.developers-life.com/facebook-compose-view.html
When using SLComposeViewController you cannot change the "via" name, it will always show "iOS" as the source.
You are required to use the Facebook iOS SDK and set up your own custom compose view instead of using the native SLComposeViewController.
UPDATE
Facebook's 3.5+ SDK offers a number of great solutions, such as the Share Dialog, which uses the native Facebook and open graph.

Resources