2012-02-22 56 views
3

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

- (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是什麼問題?

親切的問候閱讀

+0

你真的需要發送同步請求命中服務器? – iosfanboy9 2012-02-23 06:18:32

+0

@ krusty43可能不是。我應該嘗試異步NSURLConnection?在我調用'initWithNibName'之後的 – spankmaster79 2012-02-23 14:36:09

回答

1

developer.apple.com,registerForRemoteNotificationTypes是異步的:

When you send this message, the device initiates the registration process with Apple Push Service. If it succeeds, the application delegate receives a device token in the application:didRegisterForRemoteNotificationsWithDeviceToken: method; if registration doesn’t succeed, the delegate is informed via the application:didFailToRegisterForRemoteNotificationsWithError: method.

可能是你做的不便耗時在委託的回調?

+0

它究竟在哪裏聲明這是異步的?通過提到應用程序委託被調用?嗯..實際上我在我的UIWebView中加載一個站點,但是webview已經有一個backgroundcolor ...所以這應該是可見的 – spankmaster79 2012-02-22 10:57:13

+1

該設備_initiates_註冊過程與蘋果推送服務。如果將NSLog輸出放在registerForRemoteNotificationTypes後面並放入應用程序委託中的回調中,則會看到第一個NSLog比didRegisterForRemoteNotificationsWithDeviceToken/didFailToRegisterForRemoteNotificationsWithError的輸出早出現。 – 2012-02-22 11:05:25

1

將您的代碼-(BOOL)application:didFinishLaunchingWithOptions:,它應該工作的罰款。

+0

? – spankmaster79 2012-02-22 10:55:32

2

註冊在AppDelegate中- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions通知和

嘗試實施這些方法,

// one of these will be called after calling -registerForRemoteNotifications 
- (void)application:(UIApplication *)application 
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken 
- (void)application:(UIApplication *)application 
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error 
- (void)application:(UIApplication *)application didReceiveRemoteNotification: 
(NSDictionary *)userInfo 
+0

thx,已經實現了方法 – spankmaster79 2012-02-22 11:07:28

+0

你在'application:didFinishLaunchingWithOptions:'中註冊了APNS嗎? 在scrren變黑之前調用了'didFailToRegisterForRemoteNotificationsWithError'?你使用斷點測試過嗎? – iosfanboy9 2012-02-22 11:21:43

+0

我在'initWithNibName'註冊過,但是現在在'didFinishLaunchingWithOptions'中做了,並且'didFailToRegisterForRemoteNotificationsWithError'沒有被調用。我確實得到了設備令牌,但在該請求之前屏幕保持黑屏 – spankmaster79 2012-02-22 13:44:07