应用之间调用 UIApplication类的OpenURL方法

一、UIApplication类的OpenURL方法

1、调用app store界面方法

在实际开发中,往往要推荐自己其他应用和推荐自己的收费软件,那么我们就需要在程序中直接连接到app store的相应页面。

实际上的做法很简单,使用的还是UIApplication类的OpenURL方法: 

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"程序的相应连接"]];

 

2、调用其它应用的方法

// 调用 自带mail

[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"mailto://admin@hzlzh.com"]];

// 调用 电话phone

[[UIApplication sharedApplication] openURL:[NSURLURLWithString:@"tel://8008808888"]];

// 调用 SMS

[[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"sms://800888"]];

// 调用自带 浏览器 safari

[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"http://www.hzlzh.com"]];

 

// 调用 Remote

[[UIApplicationsharedApplication] openURL:[NSURL URLWithString:@"remote://fff"]];

调用phone可以传递号码,调用SMS只能设定号码,不能初始化SMS内容。

 

二、应用程序间通信

1、调在plist文件中,注册对外接口

找到appname-info.plist文件,点击打开它,在列表中找到URL types(如果没有,可添加一个URL types )里面的内容设置看下图:

应用之间调用 UIApplication类的OpenURL方法 - Awei - 半调子程序员

 

关键Key和Value是 URL identifier => com.zilanxing.andpush;URL Schemes => andpush

到此对外接口已注册好

2、调用上面的接口通信

注册url地址:andpush://cn.andpush.com/access/register?code=zOdSpC8ZXv

// 通过iOS浏览器打开应用程序注册应用

// andpush用户注册,当用户邮箱收到邀请链接,直接点击下面链接就可以打开andpush应用程序的注册页面

<a href="andpush://cn.andpush.com/access/register?code=zOdSpC8ZXv">andpush://cn.andpush.com/access/register?code=zOdSpC8ZXv</a>

// 通过其它App应用打开andpush应用程序的注册页面

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"andpush://cn.andpush.com/access/register?code=zOdSpC8ZXv"]];

3、 andpush 响就注册页面的请求

在AndPush应用的AppDelegate.m文件中添加如下代码:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url

{

    if (!url) {

        return NO;

    }

    NSLog(@"handleOpenURL: %@", [url absoluteString]);

    // 在host等于cn.andpush.com时,说明是注册请求

    if ([[url host] isEqualToString:@"cn.andpush.com"]) {

        // 请求的url地址: andpush://cn.andpush.com/access/register?code=zOdSpC8ZXv

        // 获取注册邀请码 code

        NSString *code = [[url query] substringFromIndex:[[url query] rangeOfString:@"code="].location+5];

        NSLog(@"code: %@", code);

        // 使用本地 ViewController 来注册

        UserRegisterViewController *viewController = [[[UserRegisterViewControlleralloc] init] autorelease];

        viewController.title = @"注册";

        viewController.code = code;

        //从底部显示视图

        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController];

        navigationController.title = @"注册";

        [self.tabBarController presentModalViewController:navigationController animated:TRUE];

        [navigationController release];

        // [self.navigationController pushViewController:controller animated:YES];

        // [controller release];

        return YES;

    } else {

        return NO;

    }

}

到此搞定,以后再完善。

其它参考:

http://www.cocoachina.com/newbie/tutorial/2012/0529/4302.html

http://www.byywee.com/page/M0/S753/753470.html

http://www.cocoachina.com/bbs/simple/?t58388.html

转自http://blog.163.com/wzi_xiang/blog/static/659829612012910103458775/ 

原文地址:https://www.cnblogs.com/mumoozhu/p/4502907.html