2015-05-22 76 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: