2012-02-24 14 views
5

अनिवार्य रूप से जो मैं चाहता हूं वह ऐप के लिए है, एक बार जब उपयोगकर्ता ने अपने ट्विटर खाते तक पहुंच की अनुमति दी है, तो उपयोगकर्ता द्वारा चुने गए ट्वीट को ट्वीट करने में सक्षम होने के लिए एक UITableView। आदर्श रूप से मैं आईओएस 5 में ट्विटर ढांचे का उपयोग करना चाहता हूं, लेकिन मेरे पास मुख्य मुद्दा ट्वीटिंग के लिए मोडल व्यू कंट्रोलर है। क्या यह वैकल्पिक है? क्या इसके बिना ट्वीट करना संभव है और यदि नहीं, तो आप क्या सुझाव देते हैं?आईओएस 5 ट्विटर फ्रेमवर्क: उपयोगकर्ता इनपुट और पुष्टिकरण (मोडल व्यू कंट्रोलर) के बिना ट्वीटिंग

धन्यवाद!

उत्तर

11

इसके बिना ट्वीट करना निश्चित रूप से संभव है, निम्नलिखित आईओएस 5 ऐप्स उत्पादन में है। यदि उपयोगकर्ता ने खाता पंजीकृत नहीं किया है तो यह उपयोगकर्ता को वरीयताओं के आवश्यक अनुभाग में भी ले जाता है।

- (void)postToTwitter 
{ 
    // Create an account store object. 
    ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 

    // Create an account type that ensures Twitter accounts are retrieved. 
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

    // Request access from the user to use their Twitter accounts. 
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) { 
     if(granted) { 
      // Get the list of Twitter accounts. 
      NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 


      if ([accountsArray count] > 0) { 
       // Grab the initial Twitter account to tweet from. 
       ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 
       TWRequest *postRequest = nil; 

       postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:[self stringToPost] forKey:@"status"] requestMethod:TWRequestMethodPOST]; 



       // Set the account used to post the tweet. 
       [postRequest setAccount:twitterAccount]; 

       dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { 
        [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
         dispatch_async(dispatch_get_main_queue(), ^(void) { 
          if ([urlResponse statusCode] == 200) { 
           Alert(0, nil, @"Tweet Successful", @"Ok", nil); 
          }else { 

           Alert(0, nil, @"Tweet failed", @"Ok", nil); 
          } 
         }); 
        }]; 
       }); 

      } 
      else 
      { 
       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]]; 
      } 
     } 
    }]; 
} 
+0

बहुत दोस्त धन्यवाद। – sooper

+0

@Sooper कोई समस्या नहीं –

+0

कोई छवि जोड़ने के बारे में क्या? –

6

यह होगा एक अद्यतन संस्करण TWRequest के बजाय SLRequest, जो आईओएस में पदावनत किया गया था 6. नोट इस सामाजिक जरूरत है और ढांचे लेखा अपनी परियोजना के लिए जोड़ा जा करने के लिए का उपयोग कर ... हो

- (void) postToTwitterInBackground { 

    // Create an account store object. 
    ACAccountStore *accountStore = [[ACAccountStore alloc] init]; 

    // Create an account type that ensures Twitter accounts are retrieved. 
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

    // Request access from the user to use their Twitter accounts. 
    [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) { 
     if(granted) { 
      // Get the list of Twitter accounts. 
      NSArray *accountsArray = [accountStore accountsWithAccountType:accountType]; 

      if ([accountsArray count] > 0) { 
       // Grab the initial Twitter account to tweet from. 
       ACAccount *twitterAccount = [accountsArray objectAtIndex:0]; 
       SLRequest *postRequest = nil; 

       // Post Text 
       NSDictionary *message = @{@"status": @"Tweeting from my iOS app!"}; 

       // URL 
       NSURL *requestURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/update.json"]; 

       // Request 
       postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:requestURL parameters:message]; 

       // Set Account 
       postRequest.account = twitterAccount; 

       // Post 
       [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
        NSLog(@"Twitter HTTP response: %i", [urlResponse statusCode]); 
       }]; 

      } 
     } 
    }]; 

} 
+0

आकर्षण की तरह काम करता है! मैं थोड़ी देर के लिए इस अद्यतन के लिए चारों ओर देख रहा हूँ। धन्यवाद –

+0

मेरी खुशी .... –

+0

मैं यह कोशिश कर रहा हूं लेकिन मुझे ट्विटर से 403 त्रुटि वापस मिल रही है .. कोई विचार? – RyanG

5

अद्यतन: ट्विटर द्वारा फैब्रिक में ट्विटरकिट काफी आसान है और यदि आप अपने ऐप में ट्वीट करने का प्रयास करते हैं तो अपने ट्विटर ऐप से पोस्ट करना है, तो यह विचार करने का एक अच्छा विकल्प हो सकता है।

(हाँ, यह विधि आपको किसी भी संवाद बॉक्स या पुष्टि के बिना ट्विटर पर पोस्ट करने की अनुमति देगी)।

ट्विटरकिट अनुमति भाग को संभालेगा और TWTRAPICLient का उपयोग करके हम ट्विटर बाकी एपीआई के माध्यम से ट्वीट करेंगे।

//Needs to performed once in order to get permissions from the user to post via your twitter app. 
[[Twitter sharedInstance]logInWithCompletion:^(TWTRSession *session, NSError *error) { 
    //Session details can be obtained here 
    //Get an instance of the TWTRAPIClient from the Twitter shared instance. (This is created using the credentials which was used to initialize twitter, the first time) 
    TWTRAPIClient *client = [[Twitter sharedInstance]APIClient]; 

    //Build the request that you want to launch using the API and the text to be tweeted. 
    NSURLRequest *tweetRequest = [client URLRequestWithMethod:@"POST" URL:@"https://api.twitter.com/1.1/statuses/update.json" parameters:[NSDictionary dictionaryWithObjectsAndKeys:@"TEXT TO BE TWEETED", @"status", nil] error:&error]; 

    //Perform this whenever you need to perform the tweet (REST API call) 
    [client sendTwitterRequest:tweetRequest completion:^(NSURLResponse *response, NSData *data, NSError *connectionError) { 
    //Check for the response and update UI according if necessary.    
    }]; 
}]; 

उम्मीद है कि इससे मदद मिलती है।

+0

लॉन्च किया है धन्यवाद! यह पूरी तरह से काम करता था और आपका उदाहरण वास्तव में केवल एक ही था जिसे मैं पा सकता था। मेरी इच्छा है कि ट्विटर ने अपने दस्तावेज़ों में यह किया था। – dirkoneill

+0

यह जानकर खुशी हुई कि इससे आपको @ डिककोनील की मदद मिली। बीटीडब्ल्यू initWithConsumerKey अब ट्विटरकिट v1.4.0 के साथ काम नहीं करता है जिसे दो दिन पहले रिलीज़ किया गया था। उचित जवाब अद्यतन किया है। –

+0

यह वही है जो मैं एक दिन के लिए खोज रहा था। यह मेरे लिए काम किया। ऊपर उठाया !!! – NSPratik

1

स्वीकृत उत्तर अब कई बदलावों के कारण मान्य नहीं है। यह आईओएस 10, स्विफ्ट 3 और ट्विटर के एपीआई के संस्करण 1.1 के साथ काम करता है।

** अद्यतन **

के रूप में पिछले एक एक पदावनत ट्विटर समाप्ति बिंदु पर भरोसा इस उत्तर अद्यतन किया गया है।

import Social 
import Accounts 

func postToTwitter() { 
    let accountStore = ACAccountStore() 
    let accountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter) 

    accountStore.requestAccessToAccounts(with: accountType, options: nil) { (granted, error) in 
     if granted, let accounts = accountStore.accounts(with: accountType) { 
      // This will default to the first account if they have more than one 

      if let account = accounts.first as? ACAccount { 
       let requestURL = URL(string: "https://api.twitter.com/1.1/statuses/update.json") 
       let parameters = ["status" : "Tweet tweet"] 
       guard let request = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: .POST, url: requestURL, parameters: parameters) else { return } 
       request.account = account 
       request.perform(handler: { (data, response, error) in 
        // Check to see if tweet was successful 
       }) 
      } else { 
       // User does not have an available Twitter account 
      } 
     } 
    } 
} 

This is the API जिसका उपयोग किया जा रहा है।

+0

का आह्वान किया है क्या यह अभी भी काम करता है? स्विफ्ट के साथ इसमें कुछ समस्याएं थीं। 403 त्रुटि भी प्राप्त हो रही है। – Jonny

+0

@ जॉनी मैंने जवाब अपडेट किया, अभी यह पुष्टि करने के लिए अभी परीक्षण किया गया है। – CodeBender

+0

बहुत बहुत धन्यवाद! इस बीच, मैंने इसे फैब्रिक के माध्यम से स्थापित ट्विटरकिट एसडीके का उपयोग करके काम किया। प्रवाह बहुत समान है ... मुझे लगता है कि अंतर ACAccountStore ट्विटर में आईओएस के मौजूदा ऐप का उपयोग करता है, जबकि ट्विटरकिट एक ऐप का उपयोग करता है जिसे हमें apps.twitter.com पर खुद को पंजीकृत करना होता है ... – Jonny

संबंधित मुद्दे