I am using SLComposeViewController to share the images on twitter. If i am not logged-in via iPhone settings than i want to show alert view. Please any body will suggest what to do.
Use this code:
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
//show compose view
else
UIAlertView *message = [[UIAlertView alloc] initWithTitle:#"Not available"
message:#"Twitter is not available."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[message show];
Using ACAccountStore and ACAccount you can Check weather you have login or not.
Refer the sample code here: How to post on twitter by hiding SLComposeViewController *tweetSheettweet instance
Declare in .h file
#property (strong, nonatomic) ACAccount *fbAccount;
#property (strong, nonatomic) NSString *slService;
#property (strong, nonatomic) ACAccountStore *accountStore;
Add in .m file
- (IBAction)TwittButtonPressed:(id)sender
{
if(!accountStore)
accountStore = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access to the Twitter account with the access info
[self.accountStore requestAccessToAccountsWithType:twitterAccountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
// If access granted, then get the Twitter account info
NSArray *accounts = [self.accountStore accountsWithAccountType:twitterAccountType];
if (accounts.count>0) {
self.fbAccount=[accounts lastObject];
//NSLog(#"Twitter account Details =%#",self.fbAccount);
self.fbAccount=[accounts objectAtIndex:0];
NSString *username1 = self.fbAccount.username;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:username1 forKey:#"twitUserName"];
[defaults synchronize];
//NSLog(#"USer ID %#and Username%#",[defaults valueForKey:#"twitUserID"],[defaults valueForKey:#"twitUserName"]);
self.slService = SLServiceTypeTwitter;
// NSLog(#"Show indicatoer############");
[self performSelectorOnMainThread:#selector(sharingPostData:) withObject:_slService waitUntilDone:YES];
}
else{
// NSLog(#"Access not granted");
[self performSelectorOnMainThread:#selector(throwAlertWithTitle:) withObject:#"Account not found. Please setup your account in settings" waitUntilDone:NO];
}
// [self sharingPostData:self.slService];
} else {
// NSLog(#"Access not granted");
[self performSelectorOnMainThread:#selector(throwAlertWithTitle:) withObject:#"Account not found. Please setup your account in settings" waitUntilDone:NO];
}
}];
}
-(void)throwAlertWithTitle:(NSString *)message{
//remove DSActivity if any
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:#"Notification" message:message delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
errorAlert=nil;
}
-(void)sharingPostData:(NSString *)serviceType{
if ([SLComposeViewController isAvailableForServiceType:serviceType])
{
SLComposeViewController *fvc = [SLComposeViewController composeViewControllerForServiceType:serviceType];
[fvc setInitialText:#"Share text here"];
[fvc addImage:backGroundImage.image];
[fvc addURL:[NSURL URLWithString:kAppituneslink]];
[self presentViewController:fvc animated:YES completion:nil];
}
}
Related
I am using a web service used to display details about person and profile but I don't know how to display the data from web service to labels. I have worked with login validation using web service the code is as below
-(IBAction)login:(id)sender
{
NSString *username=userName.text;
NSString *password=passWord.text;
if ([username length]!=0&&[password length]!=0)
{
NSString *link=[NSStringstringWithFormat:#"http://www.abcd.com/XXX/YYY/Login.ashx/?Username=%#&Password=%#",username,password];
NSURL *URLGet= [NSURL URLWithString:link];
NSData* data = [NSData dataWithContentsOfURL:URLGet];
NSError* error;
XYZ Return array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if ([jasonReturnArray isEqual: #"UserId 4"])
{
Connections *view2=[[Connections alloc]initWithNibName:nil bundle:nil];
view2.modalTransitionStyle= UIModalTransitionStyleFlipHorizontal;
[self presentViewController:view2 animated:NO completion:nil];
NSLog(#"%#",jasonReturnArray);
}
else
{
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"ERROR !!!!!!"message:#"Wrong Details" delegate:self cancelButtonTitle:#"Ok"otherButtonTitles: nil];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(#"%#",jasonReturnArray);
[alert show];
}
}
else
{
UIAlertView* alert=[[UIAlertView alloc] initWithTitle:#"ERROR !!!!!!"
message:#"Wrong Details" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
NSLog(#"%#",jasonReturnArray);
[alert show];
}
}
It works fine but my doubt is
how to display the data from json web service to my designed labels
Create IBOutlet of your UILabel then set text value of your label as following
for (NSString * singleStringObject in jasonReturnArray)
{
yourLabel.text=[yourLabel.text stringByAppendingString:singleStringObject];
}
in case of single string you just set text of yourLabel without any loop.
I'm having trouble coming up with a solution to the following from the Simple Login iOS quick start:
Since it is possible for a device to have more than one Twitter account attached, you will need to provide a block which can be used to determine which account to log in. Replace "[yourApp selectUserName:usernames]" below with your own code to choose from the list of usernames.
This is the code that is provided:
[authClient loginToTwitterAppWithId:#"YOUR_CONSUMER_KEY"
multipleAccountsHandler:^int(NSArray *usernames) {
// If you do not wish to authenticate with any of these usernames, return NSNotFound.
return [yourApp selectUserName:usernames];
} withCompletionBlock:^(NSError *error, FAUser *user) {
if (error != nil) {
// There was an error authenticating
} else {
// We have an authenticated Twitter user
}
}];
Would a UIActionSheet that allows the user to pick which account to use be best? How would that be done?
#import <Twitter/Twitter.h>
#import <Accounts/Accounts.h>
For login:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
// Request access from the user to use their Twitter accounts.
// [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
//NSLog(#"%#",error);
if(granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
//NSLog(#"%#",accountsArray);
twitterAccountsArray=[accountsArray mutableCopy];
if ([twitterAccountsArray count] > 0){
sheet = [[UIActionSheet alloc] initWithTitle:#"Choose an Account" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
for (ACAccount *acct in twitterAccountsArray) {
[sheet addButtonWithTitle:acct.username];
}
}
}
else{
dispatch_async(dispatch_get_main_queue(), ^{
//NSLog(#"%#",error);
if (![error.localizedDescription isEqual:[NSNull null]] &&[error.localizedDescription isEqualToString:#"No access plugin was found that supports the account type com.apple.twitter"]) {
}
else
[Utility showAlertWithString:#"We could not find any Twitter account on the device"];
});
}
}];
It will open an action sheet which will show you the list of accounts your device has.
For posting :
There must be at least one valid twitter account added on your iPhone device.
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
NSString *initialText=#"Text to be posted";
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
switch(result) {
// This means the user cancelled without sending the Tweet
case SLComposeViewControllerResultCancelled:{
}
break;
case SLComposeViewControllerResultDone:{
}
break;
}
};
[tweetSheet setInitialText:initialText];
if (initialText) {
[self presentViewController:tweetSheet animated:YES completion:nil];
}
}
I've found a nice example of how to do this with Awesome Chat's code. It uses a UIActionSheet like I was attempting to.
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (IBAction)actionTwitter:(id)sender
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
[ProgressHUD show:#"In progress..." Interaction:NO];
//---------------------------------------------------------------------------------------------------------------------------------------------
selected = 0;
//---------------------------------------------------------------------------------------------------------------------------------------------
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
//---------------------------------------------------------------------------------------------------------------------------------------------
[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
{
if (granted)
{
accounts = [account accountsWithAccountType:accountType];
//-------------------------------------------------------------------------------------------------------------------------------------
if ([accounts count] == 0)
[self performSelectorOnMainThread:#selector(showError:) withObject:#"No Twitter account was found" waitUntilDone:NO];
//-------------------------------------------------------------------------------------------------------------------------------------
if ([accounts count] == 1) [self performSelectorOnMainThread:#selector(loginTwitter) withObject:nil waitUntilDone:NO];
if ([accounts count] >= 2) [self performSelectorOnMainThread:#selector(selectTwitter) withObject:nil waitUntilDone:NO];
}
else [self performSelectorOnMainThread:#selector(showError:) withObject:#"Access to Twitter account was not granted" waitUntilDone:NO];
}];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)selectTwitter
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:#"Choose Twitter account" delegate:self cancelButtonTitle:nil
destructiveButtonTitle:nil otherButtonTitles:nil];
//---------------------------------------------------------------------------------------------------------------------------------------------
for (NSInteger i=0; i<[accounts count]; i++)
{
ACAccount *account = [accounts objectAtIndex:i];
[action addButtonWithTitle:account.username];
}
//---------------------------------------------------------------------------------------------------------------------------------------------
[action addButtonWithTitle:#"Cancel"];
action.cancelButtonIndex = accounts.count;
[action showInView:self.view];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
if (buttonIndex != actionSheet.cancelButtonIndex)
{
selected = buttonIndex;
[self loginTwitter];
}
else [ProgressHUD dismiss];
}
//-------------------------------------------------------------------------------------------------------------------------------------------------
- (void)loginTwitter
//-------------------------------------------------------------------------------------------------------------------------------------------------
{
Firebase *ref = [[Firebase alloc] initWithUrl:FIREBASE];
FirebaseSimpleLogin *authClient = [[FirebaseSimpleLogin alloc] initWithRef:ref];
//---------------------------------------------------------------------------------------------------------------------------------------------
[authClient loginToTwitterAppWithId:TWITTER_KEY multipleAccountsHandler:^int(NSArray *usernames)
{
return (int) selected;
}
withCompletionBlock:^(NSError *error, FAUser *user)
{
if (error == nil)
{
if (user != nil) [delegate didFinishLogin:ParseUserData(user.thirdPartyUserData)];
[self dismissViewControllerAnimated:YES completion:^{ [ProgressHUD dismiss]; }];
}
else
{
NSString *message = [error.userInfo valueForKey:#"NSLocalizedDescription"];
[self performSelectorOnMainThread:#selector(showError:) withObject:message waitUntilDone:NO];
}
}];
}
I am new to Xcode I need an app to send a email. Background: the destination email Id is typed in a text and by clicking the send button the message body Sample should go to the destination Email ID I tried this code in the function button clicked but it is not working when ever i try this code I get error in function can any one guide me with a step by step tutorial
mailTransfer[673:207] delegate - error(-5): timeout sending message
2014-07-05 10:54:05.393 mailTransfer[673:207] * stopping watchdog * I had added the SMTP files from google documents ... any other way to correct this code
- (IBAction)sendMessageInBack:(id)anObject
{
SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
testMsg.fromEmail = #"Yours mail ids";
testMsg.toEmail = emailField.text;
testMsg.relayHost = #"smtp.gmail.com";
testMsg.requiresAuth = YES;
testMsg.login = #"Your mail ids";
testMsg.pass = #"id password";
testMsg.subject = #"Test application ";
testMsg.wantsSecure = YES;
testMsg.delegate = self;
NSDictionary *plainPart = [NSDictionarydictionaryWithObjectsAndKeys:#"text/plain",kSKPSMTPPartContentTypeKey,#"Sample",kSKPSMTPPartMessageKey,#"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
[testMsg send];
}
-(void)messageSent:(SKPSMTPMessage *)message{
[message release];
NSLog(#"delegate - message sent");
}
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error{
[message release];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Unable to send email" delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
NSLog(#"delegate - error(%d): %#", [error code], [error localizedDescription]);
}
in SKPSMTPMessage.m
update the following line
CFDictionarySetValue(sslOptions,kCFStreamSSLLevel,kCFStreamSocketSecurityLevelTLSv1);
with
CFDictionarySetValue(sslOptions, kCFStreamSSLLevel, kCFStreamSocketSecurityLevelSSLv3);
Download SMTP framework and import SKPSMTPMessage class..
#import "SKPSMTPMessage.h"
-(void)sendEmailVideo:(NSString*)_toEmailAddress andCC:(NSString*)ccEmail
{
#try
{
// Message =[data getContentOfPanic];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
SKPSMTPMessage *emailMessage = [[SKPSMTPMessage alloc] init];
emailMessage.fromEmail=#"nikki.varsha#gmail.com";//sender email address
emailMessage.toEmail=_toEmailAddress;
//receiver email address
emailMessage.relayHost=#"smtp.gmail.com";
//emailMessage.ccEmail =ccEmail;
emailMessage.requiresAuth = YES;
emailMessage.login = #"nikki.varsha#gmail.com"; //sender email address
emailMessage.pass = #"123";
//sender email password
emailMessage.subject =#"Panic Video Message";
emailMessage.wantsSecure = YES;
emailMessage.delegate = self;
NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/plain",kSKPSMTPPartContentTypeKey,
Message,kSKPSMTPPartMessageKey,#"8bit", kSKPSMTPPartContentTransferEncodingKey,nil];
NSDictionary *vcfPart = [NSDictionary dictionaryWithObjectsAndKeys:#"text/directory;\r\n\tx-unix-mode=0644;\r\n\tname=\"Video.mov\"",kSKPSMTPPartContentTypeKey,
#"attachment;\r\n\tfilename=\"Video.mov\"",kSKPSMTPPartContentDispositionKey,[webData encodeBase64ForData],kSKPSMTPPartMessageKey,#"base64",kSKPSMTPPartContentTransferEncodingKey,nil];
emailMessage.parts=[NSArray arrayWithObjects:plainPart,vcfPart,nil];
dispatch_queue_t backgroundVideoQueue = dispatch_queue_create("com.VideoQue", 0);
dispatch_sync(backgroundVideoQueue, ^{
[emailMessage send];
});
}
#catch (NSException *exception)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"No Host" message:#"No Reciever Email Ids Available! " delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}
}
pragma mark - sendEmail delegate (SKPSMTPMessage)
-(void)messageSent:(SKPSMTPMessage *)message
{
NSLog(#"delegate - Email sent");
NSLog(#"Mesg %#",message);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Email sent." message:nil delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
}
-(void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(#"err=%#" ,message);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Unable to send email Please Check EmailId & Password"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
});
}
in SKPSMTPMessage.m file do change the following line
Remove this line
//CFDictionarySetValue(sslOptions,kCFStreamSSLLevel,kCFStreamSocketSecurityLevelTLSv1);
Add this line
CFDictionarySetValue(sslOptions, kCFStreamSSLLevel, kCFStreamSocketSecurityLevelSSLv3);
Thanks
How to post Facebook Feed on Friends wall from IOS application?
Social Framework is very uch useful for sharing and posting in
Fb,Twitter .
Here in the dialog it will ask for privacy we can set that to friends/Piblic/Me
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
// Initialize Compose View Controller
SLComposeViewController *vc = nil;
vc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
// Configure Compose View Controller
[vc setInitialText:self.captionTextField.text];
[vc addImage:self.image];
[vc setCompletionHandler:^(SLComposeViewControllerResult result){
if (result == SLComposeViewControllerResultDone) {
[[[UIAlertView alloc] initWithTitle:#"Congratulations" message:#"Your post is successfully Posted" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
}else{
}
}];
// Present Compose View Controller
[self presentViewController:vc animated:YES completion:nil];
} else {
NSString *message = #"It seems that we cannot talk to Facebook at the moment or you have not yet added your Facebook account to this device. Go to the Settings application to add your Facebook account to this device.";
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Oops" message:message delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
Via Graph API you cannot post to the friend's wall, it was deprecated.
You can use Feed Dialog to invoke the dialog and user can post the feed to any friend, here's the code:
NSMutableDictionary *params =
[NSMutableDictionary dictionaryWithObjectsAndKeys:
#"An example parameter", #"description",
#"https://developers.facebook.com/ios", #"link",
nil];
[FBWebDialogs presentFeedDialogModallyWithSession:nil
parameters:params
handler:^(FBWebDialogResult result, NSURL *resultURL, NSError *error) {}
];
Check documentation for more details and list of parameters available.
Am using SLComposeViewController for posting articles to facebook wall.When App permission is turned off for facebook in device settings,
SLComposeViewController still Works by posting article to facebook wall.Is this a SDK issue?
SLComposeViewController *facebookViewController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result)
{
[facebookViewController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
[[[UIAlertView alloc] initWithTitle:#"Facebook"
message:#"Action Cancelled"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil]
show];
[self dismissView];
}
break;
case SLComposeViewControllerResultDone:
{
[[[UIAlertView alloc] initWithTitle:#"Facebook"
message:#"Posted to Facebook successfully"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil]
show];
[self dismissView];
}
break;
}};
[facebookViewController addImage:_shareImage];
[facebookViewController setInitialText:_shareTitle];
[facebookViewController addURL:_shareLink];
[facebookViewController setCompletionHandler:completionHandler];
[self.dashboard presentViewController:facebookViewController animated:YES completion:nil];
}
I found out the answer myself.
Check the below code
if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(#"6.0"))
{
ACAccountStore *accountStore=[[ACAccountStore alloc]init];
ACAccountType * facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
// At first, we only ask for the basic read permission
NSArray * permissions = #[#"publish_stream"];
NSDictionary * dict = #{ACFacebookAppIdKey : #"facebook_appid", ACFacebookPermissionsKey : permissions, ACFacebookAudienceKey : ACFacebookAudienceEveryone};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:dict completion:^(BOOL granted, NSError *error) {
if (granted && error == nil)
{
SLComposeViewController *facebookViewController=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result)
{
[facebookViewController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
[self dismissView];
}
break;
case SLComposeViewControllerResultDone:
{
[[[UIAlertView alloc] initWithTitle:#"Facebook"
message:#"Posted to Facebook successfully"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil]
show];
[self dismissView];
}
break;
}};
[facebookViewController addImage:_shareImage];
[facebookViewController setInitialText:_shareTitle];
[facebookViewController addURL:_shareLink];
[facebookViewController setCompletionHandler:completionHandler];
[self.dashboard presentViewController:facebookViewController animated:YES completion:nil];
}
else
{
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissView];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"" message: #"App Permissions disabled in facebook settings."
delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
});
}
}];