iOS6 social framework bring up No Account Alert - ios

Similar to iOS 6 Social framework not going to settings or no alert but i am trying to use SLRequest:
I am trying to bring up the alert "No Facebook Account" when the user has not logged in to Facebook in the Settings. I have found that the alert appear AFTER you present the SLComposeViewController, rather than inside the if statement.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
//setup controller and callback code
[self presentViewController:controller animated:YES completion:nil];
}
However, I am trying to use SLRequest and i dont want to present the SLComposeViewController, but instead, after checking the accounts, popup the alert. My code is here:
- (void)postImageFB
{
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
NSLog(#"can post");
} else {
NSLog(#"cant post");
}
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
NSLog(#"accounts: %i", [accountsArray count]);
// Is it possible to popup the alert here if accounts = 0?
NSDictionary *options = #{ ACFacebookAppIdKey: #"123456789", ACFacebookPermissionsKey: #[#"publish_stream"], ACFacebookAudienceKey: ACFacebookAudienceFriends };
[accountStore requestAccessToAccountsWithType:accountType options:options completion:^(BOOL granted, NSError *error) {
if(granted) {
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
if ([accountsArray count] > 0) {
ACAccount *facebookAccount = [accountsArray objectAtIndex:0];
NSDictionary *parameters = #{#"message": #"testing"};
SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:[NSURL URLWithString:#"https://graph.facebook.com/me/photos"]
parameters:parameters];
[facebookRequest addMultipartData: [self getImageDataFromPlistWithFilename:#"image1.png"]
withName:#"source"
type:#"multipart/form-data"
filename:#"TestImage"];
facebookRequest.account = facebookAccount;
[facebookRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (error) {
NSLog(#"%#",error.description);
} else {
NSLog(#"responedata:%#", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}
}];
}
}
}];
}

Ok, I have discovered that isAvailableForServiceType always returns true?
so started testing on the device.
I figured that using an invisible SLComposeViewController to bring up the Alert seems to be a suitable workaround:
if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
controller.view.hidden = YES;
[self presentViewController:controller animated:NO completion:nil];
} else {
// Put posting code here: SLComposeViewController or SLRequest
}

Related

How To Edit Permission for facebook in setting

- (void)viewDidLoad {
[super viewDidLoad];
if (NO == [SLComposeViewController isAvailableForServiceType: SLServiceTypeFacebook]) {
[self showAlert:#"There are no Facebook accounts configured. Please add or create a Facebook account in Settings."];
return;
}
[self getMyDetails];
// Do any additional setup after loading the view from its nib.
}
- (void) getMyDetails {
if (! accountStore) {
accountStore = [[ACAccountStore alloc] init];
}
if (! facebookAccountType) {
facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
}
NSDictionary *options = #{ ACFacebookAppIdKey: [NSString stringWithFormat:#"1725978560966525"] };
[accountStore requestAccessToAccountsWithType: facebookAccountType
options: options
completion: ^(BOOL granted, NSError *error) {
if (granted)
{
NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
NSURL *url = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET
URL:url
parameters:nil];
request.account = facebookAccount;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableContainers
error:nil];
NSLog(#"id: %#", responseDictionary[#"id"]);
NSLog(#"first_name: %#", responseDictionary[#"first_name"]);
NSLog(#"last_name: %#", responseDictionary[#"last_name"]);
NSLog(#"gender: %#", responseDictionary[#"gender"]);
NSLog(#"city: %#", responseDictionary[#"location"][#"name"]);
}];
} else {
[self showAlert:#"Facebook access for this app has been denied. Please edit Facebook permissions in Settings."];
}
}];
}
- (void) showAlert:(NSString*) msg {
dispatch_async(dispatch_get_main_queue(), ^(void) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"WARNING"
message:msg
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
});
}
i tried this for getting personal information of Facebook (Logged-in User), But it require permission, as i don't know where To add permissions, i have checked in setting, tried all thing in Simulator But Still Not getting.

How to Facebook login without opening Facebook app?

I recently installed the app "mapstr" and when you choose Facebook login, you don't go through the Facebbok app, you just accept the permissions through a kind of alertView :
It's written in French and it says : "mapstr" wants to access your public profile and your friends list.
When I tap OK, I am just logged with Facebook : no app switching !
How do they do that ?
(I am developping in Swift 2.0)
I'm Mapstr founder ;)
It's a loginBehavior option in Facebook LoginManager (in iOS SDK) which is the "FBSDKLoginBehaviorSystemAccount" (you also have the app option and the web option)
If a user has configure his face boo account on its iPhone, the SDK will use it, if not it will try the app, and then the web.
The only drawback is that if the system account exists but is misconfigured it failed...
Sebastien
Its using the facebook Credentials saved in your iPhone settings to login through facebook. You will need to use accounts framework for that. Below is the sample code for the same.
-(void)facebook
{
ACAccountStore *accountStore;
accountStore = [[ACAccountStore alloc]init];
ACAccountType *FBaccountType= [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:kFACEBOOK_APPID,ACFacebookAppIdKey,#[#"email"],ACFacebookPermissionsKey, nil];
[accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {
if (granted)
{
[self afterPermissionGranted:accountStore accountType:FBaccountType];
}
else
{
//Fail gracefully...
NSLog(#"error getting permission %#",e);
dispatch_async(dispatch_get_main_queue(), ^{
[self openSessionWithAllowLoginUI:YES];
});
}
}];
}
-(void)afterPermissionGranted:(ACAccountStore *)accountStore accountType:(ACAccountType *)FBaccountType{
NSArray *accounts = [accountStore accountsWithAccountType:FBaccountType];
//it will always be the last object with single sign on
if ([accounts count] == 0) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"" message:#"No Other Facebook Account Found" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
else{
ACAccount *facebookAccount;
facebookAccount = [accounts lastObject];
ACAccountCredential *facebookCredential = [facebookAccount credential];
NSString *accessToken = [facebookCredential oauthToken];
NSLog(#"FAT: %#", accessToken);
NSURL *requestURL = [NSURL URLWithString:#"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:requestURL parameters:nil];
request.account = facebookAccount;
[request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
if(!error)
{
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary *errorPart = [list objectForKey:#"error"];
NSString *errorsubCode =[NSString stringWithFormat:#"%#",[errorPart valueForKey:#"error_subcode"]];
if (![errorsubCode isEqualToString:#"(null)"]) {
dispatch_async(dispatch_get_main_queue(), ^{
[self openSessionWithAllowLoginUI:YES];
});
}
else{
NSString *globalmailID = [NSString stringWithFormat:#"%#",[list objectForKey:#"email"]];
NSLog(#"global mail : %#",globalmailID);
NSString *fbname = [NSString stringWithFormat:#"%#",[list objectForKey:#"name"]];
NSLog(#"fname %#",fbname);
ACAccountCredential *fbCredential = [facebookAccount credential];
NSString *accessToken = [fbCredential oauthToken];
[self saveDataFromFacebook:error user:list accessToken:(NSString *)accessToken];
}
}
else
{
//handle error gracefully
NSLog(#"error from get%#",error);
//attempt to revalidate credentials
}
dispatch_async(dispatch_get_main_queue(), ^{
[self endProgressBar];
});
}];
}
}
Please remove the extra code. Feel free to ask any queries.

Making Post Request to twitter with IOS

I am new to the concept of POSTing to twitter. I've looked here in order to understand how GET something from twitter. But I can't find any example about how to POST something to twitter.
how do I tell twitter that userXYZ is posting a tweet? How about the text body of the tweet itself.
Again, I'm very new to the concept.
You can easily find those in API Documentation. I think you are looking for this.
Also there are some question already asked in SO. You can also check this.
Using Twitter SDK import #import<Twitter/Twitter.h> in your .h or .m file
if ([TWTweetComposeViewController canSendTweet])
{
NSURL* aURL = [NSURL URLWithString:#"your image Url"];
NSData* data = [[NSData alloc] initWithContentsOfURL:aURL];
UIImage *img1 = [UIImage imageWithData:data];
// Initialize Tweet Compose View Controller
TWTweetComposeViewController *vc = [[TWTweetComposeViewController alloc] init];
// Settin The Initial Text
[vc setInitialText:#"Setting The Initial Text"];
[vc addImage:img1];
// Adding a URL
strWEBTwitter = #"Pass Your URL here"
[vc addURL:[NSURL URLWithString:strWEBTwitter]];
// Setting a Completing Handler
[vc setCompletionHandler:^(TWTweetComposeViewControllerResult result)
{
[self dismissViewControllerAnimated:YES completion:nil];
NSLog(#"Result : %d", result);
if (result == TWTweetComposeViewControllerResultDone)
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Message" message:#"Thanks for sharing." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil]; [alertView show];
}
else if (result == TWTweetComposeViewControllerResultCancelled)
{
NSLog(#"Cancle")
}
}];
// Display Tweet Compose View Controller Modally
[self presentViewController:vc animated:YES completion:nil];
} else
{
// Show Alert View When The Application Cannot Send Tweets
NSString *message = #"There are no Twitter accounts configured. You can add or create a Twitter account in Settings.";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"No Twitter Accounts" message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:#"Cancel",nil];
[alertView show];
}
Thanks Rashad. This is what I am using now. It works.
- (void)testPost {
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *accounts = [accountStore accountsWithAccountType:accountType];
if (accounts.count)
{
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:#"check Check CHECK" forKey:#"status"];
NSString *retweetString = [NSString stringWithFormat:#"https://api.twitter.com/1.1/statuses/update.json"];
NSURL *retweetURL = [NSURL URLWithString:retweetString];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:retweetURL parameters:dict];
request.account = [accounts objectAtIndex:0];
[request performRequestWithHandler:^(NSData *responseData1, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (responseData1)
{
NSError *error1 = nil;
id response = [NSJSONSerialization JSONObjectWithData:responseData1 options:NSJSONReadingMutableLeaves error:&error1];
NSLog(#"%#", response);
}
}];
}
}
}];
}

How to use SLRequest for change via iOS to via <AppName>

I goes through many link and tutorials but not working for me.
Can anybody explain me How to use SLRequest for change via iOS to via MyAppName ?? step by step or give me some links which gives this solution in step by step.
EDIT: I have tried. Below is my code may be help.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *facebookAccountType = [accountStore
accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// Specify App ID and permissions
NSDictionary *options = #{
ACFacebookAppIdKey: #"012345678912345",
ACFacebookPermissionsKey: #[#"publish_stream", #"publish_actions"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *e) {
if (granted) {
NSArray *accounts = [accountStore
accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
}
else
{
// Handle Failure
}
}];
NSDictionary *parameters = #{#"message": #"My first iOS 6 Facebook posting "};
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/feed"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
feedRequest.account = self->facebookAccount;
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
// Handle response
}];
}
First of all set you facebook setting using Facebook developer account setting
then reset your simular or delete app from device.
Add and import this frameworks
#import <Accounts/Accounts.h>
#import <Social/Social.h>
#import <AdSupport/AdSupport.h>
#import <FacebookSDK/FacebookSDK.h>
use this code for further process
in yourclass.h file
#property (nonatomic, strong)ACAccountStore *accountStore;
in yourclass.m file
#synthesize accountStore;
- (IBAction)facebookButtonTouch:(id)sender {
if (self.accountStore == nil)
self.accountStore = [[ACAccountStore alloc] init];
__block ACAccount *facebookAccount = nil;
ACAccountType *facebookAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSArray * permissions = #[#"publish_stream", #"publish_actions",#"email"];
NSMutableDictionary *options = [[NSMutableDictionary alloc] initWithObjectsAndKeys:#"your app id", ACFacebookAppIdKey, permissions, ACFacebookPermissionsKey, ACFacebookAudienceOnlyMe, ACFacebookAudienceKey, nil];
[self.accountStore requestAccessToAccountsWithType:facebookAccountType
options:options completion:^(BOOL granted, NSError *error)
{
if (granted)
{
NSArray *readPermissions = #[#"read_stream", #"read_friendlists"];
[options setObject:readPermissions forKey: ACFacebookPermissionsKey];
[self.accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if(granted && error == nil) {
/**
* We now should have some read permission
* Now we may ask for write permissions or
* do something else.
**/
} else {
NSLog(#"error is: %#",[error description]);
}
}];
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
}
else {
NSLog(#"error.localizedDescription======= %#", error.localizedDescription);
}
}];
NSArray *accounts = [self.accountStore accountsWithAccountType:facebookAccountType];
facebookAccount = [accounts lastObject];
//-------------- start code for posting message on wall via SLRequest------------------
NSDictionary *parameters = #{#"message": #"Hi Friends i m ....."};
NSURL *feedURL = [NSURL URLWithString:#"https://graph.facebook.com/me/feed"];
SLRequest *feedRequest = [SLRequest
requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodPOST
URL:feedURL
parameters:parameters];
feedRequest.account = facebookAccount;
[feedRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
NSLog(#"responseData %#",[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}];
//-------------- end code for posting message on wall via SLRequest------------------
}
i hope it will help you. Thanks

Access to Twitter Account(s)

I am using the following code to post to Twitter using SLRequest and ACAccounts in iOS 6. The issue is that when there is more than one account, I have no control over which one gets access granted. Suggestions for letting the user pick the account they want to post from?
-(void)twitteraccess {
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:
ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType options:nil
completion:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
NSArray *arrayOfAccounts = [account
accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
ACAccount *twitterAccount = [arrayOfAccounts lastObject];
NSDictionary *message = #{#"status": #"Message"};
NSURL *requestURL = [NSURL
URLWithString:#"http://api.twitter.com/1/statuses/update.json"];
SLRequest *postRequest = [SLRequest
requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:requestURL parameters:message];
postRequest.account = twitterAccount;
[postRequest performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse, NSError *error)
{
NSLog(#"Twitter HTTP response: %i", [urlResponse
statusCode]);
}];
}
}
else {
if(error.code == 6){
[self performSelectorOnMainThread:#selector(alertmessagetwitter)
withObject:nil
waitUntilDone:YES]; }
}
}];
}

Resources