How can I get my app to vibrate?

Discussion in 'iOS Development' started by BigDaveyJ, Mar 21, 2009.

  1. BigDaveyJ

    BigDaveyJ New Member

    Joined:
    Nov 23, 2007
    Messages:
    2,751
    Likes Received:
    23
    Device:
    iPod touch
    I am working on iSaw, my app.

    Please Register or Log in to view images



    I have been looking for a way to get my app to vibrate, but I cannot find the code. Can someone please help me out? What is the code I need?
  2. Kn0wnTooWeLL

    Kn0wnTooWeLL New Member

    Joined:
    Nov 30, 2008
    Messages:
    123
    Likes Received:
    0
    Device:
    2G iPod touch
    Code:
    extern void * _CTServerConnectionCreate(CFAllocatorRef, int (*)(void *, CFStringRef, CFDictionaryRef, void *), int *);
    extern int _CTServerConnectionSetVibratorState(int *, void *, int, int, float, float, float);
    initialize:
    Code:
    connection = _CTServerConnectionCreate(kCFAllocatorDefault, &vibratecallback, &x);
    start vibration:
    Code:
    _CTServerConnectionSetVibratorState(&x, connection, 3, intensity, 0, 0, 0);
    stop vibration:
    Code:
    _CTServerConnectionSetVibratorState(&x, connection, 0, 0, 0, 0, 0);
  3. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Don't follow the above steps. Instead, use the call built into AudioToolbox. Vibration is considered a system sound, so just call it. It is literally one line of code that you need.
  4. spoonforknife

    spoonforknife Retired Moderator

    Joined:
    Nov 15, 2007
    Messages:
    6,896
    Likes Received:
    317
    Code:
    #import <AudioToolbox/AudioToolbox.h>
    
    - (IBAction)vib {
    
    	AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);
    }
  5. BigDaveyJ

    BigDaveyJ New Member

    Joined:
    Nov 23, 2007
    Messages:
    2,751
    Likes Received:
    23
    Device:
    iPod touch
    Thanks guys, but now it seems the vibration is too short of time. How can I extend the length of vibration for about 8 seconds
  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Adjusting the vibration time is a great way to get your app rejected, unless Apple is relaxed their anti vibrator abuse policies. On which case, loop the sound. Alternatively, use the code the other cat provided, but I can't gaurantee it will he accepted (especially because it is referencing CoreTelephony).
  7. BigDaveyJ

    BigDaveyJ New Member

    Joined:
    Nov 23, 2007
    Messages:
    2,751
    Likes Received:
    23
    Device:
    iPod touch
    Ok, so if we use the first code given to me, we can adjust the time and get accepted? Thanks SkylarEC, this app is official. lol.
  8. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    This is code I've used in the past. I call setVibration to start the vibrating. Then, I immediately issue a [performSelector:afterDelay:] to call turnOffVibrator. I forget who first figured this out, but they deserve all the credit for digging out and figuring out how to sue these functions.

    Code:
    /* Prototypes */
    extern void * _CTServerConnectionCreate(CFAllocatorRef, int (*)(void *, CFStringRef, CFDictionaryRef, void *), int *);
    extern int _CTServerConnectionSetVibratorState(int *, void *, int, float, float, float, float);
    
    
    
    /*  Your code   */
    
    
    
    -(void)turnOffVibrator:(id)param{
    	[self setVibration:FALSE];
    }
    
    int vibratecallback(void *connection, CFStringRef string, CFDictionaryRef dictionary, void *data) {
    	 return 0;
    }
     
    - (void)setVibration:(BOOL)onoff {
    	int	state = onoff ? 3 : 0;
    	void *ct = _CTServerConnectionCreate(kCFAllocatorDefault, &vibratecallback, &vibrationSomething);
    	_CTServerConnectionSetVibratorState(&vibrationSomething, ct, state, 10.0, 10.0, 10.0, 10.0);
    }





    EDIT: Again, you need to link against CoreTelephony, so I can not guarantee acceptance into the AppStore. You're better off to just loop the system sound.
  9. BeginningOfNewJourney

    BeginningOfNewJourney New Member

    Joined:
    Sep 5, 2009
    Messages:
    1
    Likes Received:
    0
    The above solution does not work for me on 3.0.1

    Here is the code...I don't get any vibration.

    Any ideas what's wrong? thanks in advance.

    [OBJC]static void* connection = nil;


    static int x = 0;


    int callback(void *connection, CFStringRef string, CFDictionaryRef dictionary, void *data) {

    return 1;

    }


    int seconds = 5;

    connection = _CTServerConnectionCreate(kCFAllocatorDefault, &callback, &x);

    // Start vibration

    _CTServerConnectionSetVibratorState(&x, connection, 3, 10, 10, 10, 10);


    time_t now = time(0);


    while(time(0) - now < seconds)
    {


    }


    //Stop vibration:
    _CTServerConnectionSetVibratorState(&x, connection, 0, 0, 0, 0, 0);[/OBJC]

Share This Page