可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've about completed my iOS app but only need to add two buttons:
- Follow us on facebook
- Follow us on twitter
I assumed I would find a couple of simple examples here, but was surprised to find no answer to this at all (yet).
I'm pretty sure I could spend the next few hours trying to figure it out, but thought I'd reach out for a little time-saving help.
Just looking for the code to go behind a button on a view controller (XCode 4.5.1, iOS 6).
I assume the only variable I might need to supply is the company's facebook account name.
Any suggestions? (Thanks in advance!)
回答1:
First, the URL schem for Facebook: fb://profile/<yourpageid>
(source). A URL with this structure will open the Facebook app, if it is installed.
More on iOS URL schemes.
When your button is tapped, you can check if the Facebook is installed:
-(IBAction)fbButtonTap:(id)sender {
NSURL *fbURL = [[NSURL alloc] initWithString:@"fb://profile/<yourpageid>"];
// check if app is installed
if ( ! [[UIApplication sharedApplication] canOpenURL:fbURL] ) {
// if we get here, we can't open the FB app.
fbURL = ...; // direct URL on FB website to open in safari
}
[[UIApplication sharedApplication] openURL:fbURL];
}
For twitter, you follow the same basic steps. The Twitter URL scheme for iOS is twitter://user?id=12345
or twitter://user?screen_name=yourname
(source). Again, if the Twitter app is not installed, you have open the twitter profile in safari.
As for taking direct actions, I do not think you can do that, since there is no inherent knowledge about any other applciation installed on the device. The best I think you can do is direct users to each respective account.
回答2:
facebook have a page about adding a button, see on this link,
https://developers.facebook.com/docs/reference/plugins/follow/
and twitters is here
https://support.twitter.com/articles/20164833-how-to-add-the-follow-button-to-your-website
hope it helps
回答3:
You could use SLRequest have someone follow you. This will only work on iOS 6 though, on iOS 5 Twitter works but not Facebook.
An example of SLRequest for someone to follow you on Twitter, make sure this is called on the background thread:
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
if (granted) {
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// For the sake of brevity, we'll assume there is only one Twitter account present.
// You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc] init];
[tempDict setValue:@"Your twitter name" forKey:@"screen_name"];
[tempDict setValue:@"true" forKey:@"follow"];
SLRequest *followRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com/1/friendships/create.json"] parameters:tempDict];
[followRequest setAccount:twitterAccount];
[followRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
NSLog(@"%@", output);
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show follow request failed
});
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI to show success
});
}
}];
}
}
}];
And for Facebook you just have to change some of it and look at their API and change the request URL.
回答4:
this is my code for show people to like the community page of my app.
you can add the _webview where you want to show inside the code.
facebook gives the code for showing the page inside webview.
-(void)likeus
{
_webview =[[UIWebView alloc] initWithFrame:CGRectMake(14,94, 292, 250)];
AppDelegate *appdelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//Load web view data
NSString *strWebsiteUlr =@"http://www.facebook.com/plugins/likebox.php?href=https%3A%2F%2Fwww.facebook.com%2Fenbuyukkim&width=292&height=258&show_faces=true&colorscheme=dark&stream=false&border_color&header=false&appId=433294056715040";
// Load URL
//Create a URL object.
NSURL *url = [NSURL URLWithString:strWebsiteUlr];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[_webview loadRequest:requestObj];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:_webview
action:@selector(removeFromSuperView)
forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(270.0, 80.0, 30.0, 30.0);
[button setBackgroundImage:[UIImage imageNamed:@"cross.png"] forState:UIControlStateNormal];
[_webview addSubview:button];
}
回答5:
I am not sure if this is what you are looking for, but I think I found the solution, here at stack overflow on another thread.
Take a look at this thread:
Adding the Facebook Like Button in an iPhone App
Hope that helps you!