在我的應用程序我使用推送通知,在我看來,註冊推送請求需要一段時間,這阻止我的應用程序立即顯示內容,只顯示一個黑色的屏幕,直到設備令牌回來。註冊蘋果推送通知異步或在後臺
- (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