3

我正在我的 iOS 应用程序中展示内容,我希望用户能够投票。我不想实现复杂的“注册用户名”功能,而是希望用户能够投票。
在 Web 开发的世界中,我可以通过使用客户端的 IP 地址来防止重复投票。

如何制作一个简单的投票系统,让用户只能对帖子进行一次投票,而不必担心重复?

4

2 回答 2

9

不要uniqueIdentifier像Apple所说的那样使用:

在 iOS 5.0 中已弃用

唯一标识符

基于各种硬件细节的每个设备唯一的字母数字字符串。(只读)(在 iOS 5.0 中已弃用。相反,创建一个特定于您的应用程序的唯一标识符。)

相反,我建议改用uniqueIdentifier这个开源库(实际上是 2 个简单的类别)。它利用设备的 MAC 地址和 App Bundle Identifier 在您的应用程序中生成一个唯一 ID,可用作 UDID 替代品。

请记住,与 UDID 不同,这个数字对于每个应用程序都是不同的。

您只需要导入包含的NSStringUIDevice类别并调用:

#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5Addition.h"
NSString *iosFiveUDID = [[UIDevice currentDevice] uniqueDeviceIdentifier]

你可以在 Github 上找到它:

https://github.com/gekitz/UIDevice-with-UniqueIdentifier-for-iOS-5


继承人的代码(只是 .m 文件 - 检查 github 项目的标题):

UIDevice+IdentifierAddition.m

#import "UIDevice+IdentifierAddition.h"
#import "NSString+MD5Addition.h"

#include <sys/socket.h> // Per msqr
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_dl.h>

@interface UIDevice(Private)

- (NSString *) macaddress;

@end

@implementation UIDevice (IdentifierAddition)

////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Private Methods

// Return the local MAC addy
// Courtesy of FreeBSD hackers email list
// Accidentally munged during previous update. Fixed thanks to erica sadun & mlamb.
- (NSString *) macaddress{
    
    int                 mib[6];
    size_t              len;
    char                *buf;
    unsigned char       *ptr;
    struct if_msghdr    *ifm;
    struct sockaddr_dl  *sdl;
    
    mib[0] = CTL_NET;
    mib[1] = AF_ROUTE;
    mib[2] = 0;
    mib[3] = AF_LINK;
    mib[4] = NET_RT_IFLIST;
    
    if ((mib[5] = if_nametoindex("en0")) == 0) {
        printf("Error: if_nametoindex error\n");
        return NULL;
    }
    
    if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 1\n");
        return NULL;
    }
    
    if ((buf = malloc(len)) == NULL) {
        printf("Could not allocate memory. error!\n");
        return NULL;
    }
    
    if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
        printf("Error: sysctl, take 2");
        return NULL;
    }
    
    ifm = (struct if_msghdr *)buf;
    sdl = (struct sockaddr_dl *)(ifm + 1);
    ptr = (unsigned char *)LLADDR(sdl);
    NSString *outstring = [NSString stringWithFormat:@"%02X:%02X:%02X:%02X:%02X:%02X", 
                           *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
    free(buf);
    
    return outstring;
}

////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public Methods

- (NSString *) uniqueDeviceIdentifier{
    NSString *macaddress = [[UIDevice currentDevice] macaddress];
    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];  
    NSString *stringToHash = [NSString stringWithFormat:@"%@%@",macaddress,bundleIdentifier];
    NSString *uniqueIdentifier = [stringToHash stringFromMD5];  
    return uniqueIdentifier;
}

- (NSString *) uniqueGlobalDeviceIdentifier{
    NSString *macaddress = [[UIDevice currentDevice] macaddress];
    NSString *uniqueIdentifier = [macaddress stringFromMD5];    
    return uniqueIdentifier;
}

@end

NSString+MD5Addition.m:

#import "NSString+MD5Addition.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString(MD5Addition)

- (NSString *) stringFromMD5{
    
    if(self == nil || [self length] == 0)
        return nil;
    
    const char *value = [self UTF8String];
    
    unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(value, strlen(value), outputBuffer);
    
    NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
        [outputString appendFormat:@"%02x",outputBuffer[count]];
    }
    return [outputString autorelease];
}

@end
于 2011-10-30T17:43:52.917 回答
4

您可以通过设备 ID 进行操作:

[[UIDevice currentDevice] uniqueIdentifier] is guaranteed to be unique to each device.

您可以实施检查,以便每个设备 ID 只能投票一次。

但是,请记住,设备 ID 特定于每个设备,这意味着如果单个用户拥有多个设备(ipad、iphone、ipod touch 等),他们可以多次投票(每个设备一次)。

于 2011-10-30T17:08:00.560 回答