Ideally, I would like to send an HTTP Request using POST to the Push Notification Server that contains the device token as well as some user-defined settings. From there I can set up a php script on the server to deal with the incoming data and input it into an sql table. If this is the only way to do it, how would I go about initiating and HTTP Request from Objective C?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
You'll first need to convert the device token to a hex string with a function like this:
- (NSString*)stringWithDeviceToken:(NSData*)deviceToken {
const char* data = [deviceToken bytes];
NSMutableString* token = [NSMutableString string];
for (int i = 0; i < [deviceToken length]; i++) {
[token appendFormat:@"%02.2hhX", data[i]];
}
return [[token copy] autorelease];
}
Then you'll need to make a request to your server:
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://example.com/script.php?token=%@", DEVICE_TOKEN]];
NSMutableURLRequest* request = [[[NSMutableRequest alloc] initWithURL:url] autorelease];
NSURLConnection* connection = [NSURLConnection connectionWithRequest:request delegate: self];
回答2:
another way:
NSString * tokenAsString = [[[deviceToken description]
stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]]
stringByReplacingOccurrencesOfString:@" " withString:@""];