我们可以推出一个"链接"在iMessage应用程序?

时间:2018-04-24 00:54:11

标签: ios imessage imessage-extension

我有一个iMessage应用程序,基本上我想显示一个按钮,按下后,启动另一个应用程序,如Safari或地图等。

我试过了:

如果UIApplication.shared.canOpenURL(url){

但" UIApplication.shared"仅适用于iOS主应用程序。我是一个仅限iMessage的应用程序。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用extensionContext子类中的MSMessagesAppViewController属性打开以下网址:

NSURL *url = [NSURL URLWithString:@"https://www.apple.com/"];
[self.extensionContext openURL:url completionHandler:nil];

我还没有对此进行测试,但我使用此方法使用自定义网址方案从我的iMessage扩展程序中启动我的iOS应用程序。我也使用它来从我的iMessage扩展中启动AppStore。

更新

由于你说你只想打开地图,你可以试试这个:

#import <MapKit/MapKit.h>

- (void)openMaps {
    CLLocationCoordinate2D coordinates = CLLocationCoordinate2DMake(33.651092f, -117.744250f); // coordinates of your desired location
    MKCoordinateRegion regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, 5000, 5000); // 5000 is the distance in meters
    NSDictionary *options = @{MKLaunchOptionsMapCenterKey: [NSValue valueWithMKCoordinate:regionSpan.center],
                              MKLaunchOptionsMapSpanKey: [NSValue valueWithMKCoordinateSpan:regionSpan.span]};
    MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coordinates addressDictionary:nil];
    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
    [mapItem setName:@"Irvine Spectrum Center"];
    [mapItem openInMapsWithLaunchOptions:options];
}

如果有效,请告诉我。