简体   繁体   中英

How do I count how many times an ibaction is performed?

I am about to release an iPhone app for a small business. The app has a call button which calls the business with an ibaction. I would like to know if i can find out how many times this action occurs so i can get a better idea of how effective the app is, and how to go about it.

Thanks for your time.

You could implement a web-service with a simple method which will increment a counter on a server. And call it asynchronously every time user taps a button. I would do it like this:

#define TAP_COUNT_KEY @"tapcnt"
#define MAKE_URL(X) [NSURL URLWithString:[NSString stringWithFormat:@"http://my_service_address?cnt=%d", X]]

dispatch_queue_t serviceQueue = dispatch_queue_create("Tap counter queue", NULL);
dispatch_async(serviceQueue, ^{
    NSUInteger counter = [[NSUserDefaults standardUserDefaults] integerForKey:TAP_COUNT_KEY] + 1;

    NSURL *serviceUrl = MAKE_URL(counter);
    NSError *error = nil;
    [NSString stringWithContentsOfURL:serviceUrl encoding:NSUTF8StringEncoding error:&error];

    if (error)
        NSLog(@"%@", [error localizedDescription]);
    else
        counter = 0;

    [[NSUserDefaults standardUserDefaults] setInteger:counter forKey:TAP_COUNT_KEY];
});
dispatch_release(serviceQueue);

On IBAction method, you can increment counter by +1. Then you can store it into NSUserDefaults by key-value pair. And every time, increase counter and store it. So it'll maintain counter state until application will be uninstalled.

int counter;

-(IBAction)click:(id)sender
{
     NSUserDefaults *_countDefaults = [NSUserDefaults standardUserDefaults];
     counter = [[_countDefaults integerForKey:@"Counter"] intValue];
     counter++;

     [_countDefaults setInteger:counter forKey:@"Counter"];
     [_countDefaults synchronize];       
}

Hopefully, you'll get idea.

Thanks.

You can increase your button tag value and access it where ever you want,

-(IBAction)btnPanic:(id)sender
{
    UIButton *btn=(UIButton *)sender;
    btn.tag=btn.tag+1;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM