2015-05-19 2 views
0

Я пытаюсь запланировать локальное оповещение стрелять через несколько минут после кнопка нажата на Apple, Watch (я тестирование на реальном устройстве). Это, как я пытаюсь это:Trigger Уведомление от компании Apple Watch

- (IBAction)buttonPressed { 
    [WKInterfaceController openParentApplication: nil reply: nil { 
} 

И в AppDelegate:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply { 

} 

Проблема, я не знаю, куда поместить код для уведомления. Я уверен, что мне нужно вызвать openParentApplication, чтобы запланировать его, но не знаю, как это сделать. Я читал по всему Интернету, но ничего не нашел.

ответ

0

Хорошо, ты почти там:

первого вызова, как это:

- (IBAction)buttonPressed { 
    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init] 
[dictionary setObject:@"notification" forKey:@"action"]; 

    [MainInterfaceController openParentApplication:dictionary reply:^(NSDictionary *replyInfo, NSError *error) { 
//handle the reply if you want to... 
} 
} 

Затем в AppDelegate:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply 
     { 
    if([[userInfo objectForKey:@"action"] isEqualToString:@"notification"]){ 

     //fire the notification 
     UILocalNotification* localNotification = [[UILocalNotification alloc] init]; 
      localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:3]; 
      localNotification.alertBody = @"Your alert message"; 
      localNotification.timeZone = [NSTimeZone defaultTimeZone]; 
      [[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

// you can use Reply(Dictinary); for responding to watchKit extention OK or KO... 
     } 

Не забудьте попросить разрешения на уведомление в вашем AppDelegate:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Override point for customization after application launch. 


    UIUserNotificationType types = UIUserNotificationTypeBadge | 
    UIUserNotificationTypeSound | UIUserNotificationTypeAlert; 

    UIUserNotificationSettings *mySettings = 
    [UIUserNotificationSettings settingsForTypes:types categories:nil]; 

    [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; 

    return YES; 
} 
+0

Спасибо! Это прекрасно работает. – Tony

Смежные вопросы