【问题标题】:'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle in Xcode'NSInternalInconsistencyException',原因:'无法在包中加载 NIB:'Xcode 中的 NSBundle
【发布时间】:2016-05-30 09:19:59
【问题描述】:

我正在编写一个实现 Google Cloud Messaging 的 iOS 应用。

我想接收授权令牌并将其打印在屏幕上。

我安装了所有必要的东西,并按照 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 都是空的。

但是当模拟器启动时,它会因以下错误而崩溃:

带有 错误 主.m.

我在互联网上搜索了很多来解决这个问题,但我无法解决它。

那么,有人可以帮助我吗?谢谢!

【问题讨论】:

  • 你在主包的项目中有LaunchScreen.storyboard吗?
  • 您不能像使用 NIB 文件一样使用故事板文件。您需要获取对故事板的引用,然后使用instantiateViewControllerWithIdentifier

标签: ios objective-c iphone xcode google-cloud-messaging


【解决方案1】:

如果您提到的故事板存在问题,那么

你应该使用,

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

self.viewController = [storyboardobj instantiateInitialViewController];

而不是

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

【讨论】:

  • 那么,swift 比 Objective-C 更好吗?你呢?
  • 两者都有一些优点和缺点,但 swift 是为克服目标 c 的限制而开发的新技术,所以你可以说得更好。 You should only discuss original post related discussion in comments. If you have another type of query then you should ask separate question
猜你喜欢
  • 2012-09-25
  • 1970-01-01
  • 2015-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
  • 2011-07-21
相关资源
最近更新 更多