in my app I use push notifications and it seems to me that the request to register for push takes a while, which stops my app from immediatly displaying content and only showing a black screen until the device token comes back.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
....
NSLog(@"Registering for push notifications...");
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeAlert |
UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound)];
....
return self;
}
How can I do this asynchrounus or in the background.. because it confuses users if the screen stays black…
regards
UPDATE: as requested my code
- (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;
}
And then in 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];
}
and in my 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];
}
hope that helps… could Reachabilit also be the problem?
Kind Regards for reading
According developer.apple.com, registerForRemoteNotificationTypes IS asynchronous:
May be you do smth time consuming in callbacks of delegate?