在safari打开WXApp
注册了URL Schemes的应用,用safari浏览器也是可以打开的。我就经常用这个来验证应用是否设置了我想要的URL Schemes
在safari打开WXApp,直接在safari的地址栏输入weixin://,enter就可以打开了
版本大于等于iOS9的适配问题
- (IBAction)buttonAction:(UIButton *)sender {
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"weixin://"]]) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"weixin://"] options:@{} completionHandler:^(BOOL success) {
}];
}else{
NSLog(@"没有安装微信");
}
}
如果没有安装微信,但是控制台会报错,
这是因为ios9以后,苹果对权限进行了加强,必须把urlschemes加入plist的白名单中,
备注:这个受影响的只是canOpenURL,openURL并不影响
我们需要在MyApp的info.plist里面将weixin设置为白名单。
步骤:点击info.plist->右键->Open As->Source Code->添加下面的代码
LSApplicationQueriesSchemes
weixin
使用URL Schems传递数据
URL Schemes除了可以用来打开APP之外,还可以用来在两个App之间传递少量的数据。
而且,在openURL的时候,如果url中带有参数,只要URL Schemes是正确的,那同样可以打开App,而且,后面的参数也会带到我们打开的App那里。
咱们做个Demo就一目了然了。
在MyApp中,url为weixin://www.jianshu.com/u/a8ee38cc2571?title=title&content=helloWorld
- (IBAction)buttonAction:(UIButton *)sender {
if ([[UIApplication sharedApplication]canOpenURL:[NSURL URLWithString:@"weixin://www.jianshu.com/u/a8ee38cc2571?title=title&content=helloWorld"]]) {
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"weixin://www.jianshu.com/u/a8ee38cc2571?title=title&content=helloWorld"] options:@{} completionHandler:^(BOOL success) {
}];
}else{
NSLog(@"没有安装微信");
}
}
然后可以在另外一个app中就可以检测到时通过urlSchems打开的
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url NS_DEPRECATED_IOS(2_0, 9_0, "Please use application:openURL:options:") __TVOS_PROHIBITED;
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation NS_DEPRECATED_IOS(4_2, 9_0, "Please use application:openURL:options:") __TVOS_PROHIBITED;
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options NS_AVAILABLE_IOS(9_0); // no equiv. notification. return NO if the application can't open for some reason
// Keys for application:openURL:options:
UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsSourceApplicationKey NS_AVAILABLE_IOS(9_0); // value is an NSString containing the bundle ID of the originating application
UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsAnnotationKey NS_AVAILABLE_IOS(9_0); // value is a property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property
UIKIT_EXTERN NSString *const UIApplicationOpenURLOptionsOpenInPlaceKey NS_AVAILABLE_IOS(9_0); // value is a bool NSNumber, set to YES if the file needs to be copied before use