一. 打开自定义的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(协议),为自定义字符串
直接上图
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: