ShareTwitter.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #import "ShareTwitter.h"
  21. #import "FHSTwitterEngine.h"
  22. #import "ShareWrapper.h"
  23. #define OUTPUT_LOG(...) if (self.debug) NSLog(__VA_ARGS__);
  24. @implementation ShareTwitter
  25. @synthesize mShareInfo;
  26. @synthesize debug = __debug;
  27. - (void) configDeveloperInfo : (NSMutableDictionary*) cpInfo
  28. {
  29. NSString* appKey = [cpInfo objectForKey:@"TwitterKey"];
  30. NSString* appSecret = [cpInfo objectForKey:@"TwitterSecret"];
  31. if (nil == appKey || nil == appSecret) {
  32. return;
  33. }
  34. [[FHSTwitterEngine sharedEngine]permanentlySetConsumerKey:appKey andSecret:appSecret];
  35. }
  36. - (void) share: (NSMutableDictionary*) shareInfo
  37. {
  38. self.mShareInfo = shareInfo;
  39. if ([[FHSTwitterEngine sharedEngine]isAuthorized])
  40. {
  41. [self doShare];
  42. } else {
  43. UIViewController* controller = [self getCurrentRootViewController];
  44. [[FHSTwitterEngine sharedEngine]showOAuthLoginControllerFromViewController:controller withCompletion:^(BOOL success) {
  45. if (success) {
  46. [self doShare];
  47. } else {
  48. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:@"Login Failed"];
  49. }
  50. }];
  51. }
  52. }
  53. - (void) setDebugMode: (BOOL) debug
  54. {
  55. self.debug = debug;
  56. }
  57. - (NSString*) getSDKVersion
  58. {
  59. return @"20130607";
  60. }
  61. - (NSString*) getPluginVersion
  62. {
  63. return @"0.2.0";
  64. }
  65. - (void) doShare
  66. {
  67. if (nil == mShareInfo) {
  68. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:@"Shared info error"];
  69. return;
  70. }
  71. NSString* strText = [mShareInfo objectForKey:@"SharedText"];
  72. NSString* strImagePath = [mShareInfo objectForKey:@"SharedImagePath"];
  73. BOOL oldConfig = [UIApplication sharedApplication].networkActivityIndicatorVisible;
  74. [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  75. NSError* returnCode = nil;
  76. if (nil != strImagePath) {
  77. NSData* data = [NSData dataWithContentsOfFile:strImagePath];
  78. returnCode = [[FHSTwitterEngine sharedEngine] postTweet:strText withImageData:data];
  79. } else {
  80. returnCode = [[FHSTwitterEngine sharedEngine]postTweet:strText];
  81. }
  82. [UIApplication sharedApplication].networkActivityIndicatorVisible = oldConfig;
  83. if (returnCode) {
  84. NSString* strErrorCode = [NSString stringWithFormat:@"ErrorCode %ld", (long)returnCode.code];
  85. [ShareWrapper onShareResult:self withRet:kShareFail withMsg:strErrorCode];
  86. } else {
  87. [ShareWrapper onShareResult:self withRet:kShareSuccess withMsg:@"Share Succeed"];
  88. }
  89. }
  90. - (UIViewController *)getCurrentRootViewController {
  91. UIViewController *result = nil;
  92. // Try to find the root view controller programmically
  93. // Find the top window (that is not an alert view or other window)
  94. UIWindow *topWindow = [[UIApplication sharedApplication] keyWindow];
  95. if (topWindow.windowLevel != UIWindowLevelNormal)
  96. {
  97. NSArray *windows = [[UIApplication sharedApplication] windows];
  98. for(topWindow in windows)
  99. {
  100. if (topWindow.windowLevel == UIWindowLevelNormal)
  101. break;
  102. }
  103. }
  104. UIView *rootView = [[topWindow subviews] objectAtIndex:0];
  105. id nextResponder = [rootView nextResponder];
  106. if ([nextResponder isKindOfClass:[UIViewController class]])
  107. result = nextResponder;
  108. else if ([topWindow respondsToSelector:@selector(rootViewController)] && topWindow.rootViewController != nil)
  109. result = topWindow.rootViewController;
  110. else
  111. NSAssert(NO, @"Could not find a root view controller.");
  112. return result;
  113. }
  114. @end