2015-05-22 77 views
2

我正在为现有的应用程序开发苹果手表扩展程序。从苹果手表发起呼叫

我的手表应用程序有联系我们部分,客户可以拨打免费电话号码。

我的问题是我如何开始在苹果手表上点击按钮而不是保持我的应用程序在前台并开始呼叫。

目前我使用此代码来开始通话

+ (void)callWithNumberWithoutPrompt:(NSString *)phoneNo { 
    NSString *prefixedMobileNumber = [phoneNo hasPrefix:@"+"]?phoneNo:[NSString stringWithFormat:@"+%@",phoneNo]; 
    NSString *phoneNumber = [@"tel://" stringByAppendingString:prefixedMobileNumber]; 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]]; 
} 

回答

3

注:这是在WatchOS 1真实的,并且可以与WatchOS 2.


释放从已经改变Ray Wenderlich WatchKit FAQ

Can third -party应用程序可以通过手表应用程序拨打电话?

编号没有公共API可让您直接从WatchKit扩展程序发起电话呼叫。由于伴侣iPhone应用程序无法带到前台,因此系统会默默忽略来自伴侣iPhone应用程序的所有电话或openURL:请求。

2

使用WatchOS2WatchConnectivity你可以从手表的应用程序将数据发送回父应用程序,然后尝试启动从父应用程序调用。这里有一个例子:

//App Delegate in iOS Parent App 

#pragma mark Watch Kit Data Sharing 

-(void)initializeWatchKit{ 
    if ([WCSession isSupported]){ 
     WCSession *session = [WCSession defaultSession]; 
     session.delegate = self; 
     [session activateSession]; 
    } 
} 

- (void)session:(WCSession *)session didReceiveMessage:(NSDictionary<NSString *, id> *)message{ 
    DLog(@"%@", message); 
    [self callRestaurantWithNumber:[NSString formattedPhoneNumber:[message valueForKey:@"phone_number"]]]; 
} 

-(void)callRestaurantWithNumber:(NSString *)formattedPhoneNumber{ 
    [[UIApplication sharedApplication] 
    openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", 
            formattedPhoneNumber]]]; 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 

    [self initializeWatchKit]; 

    return YES; 
} 

现在watchKit扩展中,你可以将数据发送回这样的父应用程序:

override func willActivate() { 
     super.willActivate() 

    if WCSession.isSupported() { 

     let defaultSession = WCSession.defaultSession() 
     defaultSession.delegate = self 
     defaultSession.activateSession() 
     if defaultSession.reachable == true { 
      let phoneNumberDict = [ "phone_number": "123-456-7890"] 

      defaultSession.sendMessage(phoneNumberDict, replyHandler: nil, errorHandler: { (error) -> Void in 
       print("THERE WAS AN ERROR SENDING DATA TO THE IOS APP: \(error.localizedDescription)") 
      }) 

     } 
    } 
} 

但是我这种方法遇到的一个限制是父母应用程序需要开放以实际接收数据并进行呼叫。该文档似乎指出,将应用程序从手表发送到ios父应用程序时将在后台打开。然而,在我的测试中,在真实设备(手表和iphone)和模拟器上,父应用程序仅在父级ios应用程序打开并预先登录时才接收数据。即使当ios父应用程序处于后台状态时,它似乎仍不会启动该呼叫。我在watch os2iOS 9.0.2上运行这个。

从另一个帖子,似乎也遇到了同样的问题。 Another watchKit SO post

链接到苹果的文档太: Apple Doc sendMessage: