【发布时间】:2014-05-10 07:39:05
【问题描述】:
我正在使用 SLComposeViewController 在 twitter 上分享图片。如果我没有通过 iPhone 设置登录,我想显示警报视图。请任何机构提出建议。
【问题讨论】:
-
谢谢老兄它的工作。
-
如果它有效,请投票,对其他人有用...
我正在使用 SLComposeViewController 在 twitter 上分享图片。如果我没有通过 iPhone 设置登录,我想显示警报视图。请任何机构提出建议。
【问题讨论】:
使用此代码:
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];
【讨论】:
使用 ACAccountStore 和 ACAccount 您可以查看您是否登录过的天气。
在此处参考示例代码:How to post on twitter by hiding SLComposeViewController *tweetSheettweet instance
【讨论】:
在.h文件中声明
@property (strong, nonatomic) ACAccount *fbAccount;
@property (strong, nonatomic) NSString *slService;
@property (strong, nonatomic) ACAccountStore *accountStore;
在.m文件中添加
- (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];
}
}
【讨论】: