2015-05-02 18 views
14

Durum: ana uygulamasında handleWatchKitExtensionRequest aramaya İzle uygulamasında openParentApplication kullanın. Bu, simülatörde güzel çalışır ve aynı zamanda iPhone uygulaması aktif/açıkken gerçek cihazlarda (Apple Watch ve iPhone) çalışır.nasıl doğru şekilde denir "() cevap" "openParentApplication" ve "handleWatchKitExtensionRequest" kullanıyoruz?

Sorun: gerçek cihazlarda (Apple Ürünü ve iPhone) çalıştırdığınızda ana iPhone uygulaması açık/aktif değilken, handleWatchKitExtensionRequestopenParentApplication verileri döndürmez.

Kod WatchKit Uzantısı'ndaki InterfaceController.m içinde: iPhone'da ana app app temsilci

NSDictionary *requst = @{ @"request" : @"getData" }; 
[InterfaceController openParentApplication:requst 
            reply:^(NSDictionary *replyInfo, NSError *error) { 
             // do something with the returned info 
            }]; 

Kodu:

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply 
{ 
    if ([[userInfo objectForKey:@"request"] isEqualToString:@"getData"]) 
    { 
    // get data 
    // ... 
    reply(data); 
    } 
} 

cevap

16

zaman ana uygulama iPhone etkin değil, reply() erişilemiyor çünkü arka plan görevi daha önce işletim sistemi tarafından öldürülüyor.

çözüm documentation belirtildiği gibi açıkça handleWatchKitExtensionRequest bir arka plan görevi başlamaktır. Arka plan görevi bu şekilde başlatılırsa, 180 saniyeye kadar çalışabilir. Bu, iPhone'daki ana uygulamanın yanıtını göndermeden önce askıya alınmamasını sağlar. iPhone'da ana app app temsilci

Kod: Eğer asynchroneously yöntem uğramadan (hemen dönmez emin olmak için aşağıdaki yöntemi uygulayabilirsiniz, veri getirmek için gereken durumda

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply 
{ 
    __block UIBackgroundTaskIdentifier watchKitHandler; 
    watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask" 
                   expirationHandler:^{ 
                   watchKitHandler = UIBackgroundTaskInvalid; 
                   }]; 

    if ([[userInfo objectForKey:@"request"] isEqualToString:@"getData"]) 
    { 
     // get data 
     // ... 
     reply(data); 
    } 

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 1), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler]; 
    }); 
} 

yanıt):

- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void (^)(NSDictionary *))reply 
{ 
    __block UIBackgroundTaskIdentifier watchKitHandler; 

    watchKitHandler = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"backgroundTask" 
                   expirationHandler:^{ 
                    watchKitHandler = UIBackgroundTaskInvalid; 
                   }]; 

    NSMutableDictionary *response = [NSMutableDictionary dictionary]; 

    dispatch_semaphore_t sema = dispatch_semaphore_create(0); 

    [ClassObject getDataWithBlock:^(BOOL succeeded, NSError *error){ 

     if (succeeded) 
     { 
      [response setObject:@"update succeded" forKey:@"updateKey"]; 
     } 
     else 
     { 
      if (error) 
      { 
       [response setObject:[NSString stringWithFormat:@"update failed: %@", error.description] forKey:@"updateKey"]; 
      } 
      else 
      { 
       [response setObject:@"update failed with no error" forKey:@"updateKey"]; 
      } 
     } 

     reply(response); 
     dispatch_semaphore_signal(sema); 
    }]; 

    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); 

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)NSEC_PER_SEC * 1), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
    [[UIApplication sharedApplication] endBackgroundTask:watchKitHandler]; 
    }); 
} 
+0

Simülatörde test etmenin herhangi bir yolu var mı? Benim gerçek saatim olmadığı için. @vomako –

+0

Kod ayrıca bir simülatörde çalışmalıdır. – vomako

+0

S: Watch uygulaması iPhone uygulamasının arka planında çalıştırıldığında simülatörde neden düzgün çalışıyor? –

İlgili konular