【问题标题】:How to disable completion sounds from SLComposeViewController (Facebook, Twitter, ...)?如何禁用 SLComposeViewController(Facebook、Twitter、...)的完成声音?
【发布时间】:2012-10-22 21:12:49
【问题描述】:

有谁知道如何在 iOS 中禁用 SLComposeViewController 的完成声音?

在用户发布消息后播放声音,例如脸书或推特。

【问题讨论】:

    标签: ios facebook twitter slcomposeviewcontroller


    【解决方案1】:

    您可以执行以下操作:

    让 SLComposeViewController 在按下“发送”时不发送推文。并手动发送推文。

    1.递归遍历所有视图并找到“发送”按钮
    // UIButton with width 50px
    - (UIButton *)tweetSendButton:(UIView *)view
    {
        for (UIView * subview in view.subviews)
        {
            if ([subview isKindOfClass:[UIButton class]]
                && subview.bounds.size.width == 50)
            {
                return (UIButton *)subview;
            }
            UIButton * button = [self tweetSendButton:subview];
            if (button) return button;
        }
        return nil;
    }
    
    ...
    UIButton * sendButton = [self tweetSendButton:_tweetController.view];
    
    2. 移除目标 SLComposeViewController 的所有动作
    NSArray * actions = [sendButton actionsForTarget:_tweetController forControlEvent:UIControlEventTouchUpInside];
    for (NSString * action in actions)
        [sendButton removeTarget:_tweetController action:NSSelectorFromString(action) forControlEvents:UIControlEventTouchUpInside];
    
    3.为UIControlEventTouchUpInside事件添加自己的动作
    [sendButton addTarget:self action:@selector(sendCustomTweet:) forControlEvents:UIControlEventTouchUpInside];
    
    4. 当按下“发送”按钮时,使用此方法获取文本和帐户(如有必要):
    ....
    UITextView * textView = [self tweetTextView:self.tweetController.view];
    UIButton * accountButton = [self twitterAccountButton:self.tweetController.view];
    
    NSString * tweetText = textView.text;
    NSString * tweetAccount = [accountButton.titleLabel.text substringFromIndex:1]; // skip @ char
    ....
    
    5. 以下是这些方法:
    // Single UITextView
    - (UITextView *)tweetTextView:(UIView *)view
    {
        for (UIView * subview in view.subviews)
        {
            if ([subview isMemberOfClass:[UITextView class]])
                return (UITextView *)subview;
            UITextView * textView = [self tweetTextView:subview];
            if (textView) return textView;
        }
        return nil;
    }
    
    // UIButton witch starts from @
    - (UIButton *)twitterAccountButton:(UIView *)view
    {
        for (UIView * subview in view.subviews)
        {
            if ([subview isKindOfClass:[UIButton class]])
            {
                UIButton * button = (UIButton *)subview;
                if (button.titleLabel.text && [button.titleLabel.text rangeOfString:@"@"].location == 0)
                    return button;
            }
            UIButton * button = [self twitterAccountButton:subview];
            if (button) return button;
        }
        return nil;
    }
    
    6.手动发送推文
    - (void)sendTweet:(NSString *)text fromAccount:(NSString *)account withArtwork:(UIImage *)artwork
    {
        // 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) return;
    
            // Get the list of Twitter accounts.
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
    
            // For the sake of brevity, we'll assume there is only one Twitter account present.
            // You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
            for(ACAccount *twitterAccount in accountsArray)
            {
                if (account && ([account compare:twitterAccount.username options:(NSCaseInsensitiveSearch)] != 0))
                    continue;
    
                // Create a request, which in this example, posts a tweet to the user's timeline.
                // This example uses version 1 of the Twitter API.
                // This may need to be changed to whichever version is currently appropriate.
                NSString * method = artwork ? @"update_with_media" : @"update";
                NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/%@.json",method,nil]];
                TWRequest *postRequest = [[TWRequest alloc] initWithURL:url parameters:@{@"status":text} requestMethod:TWRequestMethodPOST];
    
                // Set the account used to post the tweet.
                [postRequest setAccount:twitterAccount];
    
                if (artwork)
                {
                    NSData * data = UIImageJPEGRepresentation(artwork, 0.8);
                    [postRequest addMultiPartData:data withName:@"media" type:@"JPG"];
                }
    
                // Perform the request created above and create a handler block to handle the response.
                [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    if (urlResponse.statusCode == 200)
                    {
                        NSLog(@"Tweet sent successfully!");
                    }
                    else
                    {
                        NSDictionary * json = [NSJSONSerialization JSONObjectWithData:responseData
                                                                              options:kNilOptions
                                                                                error:&error];
                        NSLog(@"Tweet sending failed with error: %@", json);
                    }                    
                }];
            }
        }];
    }
    

    【讨论】:

      猜你喜欢
      • 2014-02-25
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多