Social sharing url of current webview iPhone. ( Xcode) - iphone

so I have my app setup with share action button that shares the current URL to Twitter or Facebook however when I click the Facebook button it shows a Twiiter share sheet. The (Tiwtter option works fine)
After I click the FACEBOOK option, when testing on iPhone the standard twiiter share sheet appears.
- (IBAction)social:(id)sender {
UIActionSheet *share = [[UIActionSheet alloc] initWithTitle:#"Pass on the news!" delegate:self cancelButtonTitle:#"OK" destructiveButtonTitle:nil otherButtonTitles:#"Post to Twitter", #"Post to Facebook", nil];
//You must show the action sheet for the user to see it.
[share showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//Each button title we gave to our action sheet is given a tag starting with 0.
if (actionSheet.tag == 0) {
//Check Twitter accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
SLComposeViewController *tweetSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet addURL:self.newsItemWebView.request.URL]; //This is setting the initial text for our share card.
[tweetSheet setInitialText:#"Check out this article I found using the 'Pass' iPhone app:, "];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:tweetSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't send a tweet right now, make sure you have at least one Twitter account setup and your device is using iOS6 or above!." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
} else if (actionSheet.tag == 1) {
//Check Facebook accessibility and at least one account is setup.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *facebookSheet =[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[facebookSheet addURL:self.newsItemWebView.request.URL];
//This is setting the initial text for our share card.
[facebookSheet setInitialText:#"Check out this article I found using the 'Pass' iPhone app:"];
//Brings up the little share card with the test we have pre defind.
[self presentViewController:facebookSheet animated:YES completion:nil];
} else {
//This alreat tells the user that they can't use built in socal interegration and why they can't.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"You can't post a Facebook post right now, make sure you have at least one Facebook account setup and your device is using iOS6 or above!." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
}
}

You are using actionSheet.tag which will be the same for the entiere actionsheet.
If you want to know which button is pressed you should use buttonIndex in your if statements.
Even better is to use UIActivityViewController.
NSArray *items = #[self.newsItemWebView.request.URL];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
activityViewController.completionHandler = ^(NSString *activityType, BOOL completed) {
// do any thing you want when the user finishes the share activity.
// Like present a message that that activity has completed or not.
};
[self presentViewController:activityViewController animated:YES completion:nil];

Related

how to display tweet after it was sent?

I'm working on a twitter app and i want to immediately display tweet after sending. I tried to use reloadData, but it didn't work. Can anyone help me? Thank you!
Here's my code:
- (IBAction)postNewTweet:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#""];
[self presentViewController:tweetSheet animated:YES completion:nil];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tweetTableView reloadData];
});
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry"
message:#"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
Blaze, you are reloading the table view right after presenting the compose sheet, so it reload the table right there - and you are passing the completion block as nil, so nothing will happen when user finishes tweeting.
Also, you need to be sure that you are reloading not only the table, but the array or any other object that is the source of tweetTableView.
For example:
When calling the tweet compose sheet:
[self presentViewController:tweetSheet animated:YES completion:^(SLComposeViewControllerResult result)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self reloadTweets];
});
}];
And then, you reload the tweets source and only then the table
-(void)reloadTweets
{
tweetsArray = .... //load the tweets from the internet, and only then reload the table
[self.tweetTableView reloadData];
}

Prevent AFNetworking Reachability Status Getting Called at First Launch for iOS

I just want to know how to prevent my alert view from appearing everytime I open my application that's already connected to the internet. I'm using ARC if that helps.
This is the code I have in my didFinishLaunchingWithOptions method inside my AppDelegate:
__weak id myself = self; // to silence warning for retain cycle
_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:#"http://apple.com"]];
[_httpClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusNotReachable:
{
// Not reachable
NSLog(#"Not connected to the internet");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Not connected to the internet" delegate:myself cancelButtonTitle:nil otherButtonTitles:#"Dismiss", nil];
[alert show];
break;
}
case AFNetworkReachabilityStatusReachableViaWiFi:
{
NSLog(#"Connected to the internet via WiFi");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Connected to the internet via WiFi" delegate:myself cancelButtonTitle:nil otherButtonTitles:#"Dismiss", nil];
[alert show];
break;
}
case AFNetworkReachabilityStatusReachableViaWWAN:
{
NSLog(#"Connected to the internet via WWAN");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Connected to the internet" delegate:myself cancelButtonTitle:nil otherButtonTitles:#"Dismiss", nil];
[alert show];
break;
}
default:
break;
}
}];
Echoing #D80Buckeye: just don't pop an alert for reachability. It's completely unnecessary, and doesn't add anything to the user experience (it's not like the user can do anything to fix a lack of reachability like that). If anything, you could show a non-modal indication of network reachability.
How about creating a global flag
static BOOL g_FirstTime = YES;
and check it before displaying alert view
if (g_FirstTime) {
g_FirstTime = NO;
break;
}
NSLog(#"Connected to the internet via WiFi");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:#"Connected to the internet via WiFi" delegate:myself cancelButtonTitle:nil otherButtonTitles:#"Dismiss", nil];
[alert show];
break;
Without knowing the scope of your project it's hard to make a solid suggestion. My whole-hearted suggestion is that you do NOT want to do a pop-up every time the end user bounces back on the network. If you have code implemented that is reliably detecting a refreshed network connection, if I were you, I would put some sort of subtle (but obvious) on-screen indicator like a small icon/image or maybe even a UILabel that says "connected" or "disconnected" or whatever verbiage works for you. As others have suggested I would completely steer clear of instantiating a pop-up of any sort for that alert.
Regarding the code itself - get very familiar with View Controller Programming and iOS App Programming Guide. Between those two documents you can properly map out how to detect and react to when your app comes in and out of the background state, when it launches, when it becomes active, when your views appear/load, etc. Combine those methods & calls with some global state-driven variables and you should be well on your way.
Bottom line is you want to get familiar with those app state documents as well as [[NSNotificationCenter defaultCenter] addObserver:<#(id)#> selector:<#(SEL)#> name:<#(NSString *)#> object:<#(id)#>]
Let me know if that doesn't make sense.

FaceBook Share image set user edit disable

my app that share image on Facebook. that work fine but i want to disable user inter face with post. So user can`t modify My initialText is this possible ?
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *fbComposer = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbComposer setInitialText:#"Yes U Are?"];
if ([fbComposer addImage:[UIImage imageWithData:[Array objectAtIndex:i]]]) {
}
[self presentViewController:fbComposer animated:YES completion:nil];
}else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Facebook Error"
message:#"You may not have set up facebook service on your device or\n You may not connected to internent.\nPlease check ..."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alertView show];
}
}
hear is the image of Facebook share i want to only click on post btn And Cancle btn .So user can only Post.

Add Text in TweetSheet when an UIAlert is pressed

Can anyone tell me how i can add text in TweetSheet when an UIAlert Title button is pressed?
Currently i am using this code to achieve it, but failed so far.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
if ([title isEqualToString:#"Add custom text"]) {
UIAlertView *alert= [[UIAlertView alloc] initWithTitle:#"Add a template" message:nil delegate:self cancelButtonTitle:#"Hi it was nice meeting you" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
if ([title isEqualToString:#"Hi it was nice meeting you"]) {
[tweetSheet setInitialText:#"hey it was nice meeting you"];
}
}
Can anyone tell me what's wrong here?
You're not presenting your TWTweetComposeViewController you can do this simply by adding :
[self presentModalViewController:tweetSheet animated:YES];
I guess this code is only for a simple test tweet.
For a serious project you should always:
Check if the user can send tweets by calling +(BOOL)canSendTweet before trying to show the TWTweetComposeViewController
If not sure about the properties you'll be setting (text, images, urls), you need to check if adding them was successful : -(BOOL)addImage:(UIImage *)image, -(BOOL)addURL:(NSURL *)url and -(BOOL)setInitialText:(NSString *)text all return YES if it was successful and NO if not.
You can set a completion handler to perform different actions depending on what the user decided (send tweet/cancel)
The WWDC session about Twitter integration from 2011 is very simple and detailed, you should watch it and read the documentation if you need more information.

TWTweetComposeViewController freezes my app completely on alloc-init

I've got an iPhone app that uses the Twitter framework in iOS 5. I've noticed that when I try to tweet something, the app completely freezes on the "alloc init" line of code.
Here's my code. Anybody see something wrong with it?
-(void)tweet {
NSLog(#"Tweeting");
[self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
if (stringToShare.length < 140) {
TWTweetComposeViewController *tweetController = [[TWTweetComposeViewController alloc] init];
[tweetController setInitialText:stringToShare];
[self presentModalViewController:tweetController animated:YES];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Easy there, Hemingway." message:[NSString stringWithFormat:#"Your note has %d characters, which exceeds Twitter's limit of 140.", stringToShare.length]delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
}
UPDATE: This method is called from a modal view controller. Could that be the issue?
UPDATE 2: Here's the line of code that's used to detect if the user can tweet:
if ([TWTweetComposeViewController canSendTweet]) {
[availableActions addObject:#"Tweet"];
}
I know that the issue is not constant --- it seems to occur on my phone, but not my co-founder's.