在我的应用程序我使用推送通知,在我看来,注册推送请求需要一段时间,这阻止我的应用程序立即显示内容,只显示一个黑色的屏幕,直到设备令牌回来。注册苹果推送通知异步或在后台
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
....
NSLog(@"Registering for push notifications...");
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
....
return self;
}
我怎样才能做到这一点asynchrounus或在后台..因为它,如果屏幕保持黑色混淆用户...
问候
更新:根据要求我的代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.webViewCon = [[[WebViewController alloc] initWithNibName:@"WebView_iPhone" bundle:[NSBundle mainBundle]] autorelease];
application.applicationIconBadgeNumber = 0;
[window addSubview:[webViewCon view]];
[self.window makeKeyAndVisible];
// register for push
NSLog(@"Registering for push notifications...");
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
return YES;
}
然后在didRegisterForRemoteNotificationsWithDeviceToken
- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSString *str = [NSString stringWithFormat:@"Device Token=%@",deviceToken];
NSLog(@"%@",str);
// get token & udidi
NSString* newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
NSString *udid = [UIDevice currentDevice].uniqueIdentifier;
// send request to server to save token
NSString *urlString = [NSString stringWithFormat:@"https://my.server.com/service/token?token=%@&udid=%@",newToken, udid];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[url release];
NSURLResponse *response;
[NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil];
}
,并在我的WebViewController.m
- (void)viewDidLoad
{
// create and send POST request
NSURL *url = [NSURL URLWithString:myUrlAddress];
NSString *udid = [UIDevice currentDevice].uniqueIdentifier;
NSString *requestString = [NSString stringWithFormat:@"udid=%@", udid];
NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];
NSMutableURLRequest *requestObj = [[NSMutableURLRequest alloc] initWithURL:url];
[requestObj setHTTPMethod:@"POST"];
[requestObj setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[requestObj setHTTPBody: requestData];
NSURLResponse *response;
NSError *err;
[NSURLConnection sendSynchronousRequest: requestObj returningResponse:&response error:&err];
// set delegate
webView.delegate = self;
// set backgroundcolor
[webView setOpaque:NO];
[webView setBackgroundColor:RGB(154, 148, 131)];
// check internet and load
NetworkStatus currentStatus = [[Reachability reachabilityForInternetConnection]
currentReachabilityStatus];
if(currentStatus != NotReachable) {
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"No Connection"
message:@"Check your Wi-Fi connection and restart the App."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
[alert show];
[alert release];
}
// prevent scrolling up & down
[[[webView subviews] lastObject] setScrollEnabled:NO];
[super viewDidLoad];
}
希望帮助...也可以Reachabilit是什么问题?
亲切的问候阅读
你真的需要发送同步请求命中服务器? – iosfanboy9 2012-02-23 06:18:32
@ krusty43可能不是。我应该尝试异步NSURLConnection?在我调用'initWithNibName'之后的 – spankmaster79 2012-02-23 14:36:09