I am trying to integrate Sharing on Facebook Login in my application but somehow i am not able to achieve this for sharing the location on the Facebook wall, here i am using the social framework.
The code i tried is as follows :
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.....");
// [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs://"]];
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Posted....");
}
break;
}};
[fbController setInitialText:#"This is My Sample Text"];
[fbController setCompletionHandler:completionHandler];
[self presentViewController:fbController animated:YES completion:nil];
}
else{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Sign in!" message:#"Please first Sign In!" delegate:nil cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alert show];
}
by this I can share the Initial text but what i need is to share is the current location.
Thanks in Advance.
u can get current location using CLLocationManger as for this import MapKit framework.
NSString *city;
CLLocationManager *locationManager;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
CLLocationCoordinate2D coordinate = [locationManager.location coordinate];
GeoCodeValue=[[NSString alloc] initWithFormat:#"%f,%f",coordinate.latitude,coordinate.longitude];
NSLog(#"userLocation::%#",GeoCodeValue);
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:locationManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
city = placemark.thoroughfare;
}];
& pass this city string as text to show current location.as
[fbController setInitialText:#"Current Location is %#",city"];
Related
I am new to the MapView topic.Now I am working on the map view.I am getting the san Francisco location longitude and latitude values.I am testing in the simulator.It is not showing the current location longitude and latitude values.
With the help of this tutorial http://www.creativeworkline.com/2014/12/core-location-manager-ios-8-fetching-location-background/ I am developing the app.
In AppDelegate file I wrote the following code like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIAlertView * alert;
//We have to make sure that the Background App Refresh is enable for the Location updates to work in the background.
if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied){
alert = [[UIAlertView alloc]initWithTitle:#""
message:#"The app doesn't work without the Background App Refresh enabled. To turn it on, go to Settings > General > Background App Refresh"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
}else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted){
alert = [[UIAlertView alloc]initWithTitle:#""
message:#"The functions of this app are limited because the Background App Refresh is disable."
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil, nil];
[alert show];
} else{
self.locationTracker = [[LocationTracker alloc]init];
[self.locationTracker startLocationTracking];
//Send the best location to server every 60 seconds
//You may adjust the time interval depends on the need of your app.
NSTimeInterval time = 60.0;
self.locationUpdateTimer =
[NSTimer scheduledTimerWithTimeInterval:time
target:self
selector:#selector(updateLocation)
userInfo:nil
repeats:YES];
}
return YES;
}
I have imported Location Tracker class in my ViewController
and I wrote the following code to get the location name and addresss
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
[geoCoder reverseGeocodeLocation:self.appDelgate.locationTracker.myLastLocation_ completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:#"%# %#",placemark.subLocality,placemark.subAdministrativeArea];
[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:#"address"];
}
}];
Now my problem is that it is not going inside block.So that I am getting the "returnAddress" as (null).
I wrote like this even though it is not coming
- (void)updateLocationToServer {
NSLog(#"updateLocationToServer");
// Find the best location from the array based on accuracy
NSMutableDictionary * myBestLocation = [[NSMutableDictionary alloc]init];
for(int i=0;i<self.shareModel.myLocationArray.count;i++){
NSMutableDictionary * currentLocation = [self.shareModel.myLocationArray objectAtIndex:i];
if(i==0)
myBestLocation = currentLocation;
else{
if([[currentLocation objectForKey:ACCURACY]floatValue]<=[[myBestLocation objectForKey:ACCURACY]floatValue]){
myBestLocation = currentLocation;
}
}
}
NSLog(#"My Best location:%#",myBestLocation);
NSLog(#"latitude %#",[myBestLocation valueForKey:#"latitude"]);
NSLog(#"longitude %#",[myBestLocation valueForKey:#"longitude"]);
self.DICT=[NSDictionary dictionaryWithDictionary:myBestLocation];
//If the array is 0, get the last location
//Sometimes due to network issue or unknown reason, you could not get the location during that period, the best you can do is sending the last known location to the server
if(self.shareModel.myLocationArray.count==0)
{
NSLog(#"Unable to get location, use the last known location");
self.myLocation=self.myLastLocation;
self.myLocationAccuracy=self.myLastLocationAccuracy;
}else{
CLLocationCoordinate2D theBestLocation;
theBestLocation.latitude =[[myBestLocation objectForKey:LATITUDE]floatValue];
theBestLocation.longitude =[[myBestLocation objectForKey:LONGITUDE]floatValue];
self.myLocation=theBestLocation;
self.myLocationAccuracy =[[myBestLocation objectForKey:ACCURACY]floatValue];
}
NSLog(#"Send to Server: Latitude(%f) Longitude(%f) Accuracy(%f)",self.myLocation.latitude, self.myLocation.longitude,self.myLocationAccuracy);
//TODO: Your code to send the self.myLocation and self.myLocationAccuracy to your server
//After sending the location to the server successful, remember to clear the current array with the following code. It is to make sure that you clear up old location in the array and add the new locations from locationManager
[self.shareModel.myLocationArray removeAllObjects];
self.shareModel.myLocationArray = nil;
self.shareModel.myLocationArray = [[NSMutableArray alloc]init];
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
self.locationActual = [[CLLocation alloc]initWithLatitude:[[myBestLocation objectForKey:LATITUDE]floatValue] longitude:[[myBestLocation objectForKey:LONGITUDE]floatValue]];
//CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
// __block NSString *returnAddress = nil;
CLLocation *locloc = [[CLLocation alloc] initWithLatitude:[[myBestLocation objectForKey:LATITUDE]floatValue] longitude:[[myBestLocation objectForKey:LONGITUDE]floatValue]];
[geoCoder reverseGeocodeLocation:locloc completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:#"%# %#",placemark.subLocality,placemark.subAdministrativeArea];
//[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:#"address"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"" message:returnAddress delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
}];
}
What mistake i have done here.
Can anyone please help to clear this confusion.
Thanks in Advance.
Are you sure you are using the return address after completion block? I have used the above code and its working fine.
Here you can download the sample code
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
__block NSString *returnAddress = nil;
CLLocation *locloc = [[CLLocation alloc] initWithLatitude:12.92243 longitude:80.23893];
[geoCoder reverseGeocodeLocation:locloc completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks lastObject];
if (placemark)
{
returnAddress = [NSString stringWithFormat:#"%# %#",placemark.subLocality,placemark.subAdministrativeArea];
//[[NSUserDefaults standardUserDefaults] setObject:[NSString stringWithString:returnAddress] forKey:#"address"];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"" message:returnAddress delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alertView show];
}
}];
//If you try to access retunAddress here you will get nil.
OUTPUT
Go to iOS Simulator -> Debug -> Location -> Custom location... And enter your long/lat coordinates.
In the simulator there is no way to get the actual GPS based data so you need to simulate is with your lat-long which you can set by going
Debug -> Location -> Custom Location
and set your values there.
Probably you are simulating the location in the simulator. In your xCode's console panel there is an arrow button. Click that button and select "Don't simulate location". For reference, see the image.
If that doesn't resolve the problem then run the application. Go into the Debug menu of the simulator and choose the "Custom location" option as shown in the image.
I am using SLComposeViewController for sharing in Twitter and Facebook in my app. It is working fine for Twitter but for Facebook, SLComposeViewController closes automatically on selecting location. This is an iOS 8 issue. Working fine on iOS7.
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
self.fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
// [fbSheet dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Posted....");
if([NetworkManager SharedInstance].isInternetReachable){
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Success"
message:#"Feeds shared successfully."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
break;
}};
[self.fbSheet setCompletionHandler:completionHandler];
NSString *fbString= self.titleString;
[self.fbSheet setInitialText:fbString];
[self.fbSheet addURL:[NSURL URLWithString:self.urlString]];
[self presentViewController:self.fbSheet animated:YES completion:nil];
}
The control is automatically going into completion handler block with result as cancelled. I have gone through some posts suggesting it is 64 bit architecture problem. Please help me with this if anyone is facing the same issue.
- (IBAction)facebookPost:(id)sender {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
self.fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self.fbSheet setInitialText:#"Social Framework test"];
[self.fbSheet addImage:[UIImage imageNamed:#"imagename.png"]];
[self.fbSheet addURL:[NSURL URLWithString:#"URL_NAME"]];
[self.fbSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
switch (result) {
case SLComposeViewControllerResultCancelled:
NSLog(#"Post Canceled");
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Post Sucessful");
NSLog(#"Posted....");
if([NetworkManager SharedInstance].isInternetReachable){
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Success"
message:#"Feeds shared successfully."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
break;
default:
break;
}
}];
[self presentViewController:self.fbSheet animated:YES completion:nil];
}
}
I want to open the Facebook and twitter app from my application. At the same time I have a text which has to be auto populated in the Facebook share box. How do I achieve this? I am able to open the app now. But how do I open the share page and pass the text to be populated.
The same with Twitter.
You can easily post to facebook with image and text. For this you don't have to exit your app. Try this:
- (void) postFacebook {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbSheet setInitialText:#"Your Text here"];
[fbSheet addImage:[UIImage imageNamed:#"Icon-144.png"]];
[fbSheet addURL:[NSURL URLWithString:#"http://asdf.com"]];
[[CCDirector sharedDirector] presentViewController:fbSheet animated:YES completion:nil];
}
else {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:#"Sorry!!!"
message:#"You can't share on facebook right now, make sure your device has an internet connection and you have at least one facebook account setup in your device."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
For tweeter do this:
- (void) callTwitter {
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:#"Your text here"];
[tweetSheet addImage:[UIImage imageNamed:#"Icon-144.png"]];
[tweetSheet addURL:[NSURL URLWithString:#"http://asdf.com"]];
[[CCDirector sharedDirector] presentViewController:tweetSheet animated:YES completion:nil];
}
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 in your device."
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alertView show];
}
}
Don't forget to add the SOCIAL.FRAMEWORK by going to
BuildPhases -> LinkBinaryWithLibraries
Also in the ViewController don't forget to import at the top #import <Social/Social.h>
EDIT:
Well I found this in SO:
NSString *myMessage = [NSString stringWithFormat:#"%#",
[self.txtAdreca.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *post = [NSString stringWithFormat:#"fb://publish/profile/me?text=%#",urlString];
BOOL canOpenURL = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:post]];
if (canOpenURL) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:post]];
}
For tweeter:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"twitter://status?id=12345"]];
Hope this helps.. :)
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];
}
}
In my project i am having 5 levels. In First and second level i can able to move and shoot at a time. When completing 2nd level i want to share it with Facebook. After facebook share i cant able to move and shoot at a time. Only one process is working(Either shoot or move).
What i want to do for solve this problem.
My coding is here:
{
UIViewController*v=[[UIViewController alloc]init];
[[[CCDirector sharedDirector]openGLView]addSubview:v.view];
SLComposeViewController*mySLComposerSheet;
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook])
{
NSString *strg=[NSString stringWithFormat:#" "];
NSString *bodyStr=[NSString stringWithFormat:#"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"!\n;
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook]; //Tell him with what social plattform to use it, e.g. facebook or twitter
[mySLComposerSheet setInitialText:bodyStr]; //the message you want to post
[mySLComposerSheet addImage:[UIImage imageNamed:#"my.jpg"]]; //an image you could post
NSURL *url = [NSURL URLWithString:#"https:example.com"];
[mySLComposerSheet addURL:url];
[v presentViewController:mySLComposerSheet animated:YES completion:nil];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
NSString *output,*head;
switch (result) {
case SLComposeViewControllerResultCancelled:
output = #"FREE levels NOT unlocked";
head=#"Facebook Share Unsuccessfull";
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:head message:output delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert1 show];
[self reload];
break;
case SLComposeViewControllerResultDone:
output = #"Extra FREE levels successfully unlocked";
head=#"Successfull";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:head message:output delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
alert.tag=22;
break;
default:
break;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Facebook" message:output delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
}];
}
You are presenting the ViewController mySLComposerSheet
[v presentViewController:mySLComposerSheet animated:YES completion:nil];
but there is no dismiss... statement in your completionHandler
[self dismissViewControllerAnimated:YES completion:nil];
see the post https://stackoverflow.com/a/12651085/2787026