我已经阅读了很多这方面的教程,我只是想知道这是否是正确的方法
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSLog(@"My token is: %@", deviceToken);
NSString* newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *urlString = [NSString stringWithFormat:@"http://myhost.com./filecreate.php?token=%@",newToken];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
}
欢迎任何建议。
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(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]];
}
NSString *urlString = [NSString stringWithFormat:@"http://myhost.com/filecreate.php?token=%@",token];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSData *urlData;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
}
我的应用程序可以使用这两种代码,但正确的方法是什么?
最佳答案
正如mja在评论中所说,最好添加错误恢复机制(如果请求失败,您将丢失 token )并使用异步请求(在后台发送 token )
在您的 AppDelegate.m 中:
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
{
NSString * token = [NSString stringWithFormat:@"%@", deviceToken];
//Format token as you need:
token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
token = [token stringByReplacingOccurrencesOfString:@">" withString:@""];
token = [token stringByReplacingOccurrencesOfString:@"<" withString:@""];
[[NSUserDefaults standardUserDefaults] setObject:token forKey:@"apnsToken"]; //save token to resend it if request fails
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"apnsTokenSentSuccessfully"]; // set flag for request status
[DataUpdater sendUserToken]; //send token
}
要发送 token ,请创建新类(或使用现有类之一):
DataUpdater.h
#import <Foundation/Foundation.h>
@interface DataUpdater : NSObject
+ (void)sendUserToken;
@end
DataUpdater.m
#import "DataUpdater.h"
@implementation DataUpdater
+ (void)sendUserToken {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"apnsTokenSentSuccessfully"]) {
NSLog(@"apnsTokenSentSuccessfully already");
return;
}
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myhost.com/filecreate.php?token=%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"apnsToken"]]]; //set here your URL
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if (error == nil) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"apnsTokenSentSuccessfully"];
NSLog(@"Token is being sent successfully");
//you can check server response here if you need
}
}];
}
@end
然后,当互联网连接出现时,您可以在 Controller 中调用[DataUpdater sendUserToken];,或者定期调用,或者在-(void)viewDidLoad或-( void)viewWillAppear 方法
我的建议:
1)我使用AFNetworking发送异步请求并检查服务器 JSON 响应
2) 有时最好使用 Parse 等服务。使用推送通知
关于iphone - 将设备 token 发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7482514/