0

ok so I have my listener set up in my application so it is reading when I click on the external volume buttons. My question is and seems I am stuck on this is that I have two buttons in my application identified as:

- (IBAction)sendMessage:(id)sender;
- (IBAction)cancelMessage:(id)sender;

When I click on the Up ^ physical button on my iPad, I want it to link up with the,

- (IBAction)sendMessage:(id)sender;

And with the down physical button, Need to link with the, - (IBAction)cancelMessage:(id)sender;

Here is my code where I am stuck on:

#import "FirstViewController.h"
#import "AppDelegate.h"
#import "AudioToolbox/AudioServices.h"
#import <MediaPlayer/MediaPlayer.h>



@interface FirstViewController ()


@property (nonatomic, strong) AppDelegate *appDelegate;

-(void)sendMyMessage;
-(void)didReceiveDataWithNotification:(NSNotification *)notification;

@end

@implementation FirstViewController

// ADDED THIS SECTION------------------------------------------------------------

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

// ADDED THIS SECTION------------------------------------------------------------


int Count=0;

- (void)viewDidLoad
{
 // ADDED THIS SECTION------------------------------------------------------------


    UISwitch *switcher = [[UISwitch alloc] init];
    switcher.center = self.view.center;
    [switcher addTarget: self action: @selector(volumeListener_onOff:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:switcher];

 // ADDED THIS SECTION------------------------------------------------------------



    [super viewDidLoad];



    // Do any additional setup after loading the view, typically from a nib.

    _appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    _txtMessage.delegate = self;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(didReceiveDataWithNotification:)
                                                 name:@"MCDidReceiveDataNotification"
                                               object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



 // ADDED THIS SECTION------------------------------------------------------------
 // ADDED THIS SECTION------------------------------------------------------------

- (void) volumeListener_onOff:(id)sender
{
    UISwitch *onOff = (UISwitch *) sender;

    if(onOff.on){
        volumeListener = [[VolumeListener alloc] init];
        [[self.view viewWithTag:54870149] removeFromSuperview];
        [self.view addSubview: [volumeListener dummyVolume]];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
    }
    else{
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
        [[self.view viewWithTag:54870149] removeFromSuperview];
    }
}


 // ADDED THIS SECTION------------------------------------------------------------
 // ADDED THIS SECTION------------------------------------------------------------


- (void)volumeChanged:(NSNotification *)notification{
    if(volumeListener.runningVolumeNotification==FALSE){
        dispatch_async(dispatch_get_main_queue(), ^{
            volumeListener.runningVolumeNotification = TRUE;
            MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
            [musicPlayer setVolume:volumeListener.systemVolume];

            // do what you want to accomplish here
            [self.view setAlpha:0.0f];
            [UIView beginAnimations:@"flash screen" context:nil];
            [UIView setAnimationDuration:0.1f];
            [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
            [self.view setAlpha:1.0f];
            [UIView commitAnimations];

            dispatch_async(dispatch_get_global_queue(0, 0), ^{
                volumeListener.runningVolumeNotification = FALSE;
            });
        });
    }
}

// ADDED THIS SECTION------------------------------------------------------------
// ADDED THIS SECTION------------------------------------------------------------

#pragma mark - UITextField Delegate method implementation

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self sendMyMessage];
    return YES;
}


#pragma mark - IBAction method implementation

- (IBAction)sendMessage:(id)sender {
    [self sendMyMessage ];
}

- (IBAction)cancelMessage:(id)sender {
    [self sendMyadditionMessage ];
}


#pragma mark - Private method implementation

// THIS IS THE SECTION FOR ADDITION ----------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------

-(void)sendMyMessage{


    NSData *dataToSend = [_txtMessage.text dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *allPeers = _appDelegate.mcManager.session.connectedPeers;
    NSError *error;

    [_appDelegate.mcManager.session sendData:dataToSend
                                     toPeers:allPeers
                                    withMode:MCSessionSendDataReliable
                                       error:&error];

    if (error) {
        NSLog(@"%@", [error localizedDescription]);
    }

    [_tvChat setText:[_tvChat.text stringByAppendingString:[NSString stringWithFormat:@"\n%@\n\n", _txtMessage.text]]];
    [_txtMessage setText:@""];

    // THIS IS WHAT STARTS THE COUNT FOR THE TEXT
    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", Count--];
    _txtMessage.text = numValue;
    // END CODE FOR COUNT

}


// START SECTION FOR ADDITION ---------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------

-(void)sendMyadditionMessage{
    NSData *dataToSend = [_txtMessage.text dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *allPeers = _appDelegate.mcManager.session.connectedPeers;
    NSError *error;

    [_appDelegate.mcManager.session sendData:dataToSend
                                     toPeers:allPeers
                                    withMode:MCSessionSendDataReliable
                                       error:&error];

    if (error) {
        NSLog(@"%@", [error localizedDescription]);
    }

    [_tvChat setText:[_tvChat.text stringByAppendingString:[NSString stringWithFormat:@"\n%@\n\n", _txtMessage.text]]];
    [_txtMessage setText:@""];

    // THIS IS WHAT STARTS THE COUNT FOR THE TEXT
    NSString *numValue = [[NSString alloc] initWithFormat:@"%d", Count++];
    _txtMessage.text = numValue;
    // END CODE FOR COUNT


    if(Count == 3) {
        return [self vibrate];
    };
}


// END SECTION FOR ADDITION ---------------------------------------------------------------
// ---------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------

-(void)vibrate
{
    NSLog(@"I'm vibrating");
    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}


-(void)didReceiveDataWithNotification:(NSNotification *)notification{
    MCPeerID *peerID = [[notification userInfo] objectForKey:@"peerID"];
    NSString *peerDisplayName = peerID.displayName;

    NSData *receivedData = [[notification userInfo] objectForKey:@"data"];
    NSString *receivedText = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

    [_tvChat performSelectorOnMainThread:@selector(setText:) withObject:[_tvChat.text stringByAppendingString:[NSString stringWithFormat:@"%@ wrote:\n%@\n\n", peerDisplayName, receivedText]] waitUntilDone:NO];
}

@end
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Chase Chiangi
  • 61
  • 1
  • 7
  • No, you can't do this. – Fogmeister Jan 06 '15 at 18:31
  • 4
    Ignoring the answer from your earlier question will not change the outcome. http://stackoverflow.com/questions/27786923/physical-volume-control-buttons-assigned-to-ios-app-buttons – Fogmeister Jan 06 '15 at 18:33
  • Please narrow down the code in your question to the relevant code (volume button events and the two methods you want to call. – rmaddy Jan 06 '15 at 18:36
  • 1
    Why don't you simply call your two methods (`sendMessage:` and `cancelMessage:`) in your `volumeChanged:` method? – rmaddy Jan 06 '15 at 18:37
  • @vikingosegundo I wouldn't mark this as a duplicate of his previous question since this one actually has relevant code and can actually be answered. – rmaddy Jan 06 '15 at 18:43
  • 1
    Than he should edit his previous question. – vikingosegundo Jan 06 '15 at 18:44
  • @Fogmeister its already done, I have control of the buttons on my physical volume control.. My question boils down to once the listener knows I hit the up/down button, I need it to select my two IBACTION buttons – Chase Chiangi Jan 06 '15 at 18:48
  • 1
    [**10.5** Apps that alter the functions of standard switches, such as the Volume Up/Down and Ring/Silent switches, will be rejected](https://developer.apple.com/app-store/review/guidelines/#user-interface) – vikingosegundo Jan 06 '15 at 18:51
  • @vikingosegundo I am not entering it in the App Store.. So instead of saying "it can't be done" which BTW it alrdy has been done.. Can you incorporate in helping? – Chase Chiangi Jan 06 '15 at 18:53
  • I didn't say it can't be done. Though I wouldn't do it. – vikingosegundo Jan 06 '15 at 18:54

1 Answers1

1

May I make a suggestion? Use RBVolumeButtons, an open source class that lets you easily steal the volume buttons. I use this in my app and it works perfectly. Let me know if you have any questions about how to use it.

Max
  • 81
  • 3
  • 6
  • Can you share how you use it? I can't get it to work. E.g. where to you create the `RBVolumeButtons` object? In `viewWillAppear` or `viewDidLoad`? – Stan James Jul 15 '16 at 01:12