我正在为现有的iPhone应用程序创建一个Apple Watch应用程序。 iPhone App具有拨打电话的功能。我正在使用以下代码来实现这一目标,
NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
我不知道如何在iWatch中实现这个功能。请提出一些建议来实现上述结果。
更新
目前我正在使用以下代码,
iWatch扩展代码:
- (IBAction)onCallNumberclicked {
NSDictionary *phoneNUmberDict = @{@"phoneNumber" : self.detailPlaceVO.formattedPhoneNumber};
[WKInterfaceController openParentApplication:phoneNUmberDict reply:^(NSDictionary *replyInfo, NSError *error) {
if (!error && replyInfo && [[replyInfo valueForKey:@"status"] boolValue] == true) {
NSLog(@"call success");
} else {
NSLog(@"call Failure");
}
}];
}
iPhone App Appdelegate代码:
- (void)application:(UIApplication *)application handleWatchKitExtensionRequest:(NSDictionary *)userInfo reply:(void(^)(NSDictionary *replyInfo))reply {
if (userInfo != nil && [userInfo valueForKey:@"phoneNumber"]) {
NSLog(@"PhoneNUmber : %@", [userInfo valueForKey:@"phoneNumber"]);
NSString *cleanedString = [[[userInfo valueForKey:@"phoneNumber"] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
BOOL isSuccess = false;
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
isSuccess = true;
}
NSMutableDictionary *replyDict = [NSMutableDictionary new];
[replyDict setValue:[NSNumber numberWithBool:isSuccess] forKey:@"status"];
reply(replyDict);
} }
我不知道这段代码是否适用于实时构建。请给出你的建议。