ios openUrl:options: 自定义app的启动和系统app的启动

一. 打开自定义的app

原理:

1. 需要自定义的app 向系统注册自定义的url协议

2. 其他app通过去调用这个app的url,打开它

3. app收到启动请求

1.注册

在app的info.list中

添加键值对: URL types ,是个Array类型,其下是个Dictionary

第一个元素中,key叫URL identifier,它是app向系统注册的id, 全局唯一标识,一般为app的bundled的反写

再手动加一个元素,添加键值对: URL Schemes,值是Array类型.

Array的第一个元素是自定义URL的scheme(协议),为自定义字符串

直接上图


URL结构

2. 调用


在其他app中写:

leturl =NSURL(string:"ivy://params?param1=1&m2=2") // 这个ivy就是上一步中的scheme

if (UIApplication.shared.canOpenURL(url) ){

UIApplication.shared.open(url, options: ["key1":"value1"]) { (b) in

}

}

在widget编程中这么写:

leturl =NSURL(string:"ivy://params?param1=1&m2=2")// 这个ivy就是上一步中的scheme

self.extensionContext?.open(urlas!URL, completionHandler: { (b) in

})

3.接收

在被调用的自定义app的AppDelegate里面,实现函数:

funcapplication(_app:UIApplication, open url:URL, options: [UIApplicationOpenURLOptionsKey:Any] = [:]) ->Bool

代码:

funcapplication(_app:UIApplication, open url:URL, options: [UIApplicationOpenURLOptionsKey:Any] = [:]) ->Bool{

print("\(url),options:\(options)")

print("url:\(url.scheme), host:\(url.host), path:\(url.path), query:\(url.query)")

return true

}

这里有第二步的调用中, 传入的完整url和options

对url可以根据需要进行进一步的处理

二. 开打系统的app

一般常用的是四个:

1.Email发邮件 

mailto:foo@example.com?cc=bar@example.com&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!

2. Phone打电话    

tel://10086   

tel:1-408-555-5555

3. FaceTime Conversation 视频聊天 

facetime-audio://user@example.com

facetime-audio://user@example.com

4. Text Messages短信   

sms://10086

5. Maps 地图   

http://maps.google.com/maps?q=Shanghai

6. iTunes    

http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewAlbum?i=156093464&id=156093462&s=143441

7. YouTube Videos    

http://www.youtube.com/v/VIDEO_IDENTIFIER


我们直接用UIApplication.shared.open()方法调起就可以了.

这里讲下邮件和短信的注意事项

注意:

对于他们,openurl这种方式,是ios3之前的启动方式

现在MessageUI.framework提供了发邮件和发短信的原生界面嵌入app方式,似乎更好

用的时候要在target 的 General 的 Linked Frameworks and Libraries中加上这个库哟

1. 发邮件

发邮件如今有个能内置的原生界面:MFMailComposeViewController .我们可以presentViewController把它加入我们的界面,一定要实现它的代理MFMailComposeViewControllerDelegate,并手动写dismissViewControllerAnimated

它的用法直接上代码:

- (void)displayMailComposerSheet

{

MFMailComposeViewController*picker = [[MFMailComposeViewControlleralloc]init];

picker.mailComposeDelegate=self;

[pickersetSubject:@"Hello from California!"];

// Set up recipients

NSArray*toRecipients = [NSArrayarrayWithObject:@"first@example.com"];

NSArray*ccRecipients = [NSArrayarrayWithObjects:@"second@example.com",@"third@example.com",nil];

NSArray*bccRecipients = [NSArrayarrayWithObject:@"fourth@example.com"];

[pickersetToRecipients:toRecipients];

[pickersetCcRecipients:ccRecipients];

[pickersetBccRecipients:bccRecipients];

// Attach an image to the email

NSString*path = [[NSBundlemainBundle]pathForResource:@"rainy"ofType:@"jpg"];

NSData*myData = [NSDatadataWithContentsOfFile:path];

[pickeraddAttachmentData:myDatamimeType:@"image/jpeg"fileName:@"rainy"];

// Fill out the email body text

NSString*emailBody =@"It is raining in sunny California!";

[pickersetMessageBody:emailBodyisHTML:NO];

[selfpresentViewController:pickeranimated:YEScompletion:NULL];

}

这是代理的方法 用户点击了取消按钮,发送按钮等等,都会进入这个方法

- (void)mailComposeController:(MFMailComposeViewController*)controller

didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

{

self.feedbackMsg.hidden=NO;

// Notifies users about errors associated with the interface

switch(result)

{

caseMFMailComposeResultCancelled:

self.feedbackMsg.text=@"Result: Mail sending canceled";

break;

caseMFMailComposeResultSaved:

self.feedbackMsg.text=@"Result: Mail saved";

break;

caseMFMailComposeResultSent:

self.feedbackMsg.text=@"Result: Mail sent";

break;

caseMFMailComposeResultFailed:

self.feedbackMsg.text=@"Result: Mail sending failed";

break;

default:

self.feedbackMsg.text=@"Result: Mail not sent";

break;

}

[selfdismissViewControllerAnimated:YEScompletion:NULL];

}

2. 发短信

发短信和发email是很类似的

如今有个能内置的原生界面:MFMessageComposeViewController .我们可以presentViewController把它加入我们的界面,强制实现它的代理MFMessageComposeViewControllerDelegate,并手动写

它的用法直接上代码:

dismissViewControllerAnimated

- (void)displaySMSComposerSheet

{

MFMessageComposeViewController*picker = [[MFMessageComposeViewControlleralloc]init];

picker.messageComposeDelegate=self;

// You can specify one or more preconfigured recipients.The user has

// the option to remove or add recipients from the message composer view

// controller.

/* picker.recipients = @[@"Phone number here"]; */

// You can specify the initial message text that will appear in the message

// composer view controller.

//ivy add

picker.recipients=@[@"13044143671"];

picker.body=@"Hello from California!";

[selfpresentViewController:pickeranimated:YEScompletion:NULL];

}

这是代理的方法 用户点击了取消按钮,发送按钮等等,都会进入这个方法

- (void)messageComposeViewController:(MFMessageComposeViewController*)controller

didFinishWithResult:(MessageComposeResult)result

{

self.feedbackMsg.hidden=NO;

// Notifies users about errors associated with the interface

switch(result)

{

caseMessageComposeResultCancelled:

self.feedbackMsg.text=@"Result: SMS sending canceled";

break;

caseMessageComposeResultSent:

self.feedbackMsg.text=@"Result: SMS sent";

break;

caseMessageComposeResultFailed:

self.feedbackMsg.text=@"Result: SMS sending failed";

break;

default:

self.feedbackMsg.text=@"Result: SMS not sent";

break;

}

[selfdismissViewControllerAnimated:YEScompletion:NULL];

}


参考:

https://developer.apple.com/library/content/featuredarticles/iPhoneURLScheme_Reference/Introduction/Introduction.html   这是官方关于所有原生url的链接格式

http://www.cnblogs.com/langtianya/p/4052882.html  发送邮件

http://www.2cto.com/kf/201401/274753.html  打开自定义app

demon:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,757评论 2 45
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,088评论 19 139
  • 原文链接http://www.cnblogs.com/kenshincui/p/4186022.html 音频在i...
    Hyman0819阅读 22,044评论 4 74
  • 这个日子越来越不重要,再也没有人特意等到凌晨给你说生日快乐,也基本没有人再记得你生日,大家都挺忙的,不是么,我一点...
    西柚阿亘阅读 1,351评论 0 0
  • 原来天马行空的想象也可以锻炼记忆,怪不得自己平常记忆力这么差,平常都懒得想象了,脑子都生锈了…… 妈妈联想:爸爸 ...
    打伞看日出阅读 3,153评论 1 1

友情链接更多精彩内容