gpt4 book ai didi

iphone - 发推文,不使用推文表

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:29:50 24 4
gpt4 key购买 nike

我使用下面的代码通过 twitter 分享内容(来自 UITextViewUIImageView)

-(void)shareViaTweet:(NSString *)shareMessage{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[tweetSheet setInitialText:[NSString stringWithFormat:@"%@",shareMessage]];
if (self.imageString)
{
[tweetSheet addImage:[UIImage imageNamed:self.imageString]];
}

if (self.urlString)
{
[tweetSheet addURL:[NSURL URLWithString:self.urlString]];
}
[self 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"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}

}

但我需要分享这个,而不是使用弹出 View (我认为是推文表)。这是因为下面的代码,

[self presentViewController:tweetSheet animated:YES completion:nil];

当我点击我的应用程序的“分享”按钮时,我需要在 Twitter 上发布它。

编辑:

- (IBAction)doneButtonClicked:(id)sender
{
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *message = messageTextView.text;
//hear before posting u can allow user to select the account
NSArray *arrayOfAccons = [account accountsWithAccountType:accountType];
for(ACAccount *acc in arrayOfAccons)
{
NSLog(@"%@",acc.username); //in this u can get all accounts user names provide some UI for user to select,such as UITableview
}


NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"
@"/1.1/statuses/user_timeline.json"];
NSDictionary *params = @{@"screen_name" : message,
@"forKey":@"status",
@"trim_user" : @"1",
@"count" : @"1"};
// Request access from the user to access their Twitter account

[account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)

{
if (granted == YES)
{
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
//use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0]; //hear this line replace with selected account. than post it :)




SLRequest *request =
[SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:url
parameters:params];


//Post the request
[request setAccount:acct];

//manage the response
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if(error)
{
//if there is an error while posting the tweet
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Error in posting" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

}
else
{
// on successful posting the tweet
NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Successfully posted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];


}
}];

}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"You have no twitter account" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

}
}
else
{
//suppose user not set any of the accounts
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Permission not granted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

}
} ];


//[widgetsHandler closeWidget:nil];


//[self postImage:shareImageView.image withStatus:messageTextView.text];
}

更新:错误

enter image description here

最佳答案

要发送图片你需要做这样的事情


  [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
{
if (granted == YES)
{
// Populate array with all available Twitter accounts
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0)
{
//use the first account available
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

//create this request
SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"https://api.twitter.com"@"/1.1/statuses/update_with_media.json"] parameters: [NSDictionary dictionaryWithObject:message forKey:@"status"]];
UIImage *imageToPost = [UIImage imageNamed:@"image.jpg"];
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0f);//set the compression quality
[postRequest addMultipartData:imageData withName:@"media" type:@"image/jpeg" filename:@"image.jpg"];

//set account and same as above code

....
....


关于iphone - 发推文,不使用推文表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18650837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com