我正在撰写实施 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都是空的。
但是当模拟器启动时,它会因此错误崩溃:
&lt;&#;; NSInternalInconsistencyException&#39;,原因:&#39;无法加载NIB 捆绑:&#39; NSBundle&#39; &GT;用&lt;线程1:信号SIGABRT&gt;错误 Main.m。
我在互联网上搜索了很多来解决这个问题,但我无法解决。
那么,有谁可以帮助我吗?谢谢!
答案 0 :(得分:0)
如果您提到的故事板上有提到的名称,那么
你应该使用,
UIStoryboard *storyboardobj=[UIStoryboard storyboardWithName:@"LaunchScreen" bundle:nil];
self.viewController = [storyboardobj instantiateInitialViewController];
而不是
self.viewController = [[[ViewController alloc] initWithNibName:@"LaunchScreen.storyboard" bundle:nil] autorelease];