Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.34 KB | None | 0 0
  1. - (void) shareOnTwitterWithMessage:(NSString *)message {
  2.  
  3. ACAccountStore *twitterAccountStore = [[ACAccountStore alloc]init];
  4. ACAccountType *TWaccountType= [twitterAccountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
  5.  
  6. [twitterAccountStore requestAccessToAccountsWithType:TWaccountType options:nil completion:
  7.  
  8. ^(BOOL granted, NSError *e) {
  9.  
  10. if (granted) {
  11.  
  12. NSArray *accounts = [twitterAccountStore accountsWithAccountType:TWaccountType];
  13.  
  14. twitterAccounts = [accounts lastObject];
  15.  
  16. NSDictionary *dataDict = @{@"status": message};
  17.  
  18. [self performSelectorInBackground:@selector(postToTwitter:) withObject:dataDict];
  19.  
  20. }
  21. else {
  22.  
  23. return ;
  24. }
  25.  
  26. }];
  27. }
  28.  
  29.  
  30. - (void)postToTwitter:(NSDictionary *)dataDict{
  31.  
  32. NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update_with_media.json"];
  33.  
  34. SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL parameters:dataDict];
  35.  
  36. NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"icon@2x.png"]);
  37.  
  38. [request addMultipartData:imageData
  39. withName:@"media[]"
  40. type:@"image/jpeg"
  41. filename:@"image.jpg"];
  42.  
  43. request.account = twitterAccounts;
  44.  
  45. [request performRequestWithHandler:^(NSData *data, NSHTTPURLResponse *response, NSError *error) {
  46.  
  47. if(!error){
  48.  
  49. NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
  50.  
  51. if(![list objectForKey:@"errors"]){
  52.  
  53. if([list objectForKey:@"error"]!=nil){
  54.  
  55. //Delegate For Fail
  56. return ;
  57. }
  58.  
  59. }
  60. }
  61.  
  62. }];
  63.  
  64. }
  65.  
  66. NSString *statusesShowEndpoint = @"https://api.twitter.com/1.1/statuses/update.json";
  67. NSDictionary *params = @{@"status": @"Hello, my first autopost tweet..."};
  68.  
  69. NSError *clientError;
  70. NSURLRequest *request = [[[Twitter sharedInstance] APIClient]
  71. URLRequestWithMethod:@"POST"
  72. URL:statusesShowEndpoint
  73. parameters:params
  74. error:&clientError];
  75.  
  76. if (request) {
  77. [[[Twitter sharedInstance] APIClient]
  78. sendTwitterRequest:request
  79. completion:^(NSURLResponse *response,
  80. NSData *data,
  81. NSError *connectionError) {
  82. if (data) {
  83. // handle the response data e.g.
  84. NSError *jsonError;
  85. NSDictionary *dicResponse = [NSJSONSerialization
  86. JSONObjectWithData:data
  87. options:0
  88. error:&jsonError];
  89. NSLog(@"%@",[dicResponse description]);
  90. }
  91. else {
  92. NSLog(@"Error code: %ld | Error description: %@", (long)[connectionError code], [connectionError localizedDescription]);
  93. }
  94. }];
  95. }
  96. else {
  97. NSLog(@"Error: %@", clientError);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement