2015-02-11 72 views
2

我想發推文,但我不想使用撰寫推文。iOS發送帶織物的推文,無需撰寫推文

TWTRComposer *composer = [[TWTRComposer alloc] init]; 

[composer setText:@"just setting up my Fabric"]; 
[composer setImage:[UIImage imageNamed:@"fabric"]]; 

[composer showWithCompletion:^(TWTRComposerResult result) { 
    if (result == TWTRComposerResultCancelled) { 
     NSLog(@"Tweet composition cancelled"); 
    } 
    else { 
     NSLog(@"Sending Tweet!"); 
    } 
}]; 

https://dev.twitter.com/twitter-kit/ios/compose這種方法顯示一些彈出屏幕,我想送鳴叫一瞬間,我怎麼能做到這一點與面料API?這種類型是可能的?

這部分代碼不能與面料做工

ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) 
    { 
     if(granted) 
     { 
      NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 

      if ([accountsArray count] > 0) 
      { 
       ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 

       TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update_with_media.json"] parameters:[NSDictionary dictionaryWithObject:self.textViewOutlet.text forKey:@"status"] requestMethod:TWRequestMethodPOST]; 

       [postRequest addMultiPartData:UIImagePNGRepresentation(image) withName:@"media" type:@"multipart/png"]; 
       [postRequest setAccount:twitterAccount]; 

       [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
        { 
         //show status after done 
         NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]]; 
         NSLog(@"Twiter post status : %@", output); 
        }]; 
      } 
     } 
    }]; 

回答

2

https://github.com/nst/STTwitter

我用這個API,比布:)

[_twitter postStatusUpdate:@"tweet text" inReplyToStatusID:nil latitude:nil longitude:nil placeID:nil displayCoordinates:nil trimUser:nil successBlock:nil errorBlock:nil]; 

很容易發出鳴叫不作曲家可能會更容易些一個幫助別人...

+0

你試過上面的代碼我正在使用,但我越來越Domain = STTwitterTwitterErrorDomain代碼= 220「您的憑據不允許訪問此資源。」 UserInfo = 0x7ff4bb5a2720 {NSLocalizedDescription =您的憑據不允許訪問此資源。,NSUnderlyingError = 0x7ff4bb598c10「HTTP狀態403:禁止」 – RameshIos 2015-03-18 09:15:46

+0

您必須先獲得許可 – 2015-03-23 12:06:05

+0

如何獲取權限?我啓用了允許此應用程序用於使用Twitter登錄並更改權限讀取,寫入和訪問直接消息。 – RameshIos 2015-03-24 05:33:55

2

試試這個:

func tweet(userId: String) { 
     let client = TWTRAPIClient(userID: userId) 

     let error: NSErrorPointer = NSErrorPointer() 
     let url: String = "https://api.twitter.com/1.1/statuses/update.json" 
     let message: [NSObject : AnyObject] = [ 
      "status" : "Sample Tweet Tweet!" 
     ] 

     let preparedRequest: NSURLRequest = client.URLRequestWithMethod("POST", URL: url, parameters: message, error: error) 

     client.sendTwitterRequest(preparedRequest) { (response, data, jsonError) -> Void in 
      do { 
       let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String:AnyObject] 
       NSLog("%@", json) 
       print("Tweet post!") 
      } catch { 
       print("json error: \(error)") 
      } 
     } 
    } 

您必須先獲取用戶的userId。如果您使用手動按鈕註冊,請使用以下代碼:

func tweet(userId: String) { 
     let client = TWTRAPIClient(userID: userId) 

     let error: NSErrorPointer = NSErrorPointer() 
     let url: String = "https://api.twitter.com/1.1/statuses/update.json" 
     let message: [NSObject : AnyObject] = [ 
      "status" : "Sample Tweet Tweet!" 
     ] 

     let preparedRequest: NSURLRequest = client.URLRequestWithMethod("POST", URL: url, parameters: message, error: error) 

     client.sendTwitterRequest(preparedRequest) { (response, data, jsonError) -> Void in 
      do { 
       let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as! [String:AnyObject] 
       NSLog("%@", json) 
       print("Tweet post!") 
      } catch { 
       print("json error: \(error)") 
      } 
     } 
    } 

對不起,我使用Swift編寫了此代碼。

+0

謝謝...你有這樣的Swift 3嗎? – Jonny 2017-03-28 08:49:05