2016-05-30 49 views
0

我在寫一個iOS應用程序,它實現了Google Cloud Messaging'NSInternalInconsistencyException',原因:'無法在捆綁中加載NIB:'Xcode中的NSBundle

我想收到授權令牌並將其打印在屏幕上。

我安裝了所有必需的東西,並且我在YouTube上的教程之後編寫了代碼。

這是我的代碼:

AppDelegate.h

#import <UIKit/UIKit.h> 

@class ViewController; 

@interface AppDelegate : UIResponder <UIApplicationDelegate>; 

@property (strong, nonatomic) UIWindow *window; 

@property (strong, nonatomic) ViewController *viewController; 

@property (nonatomic) NSString *getton; 

@end 

AppDelegate.m

#import "AppDelegate.h" 
#import "ViewController.h" 
#import "GoogleCloudMessaging.h" 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize viewController = _viewController; 
@synthesize getton; 

- (void) dealloc { 
    [_window release]; 
    [_viewController release]; 
    [super dealloc]; 
} 

- (BOOL)application:(UIApplication *)application 
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    self.window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]] autorelease]; 
    [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
    [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    self.viewController = [[[ViewController alloc] initWithNibName:@"LaunchScreen.storyboard" bundle:nil] autorelease]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    [self registerDeviceToken: deviceToken]; 
} 

- (void) registerDeviceToken:(NSData *)deviceToken { 
    NSLog(@"Device Token: %@", deviceToken); 
    NSMutableString *string=[[NSMutableString alloc] init]; 
    int length=[deviceToken length]; 
    char const *bytes=[deviceToken bytes]; 
    for (int i=0; i<length; i++) { 
     [string appendString:[NSString stringWithFormat:@"%02.2hhx",bytes[i]]]; 
    } 
    NSLog(@"%@",string); 
    [self performSelectorInBackground:@selector(connectionWebRegister:)withObject:string]; 
    [string release]; 
} 

-(void) connectionWebRegister:(NSString *) deviceTokenString { 
    NSAutoreleasePool *pool = [NSAutoreleasePool new]; 
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://serviceProvider/registerTokenId?tokenId=%@&app=",deviceTokenString]]; 
    NSLog(@"APNS URL : %@",url); 
    NSData * res = [NSData dataWithContentsOfURL:url]; 
    getton=deviceTokenString; 
    if (res!=nil) { 
     NSString *response = [[NSString alloc] initWithBytes: [res bytes] lenght:[res length] encoding: NSUTF8StringEncoding]; 
     NSLog(@"%@", response); 
     [response release]; 
    } 
    [pool drain]; 
} 

- (void)application:(UIApplication *)app didReceiveRemoteNotification:(NSDictionary *)userInfo 
{ 
    NSLog(@"test"); 
    NSMutableDictionary * test = [userInfo objectForKey:@"aps"]; 
    UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"MESSAGE" 
                message:[test objectForKey:@"alert"] 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
}  
@end 

ViewController.h和ViewController.m都是空的。

但是模擬器啓動時,它崩潰,出現此錯誤:

<'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle' > with an < Thread 1: signal SIGABRT > error in Main.m .

我已經搜索互聯網上的很多解決這個問題,但我沒能解決它。

那麼,有沒有人可以幫助我?謝謝!然後

您應該使用

+0

你在主包項目有'LaunchScreen.storyboard' ? – Lion

+0

您不能像使用NIB文件一樣使用故事板文件。您需要獲取對故事板的引用,然後使用'instantiateViewControllerWithIdentifier' – Paulw11

回答

0

如果你在問題中提到的名字故事情節存在,

UIStoryboard *storyboardobj=[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil]; 

self.viewController = [storyboardobj instantiateInitialViewController]; 

,而不是

self.viewController = [[[ViewController alloc] initWithNibName:@"LaunchScreen.storyboard" bundle:nil] autorelease]; 
+0

謝謝,我解決了問題! – MatteoRK22

+0

不客氣.... :) – Lion

+0

那麼,是否比Objective-C更快捷?你呢? – MatteoRK22

相關問題