Prompt login alert with Twitter framework in iOS5? - objective-c

As you all may know, since iOS5 there is a native Twitter framework which make it easy to post tweets from your app.
Is there a way to prompt an alert that forwards the user to the settings app and ask for username and password?
I know that i could solve the problem with the following code:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"prefs:root=TWITTER"]];
But thats undocumented code..
Thanks in advance
Regards Billy
(My first post on SO)

In iOS5.1, we should use TWTweetComposeViewController to show the dialog since apple rejects apps using prefs:root=TWITTER.
But, I didn't like showing the tweet screen and keyboard
so I figured out the way to hide them, but show the pop up screen.
UPDATE:
Apple approved my app using this trick.
TWTweetComposeViewController *viewController = [[TWTweetComposeViewController alloc] init];
//hide the tweet screen
viewController.view.hidden = YES;
//fire tweetComposeView to show "No Twitter Accounts" alert view on iOS5.1
viewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
if (result == TWTweetComposeViewControllerResultCancelled) {
[self dismissModalViewControllerAnimated:NO];
}
};
[self presentModalViewController:viewController animated:NO];
//hide the keyboard
[viewController.view endEditing:YES];
//this approach doesn't work since you can't jump to settings
// [self dismissModalViewControllerAnimated:NO];

You don't need to implement this, if you set up your Twitter integration to make a post on Twitter and iOS detects that there is no Twitter account set up it will do this automatically for you!
This is a screenshot of one of my apps running on my iPhone 4S on iOS 5.1
The removal of Preferences links is in reference to custom actions by the developer, as in linking to your own preferences menu. This does not apply because not only is Twitter a built in function of iOS 5 but the UIAlertView that pops up to notify you isn't handled by the developer, it is an automatic function of iOS.

Here i found the way :
Display custom alert if no twitter account has been setup on your device settings:
if (![TWTweetComposeViewController canSendTweet]) {
UIAlertView *alertViewTwitter = [[[UIAlertView alloc]
initWithTitle:#"No Twitter Accounts"
message:#"There are no Twitter accounts configured. You can add or create a Twitter account in Settings."
delegate:self
cancelButtonTitle:#"Settings"
otherButtonTitles:#"Cancel",nil] autorelease];
[alertViewTwitter show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==0) {
TWTweetComposeViewController *ctrl = [[TWTweetComposeViewController alloc] init];
if ([ctrl respondsToSelector:#selector(alertView:clickedButtonAtIndex:)]) {
[(id <UIAlertViewDelegate>)ctrl alertView:alertView
clickedButtonAtIndex:0];
}
[ctrl release];
}
}
Hope this will make sense :)

It's not possible, although it should automatically ask the user to login, if the user isn't logged in already.
As of iOS 5.1 that feature has been removed, as seen here

Related

AddressBook privacy settings not enforced

I'm doing some working getting an app to line up with the new privacy settings in iOS 8. I've completed the requirements satisfactorily for camera access and now I'm taking a look at how this app access the address book. I'm new to working with address book APIs so these questions may have obvious answers.
As with camera access, I was thinking that access to contacts would behave similarly with respect to the status of the privacy settings granted to the app. The thing is, so far, no matter what the value of ABAuthorizationStatus is - kABAuthorizationStatusNotDetermined or kABAuthorizationStatusDenied - access is always allowed.
Also, when the status is kABAuthorizationStatusNotDetermined, the 'Okay/Don't Allow' dialog is never displayed to the user (I've erased the phone and resinstalled the app to confirm). Furthermore, the app never shows up under the privacy settings. I assume this is because the status is always kABAuthorizationStatusNotDetermined.
The code to initiate access to contacts is below. The controller is shown (read only) and contacts' information can be obtained. Delegate code not shown.
// Debug - Value is always denied or not determined.
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
// Existing code since iOS 7 - always works despite status.
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
It's great that everything seems to be working but I'd like what, if anything, I'm doing wrong.
iPhone 6,
iOS 8.0.2,
Xcode 6
Thanks!
From what I've found (and I could be wrong) while dealing with updating a code base to correctly deal with the new privacy settings in iOS 8 is that such settings are not respected uniformly across features. For example, without changes to address privacy settings for locations, access to CLLocationManager won't work. Depending on the app, this may appear to the user as though nothing is happening or an error message may appear. However, if no such changes are made for address book privacy settings, access is always granted.
I figured out my error after stumbling upon some extremely helpful example code posted by Apple. Below is code I add/modified so that privacy settings are respected properly in the app I work on. When I find the link to the code example I will post it.
-(void) presentAddressBookPicker {
switch (ABAddressBookGetAuthorizationStatus()) {
case kABAuthorizationStatusAuthorized:
[self accessGrantedForAddressBook];
break;
case kABAuthorizationStatusNotDetermined:
[self requestAccessToAddressBook];
break;
case kABAuthorizationStatusRestricted:
case kABAuthorizationStatusDenied:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:#"Unable to access address book"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
break;
default:
// Unlikely but log it anyway.
DLog(#"Unknown address book status.");
break;
}
}
-(void) accessGrantedForAddressBook {
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
picker.peoplePickerDelegate = self;
[self presentViewController:picker animated:YES completion:nil];
}
-(void) requestAccessToAddressBook {
__weak MyWebViewController* weakSelf = self;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[weakSelf accessGrantedForAddressBook];
});
}
CFRelease(addressBook);
});
}

How do I post my own app with facebook

I have made an app and I want to create a UIButton that shares this app with facebook users.
I wrote the following code:
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller setInitialText:#"My App Name"];
[controller addURL:[NSURL URLWithString:#"https://itunes.apple.com/il/app/fruit-ninja-free/id403858572?mt=8/"]];
[self presentViewController:controller animated:YES completion:Nil];
}
else {
}
I've used an example URL of the famous fruit ninja cause my app is not yet on the app store and doesn't have it's own URL.
The issue:
If the app is not yet on iTunes, I don't have an iTunes URL to share yet... So how do I share my app?
Help is much appreciated...
You should create stub for your app at iTunes Connect. After that you'll be able to get link to your application even before submission.

iOS6 Twitter integration

There seems to be a difference between the iPhone simulator and actual device when checking if Twitter is available.
I check if a Twitter account is setup by using this code: [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter];
In the simulator there is a nice UIAlertView informing the user that there are no Twitter accounts setup and two buttons one for Settings and one for Cancel.
However when I run my app on my device it will not show the above UIAlertView. Why is that? And how can I catch what button is tapped in the above UIAlertView (since I did not instantiate it?)
This is what it looks like on the simulator:
To handle the result of the Twitter call, You can use this snippet :
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewControllerCompletionHandler __block completionHandler=^(SLComposeViewControllerResult result){
[twitterController dismissViewControllerAnimated:YES completion:nil];
switch(result){
case SLComposeViewControllerResultCancelled:
default:
{
NSLog(#"Cancelled.....");
}
break;
case SLComposeViewControllerResultDone:
{
NSLog(#"Posted....");
}
break;
}};
[twitterController setCompletionHandler:completionHandler];
...
...
}
I also am having the same issue that the device doesn't display the UIAlert like the simulator does. Until Apple fixes it, this is what I'm doing (not as clean or nice as the simulator, and requires the user to manually go to the homescreen) Apple, please fix this!
Edit: the stock Apple apps show the UIAlert perfectly of course, example: sharing a photo from the photo app without having a Twitter/Facebook account will display the correct UIAlert.
if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[mySLComposerSheet setInitialText:#"Check out the app:"];
[mySLComposerSheet addImage:[UIImage imageNamed:#"test.png"]];
[mySLComposerSheet addURL:[NSURL URLWithString:#"http://urlofyourapp.com"]];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"You need to setup an account in the Settings app under Twitter to use this feature." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
[mySLComposerSheet setCompletionHandler:^(SLComposeViewControllerResult result) {
if (result == SLComposeViewControllerResultCancelled){[mySLComposerSheet dismissModalViewControllerAnimated:YES];}
else if (result == SLComposeViewControllerResultDone){[mySLComposerSheet dismissModalViewControllerAnimated:YES];}
}];

Login View in tabbed application

How can I build a login view in a tabbed application? I'm new in ios development. I have read a lot through the internet but no one just posts the code I can understand. Any help is appreciated =).
here is a related post here: Adding login screen in front of Cocoa Touch Tab Bar Application for IOS
I prefer writing like this tho.
[window setRootController:tabBarCon];
if (notLogedIn) {
loginViewController = [[InitialScreenViewController alloc] init];
[tabBarCon.view addSubview:loginViewController.view];
}
[window makeKeyAndVisible];
return YES;
- (bool)notLoggedIn {
//check if there is any sessions exists in local storage
}
If your content relates to the user account, remember to implement a method to reload the the content after login.
Finally I did This to solve my question:
My rootViewController is my registration view, then I set a button with implements a custom segue:
-(IBAction)button {
NSLog(#"username: %#, password: %#, passwordrt: %#", username.text, password.text, passwordrt.text);
[self performSegueWithIdentifier:#"tabBar" sender:self];
}

How do I post something to facebook from iOS with latest Facebook SDK?

I'm trying to post a simple url to facebook from within my Native iOS App. What I do is this:
facebook = [[Facebook alloc] initWithAppId:FB_APP_ID andDelegate:delegate];
then at some point
[facebook authorize:permissions];
at this point my app quits and it gets me to safari and to the permissions page instead of presenting me a login-dialog (inside my app) where I login and then hit post or something an just post to my wall.
I'm pretty sure this is a simple question but since this is the first time I'm doing facebook integration it is a pain for me configuring it and the docs don't help to much.
Any help is appreciated! Thanks!
If you definitely need to remove redirection to Safari or Facebook app, you should open the FBConnect Facebook.h class and find the following method:
- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
safariAuth:(BOOL)trySafariAuth {
there you need to state that you do not want to be redirected anywhere, to do that set those two bools to NO
- (void)authorizeWithFBAppAuth:(BOOL)tryFBAppAuth
safariAuth:(BOOL)trySafariAuth {
tryFBAppAuth = NO;
trySafariAuth = NO;
Next, to post something to wall, you have two options. One is broken - the standard facebook dialog window, which will bring the keyboard BEHIND the actual webview once you show any UIAlertView ANYWHERE in your application, therefore do NOT use it. Instead use your own custom interface with a simple UITextView area and a push to wall outlet button. Here's the method that you'll need in order to post to wall from your custom view:
- (void)facebookPostToWallWithMessage:(NSString *)message {
sharedInstance.facebookDelegate = delegate;
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
message, #"message", // The status message
nil];
pushMessageRequest = [facebook requestWithGraphPath:#"me/feed"
andParams:params
andHttpMethod:#"POST"
andDelegate:self];
}
And here's the delegation methods that you'll need in order to inform users that they've posted an update or to inform them that it has failed:
- (void)request:(FBRequest *)request didLoad:(id)result {
if (request == pushMessageRequest) {
NSLog(#"message has been posted, inform delegate");
}
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error {
if (request == pushMessageRequest) {
//failed to post message, inform delegate
//[facebookDelegate facebookFailedToPostToWallWithMessage:error.localizedDescription];
}
}