跳转到设置相应项 您所在的位置:网站首页 短信默认打开浏览器 跳转到设置相应项

跳转到设置相应项

#跳转到设置相应项| 来源: 网络整理| 查看: 265

原文网址:http://blog.csdn.net/james_1010/article/details/8556715

一,打开设置各项

5.0+后可在代码中调用如下代码打开设置相应界面: NSURL*url=[NSURL URLWithString:@”command stringI”]; [[UIApplication sharedApplication] openURL:url]; 即可跳转到设置页面的对应项:command string: Notification — prefs:root=NOTIFICATI_ID Phone — prefs:root=Phone Photos — prefs:root=Photos Profile — prefs:root=General&path=ManagedConfigurationList Reset — prefs:root=General&path=Reset Safari — prefs:root=Safari Siri — prefs:root=General&path=Assistant Sounds — prefs:root=Sounds Software Update — prefs:root=General&path=SOFTWARE_UPDATE_LINK Store — prefs:root=STORE Twitter — prefs:root=TWITTER Usage — prefs:root=General&path=USAGE VPN — prefs:root=General&path=Network/VPN Wallpaper — prefs:root=Wallpaper

Wi-Fi — prefs:root=WIFI

二,打开其他应用

我们来讨论一下,在iOS开发中,如何实现从app1打开app2。

基本的思路就是,可以为app2定义一个URL,在app1中通过打开这个URL来打开app2,在此过程中,可以传送一些参数。下面来讨论一下具体的实现过程。1. 在app2的info.plist中定义URL,就是在文件中添加URL types一项。可按下图进行添加。

更高版本xcode设置如下:

[identifier可不设]

2. 在app1的代码中打开刚才定义的URL,代码如下:

[cpp] view plaincopy

NSURL *url = [NSURL URLWithString:@"myapp://test?para1=1¶2=2"]; [[UIApplication sharedApplication] openURL:url]; 当然,这个URL的形式可以是其他形式的,只要以"myapp://"开始即可。这样,就可以在app1中打开app2.打开之后,会调用app2的AppDelegate的

[cpp] view plaincopy

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

由于URL是自己定义的,所以会存在URL重复的情况。经过测试发现,如果试图打开这个URL,那么会打开先安装的那个应用。

(如果没安装app2,则:

NSString *pLink = @”http://itunes.apple.com/cn/app//app21111111?mt=1“; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:pLink]];

)

三,打开浏览器

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”http://www.google.com/“]];

四,拨打电话

// 定义点击拨号按钮时的操作

(void)callAction{

NSString *number = @”“;// 此处读入电话号码

// NSString *num = [[NSString alloc]initWithFormat:@”tel://%@”,number]; //number为号码字符串 如果使用这个方法结束电话之后会进入联系人列表

NSString *num = [[NSString alloc]initWithFormat:@”telprompt://%@”,number]; //而这个方法则打电话前先弹框 是否打电话 然后打完电话之后回到程序中 网上说这个方法可能不合法 无法通过审核

[[UIApplication sharedApplication] openURL:[NSURLURLWithString:num]]; //拨号

}

// 第三种方式打电话

-(void)CallPhone{

NSString *phoneNum = @”“;// 电话号码

NSURL *phoneURL = [NSURL URLWithString:[NSStringstringWithFormat:@”tel:%@”,phoneNum]];

if ( !phoneCallWebView ) {

phoneCallWebView = [[UIWebView alloc]initWithFrame:CGRectZero];// 这个webView只是一个后台的容易 不需要add到页面上来 效果跟方法二一样 但是这个方法是合法的

}

不管是用电话应用本身拨打电话还是在第三方应用中拨打电话,只要在电话号码后加”,”就能直接拨打分机了;

例如在第三方应用中 添加下面的代码:

[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@”tel://01011112222,3333”]];

就能直接拨打分机了

五,发送短信

iOS4.0新加入了MFMessageComposeViewController和MFMessageComposeViewControllerDelegate,提供了发送短信的接口,可以像发送邮件那样不用跳出程序来发送短信. 介绍可参阅Message UI Framework Reference

一些笔记:

MFMessageComposeViewController

提供了操作界面使用前必须检查canSendText方法,若返回NO则不应将这个controller展现出来,而应该提示用户不支持发送短信功能.界面不能自行定制要发送的短信的内容(body)和收件人(recipients)在展现这个controller前需初始化好,展现了之后短信内容不能通过程序来进行修改.不过用户仍然可以手工修改短信内容和选择收件人用户点了发送或者取消,或者发送失败时,MFMessageComposeViewControllerDelegate 的– messageComposeViewController:didFinishWithResult:方法都能得到通知,在这里进行相应的处理

若在iOS3.0上运行的话,会提示dyld: Symbol not found: OBJC_CLASS$_MFMessageComposeViewController .解决方案:

MessageUI.framework的引入类型应选择weak(在target -> Get Info -> General -> Linked Libraries -> MessageUI.framework -> Type 里修改)不要在.h文件里直接import MessageUI/MFMessageComposeViewController.h,改为import

代码:

pragmamark - pragmamark SMS

-(IBAction)showSMSPicker:(id)sender { //The MFMessageComposeViewController class is only available in iPhone OS 4.0 or later. //So, we must verify the existence of the above class and log an error message for devices //running earlier versions of the iPhone OS. Set feedbackMsg if device doesn’t support //MFMessageComposeViewController API.Class messageClass = (NSClassFromString(@”MFMessageComposeViewController”));

if(messageClass !=nil) {//Check whether the current device is configured for sending SMS messagesif([messageClass canSendText]) {[self displaySMSComposerSheet];}else{[UIAlertView quickAlertWithTitle:@"设备没有短信功能"messageTitle:nil dismissTitle:@"关闭"];} } else{[UIAlertView quickAlertWithTitle:@"iOS版本过低,iOS4.0以上才支持程序内发送短信"messageTitle:nil dismissTitle:@"关闭"]; }

}

-(void)displaySMSComposerSheet { MFMessageComposeViewController *picker =[[MFMessageComposeViewController alloc] init]; picker.messageComposeDelegate =self;

NSMutableString* absUrl =[[NSMutableString alloc] initWithString:web.request.URL.absoluteString]; [absUrl replaceOccurrencesOfString:@"http://i.aizheke.com"withString:@"http://m.aizheke.com"options:NSCaseInsensitiveSearch range:NSMakeRange(0, [absUrl length])];picker.body=[NSString stringWithFormat:@"我在爱折客上看到:%@ 可能对你有用,推荐给你!link:%@",[web stringByEvaluatingJavaScriptFromString:@"document.title"],absUrl]; [absUrl release]; [self presentModalViewController:picker animated:YES]; [picker release];

}

(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {

switch(result) { caseMessageComposeResultCancelled: LOG_EXPR(@”Result: SMS sending canceled”); break; caseMessageComposeResultSent: LOG_EXPR(@”Result: SMS sent”); break; caseMessageComposeResultFailed: [UIAlertView quickAlertWithTitle:@”短信发送失败”messageTitle:nil dismissTitle:@”关闭”]; break; default: LOG_EXPR(@”Result: SMS not sent”); break; } [self dismissModalViewControllerAnimated:YES]; }

iOS中,应用A如果想打开应用B,需要在B工程的info.plist文件中添加URL types字段下面的URL identifier 和 URL Schemes 字段;

比如URL identifier 设置为:com.mycompany.testapp ; URL Schemes 设置为:launch;

那么在工程A中可以通过:

UIApplication *app = [UIApplicationsharedApplication];NSURL *url = [NSURLURLWithString:@"myapp://com.adsage"];[app openURL:url];

启动应用B。

同样的道理,利用 UIApplication 类的 - (BOOL)canOpenURL:(NSURL *)url 成员方法可以判断是否能启动应用B;

UIApplication *app = [UIApplicationsharedApplication];

NSURL *url = [NSURLURLWithString:@"myapp://com.adsage"];

if ([appcanOpenURL:url]) {

NSLog(@"can launch B app!");

}else {

NSLog(@"can not launch B app!");

}

如果能启动应用B,说明已经安装应用B,反之。

不过此方法必须知道应用B(被启动应用) 的URL types属性 。

在SDK中打开其他接入应用的解决方案

一直以来,在iOS的开发中,在程序中打开另外一个应用是不允许。后来有正义之士用class-dump在私有API中找到了这样的功能。那就是使用UIApplication的launchApplicationWithIdentifier:suspended:来打开。

使用的办法如下:

NSString *identifier = [[NSBundle mainBundle] objectForInfoDictionaryKey:@”CFBundleIdentifier”];

[[UIApplication sharedApplication] launchApplicationWithIdentifier:identifier suspended:NO];

毕竟是私有API不是一个好的办法,至少你永远都得不到App Store的认可。

在某些时候是其实我们可能还是需要这样的功能。作为一个SDK,其实还是有一种比较好的解决方案的。那就是使用UIApplication的openURL:的方法。

我们先来了解一下openURL和实现的方案。OpenURL其实是有很丰富的功能,除了简单的调用safari打开网站,还可有google地图搜索,Mail,拨打电话,发送短信,打开AppStore。

-(IBAction)openMaps {//打开地图 // Where is Apple on the map anyway? NSString* addressText = @”1 Infinite Loop, Cupertino, CA 95014″; // URL encode the spaces addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]; NSString* urlText = [NSString stringWithFormat:@”http://maps.google.com/maps?q=%@“, addressText]; // lets throw this text on the log so we can view the url in the event we have an issue NSLog(urlText); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]]; } -(IBAction)openEmail {//打开mail // Fire off an email to apple support [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”mailto://[email protected]”]]; } -(IBAction)openPhone {//拨打电话 // Call Google 411 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”tel://8004664411”]]; } -(IBAction)openSms {//打开短信 // Text to Google SMS [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”sms://466453”]]; } -(IBAction)openBrowser {//打开浏览器 // Lanuch any iPhone developers fav site [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@”http://itunesconnect.apple.com“]]; }

那怎样来制作从一个应用打开其他应用,这其实很简单,打开info.plist,添加一项URL types,展开URL types,再展开Item1,将Item1下的URL identifier修改为URL Scheme,展开URL Scheme,将Item1的内容修改为myapp其他程序可通过myapp://访问此自定义URL。

其实就是类似下面的样式。

这样就只要open这个应用的自定义url,系统就可以帮我们找到并打开这个程序。

NSURL *url = [NSURL URLWithString:@” myapp:”];

[[UIApplication sharedApplication] openURL:url];

作为SDK比普通应用的优势在于,每一个接入的应用都有一个AppId用于区分,我们就可以充分利用这个AppId来制作。

我们可以要求第三方开发者需要在他们Info.Plist中配置这样的字段,这样我们就可以在我们的SDK界面中打开对应AppId的应用,当然,这需要设备中真的有安装这个程序。

例如某应用分配AppId为111122223333,我们要求其再Info.plist定义URL Schemes为NDSDK111122223333,这样,我们在内部代码就可以准确识别是否有这样的程序。

更有甚者,我们可以通过canOpenURL这个方法来判断这台设备是否安装了这个应用,如果可以打开,返回YES,那应该是有安装这样的程序,不管是ipa还是Pxl的程序,应该都是没有问题的。

如果我们真的选择这样子做,那就需要在文档中说明清楚。但是需要注意的是,也许作为程序员,可能不是很喜欢看文档,也许你费尽心思写的文档他并没有看到。这时我们应该来一点强硬的手段,于是有了下面这段代码的功能。

1:检查用户是否配置了AppId

2:有没有准确配置Info的CFBundleURLSchemes字段

3:是不是可以正确打开。

// Check App ID:

// This is really a warning for the developer, this should not// happen in a completed appif (!kAppId) {UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Setup Error"message:@"Missing app ID. You cannot run the app until you provide this in the code."delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil,nil];[alertView show];[alertView release];} else {// Now check that the URL scheme fb[app_id]://authorize is in the .plist and can// be opened, doing a simple check without local app id factored in hereNSString *url = [NSString stringWithFormat:@"fb%@://authorize",kAppId];BOOL bSchemeInPlist = NO; // find out if the sceme is in the plist file.NSArray* aBundleURLTypes = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleURLTypes"];if ([aBundleURLTypes isKindOfClass:[NSArray class]] &&([aBundleURLTypes count] > 0)) {NSDictionary* aBundleURLTypes0 = [aBundleURLTypes objectAtIndex:0];if ([aBundleURLTypes0 isKindOfClass:[NSDictionary class]]) {NSArray* aBundleURLSchemes = [aBundleURLTypes0 objectForKey:@"CFBundleURLSchemes"];if ([aBundleURLSchemes isKindOfClass:[NSArray class]] &&([aBundleURLSchemes count] > 0)) {NSString *scheme = [aBundleURLSchemes objectAtIndex:0];if ([scheme isKindOfClass:[NSString class]] &&[url hasPrefix:scheme]) {bSchemeInPlist = YES;}}}}// Check if the authorization callback will workBOOL bCanOpenUrl = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString: url]];if (!bSchemeInPlist || !bCanOpenUrl) {UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Setup Error"message:@"Invalid or missing URL scheme. You cannot run the app until you set up a valid URL scheme in your .plist."delegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil,nil];[alertView show];[alertView release];}}

ios应用之间的调用步骤: 一、调用自己开发的应用

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

在xcode group&files 里面,展开 resources选择info.plist

鼠标右击information property list ,然后从列表中选择URL types

右击 add row 添加一个对象(item)右击item add row 从列表中选择 URL Schemes 再右击添加一个对象(item1)将item1得值设置为:myapp这个myapp就是对外接口,其它应用可以通过它,调用该应用

plist如下图所示:

2)调用方法

在你需要调用上面注册过对外接口的应用中,添加下面代码即可:

NSURL *url = [NSURL URLWithString:@”myapp:”];

[[UIApplication sharedApplication] openURL:url];

通过上述两个步骤,你可以在你的应用中,让用户打开你的其它应用

二、调用IOS自带的应用

上面讲述的是调用自身的应用,讲解了如何在自己应用之间调用问题,今天介绍一下如果调用IOS自带的app的方法

一、调用app store界面方法

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

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

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

二、调用其它应用的方法

1)调用 自带mail


[[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@”mailto://[email protected]”]];

2)调用 电话phone


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


3)调用 SMS


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


4)调用自带 浏览器 safari


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

5)调用 Remote


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

上面是最基本的语句,没什么处理过程。

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

三、官方原理讲解 和其它应用程序进行通讯

如果一个应用程序支持一些已知类型的URL,您就可以通过对应的URL模式和该程序进行通讯。然而,在大多数情况下,URL只是用于简单地启动一个应用程序并显示一些和调用方有关的信息。举例来说,对于一个用于管理地址信息的应用程序,您就可以在发送给它的URL中包含一个Maps程序可以处理的地址,以便显示相应的位置。这个级别的通讯为用户创造一个集成度高得多的环境,减少应用程序重新实现设备上其它程序已经实现的功能的必要性。

苹果内置支持http、mailto、tel、和sms这些URL模式,还支持基于http的、指向Maps、YouTube、和iPod程序的URL。应用程序也可以自己注册定制的URL模式。您的应用程序可以和其它应用程序通讯,具体方法是用正确格式的内容创建一个NSURL对象,然后将它传给共享UIApplication对象openURL:方法。openURL:方法会启动注册接收该URL类型的应用程序,并将URL传给它。当用户最终退出该应用程序时,系统通常会重新启动您的应用程序,但并不总是这样。系统会考虑用户在URL处理程序中的动作及在用户看来返回您的应用程序是否合理,然后做出决定。

下面的代码片断展示了一个程序如何请求另一个程序提供的服务(假定这个例子中的“todolist”是由应用程序注册的定制模式):

NSURL *myURL = [NSURL URLWithString:@”todolist://www.acme.com?Quarterly%20Report#200806231300”];

[[UIApplication sharedApplication] openURL:myURL];

重要提示:如果您的URL类型包含的模式和苹果定义的一样,则启动的是苹果提供的程序,而不是您的程序。如果有多个第三方的应用程序注册处理同样的URL模式,则该类型的URL由哪个程序处理是没有定义的。

如果您的应用程序定义了自己的URL模式,则应该实现对该模式进行处理的方法,具体信息在“实现定制的URL模式”部分中进行描述。有关系统支持的URL处理,包括如何处理URL的格式,请参见苹果的URL模式参考。 实现定制的URL模式

您可以为自己的应用程序注册包含定制模式的URL类型。定制的URL模式是第三方应用程序和其它程序及系统进行交互的机制。通过定制的URL模式,应用程序可以将自己的服务提供给其它程序。 注册定制的URL模式

在为您的应用程序注册URL类型时,必须指定CFBundleURLTypes属性的子属性,我们已经在“信息属性列表”部分中介绍过这个属性了。CFBundleURLTypes属性是应用程序的Info.plist文件中的一个字典数组,每个字典负责定义一个应用程序支持的URL类型。表1-6描述了CFBundleURLTypes字典的键和值。 表1-6 CFBundleURLTypes属性的键和值

CFBundleURLName

这是个字符串,表示URL类型的抽象名。为了确保其唯一性,建议您使用反向DNS风格的标识,比如com.acme.myscheme。

这里提供的URL类型名是一个指向本地化字符串的键,该字符串位于本地化语言包子目录中的InfoPlist.strings文件中。本地化字符串是人类可识别的URL类型名称,用相应的语言来表示。

CFBundleURLSchemes

这是个URL模式的数组,表示归属于这个URL类型的URL。每个模式都是一个字符串。属于指定URL类型的URL都带有它们的模式组件。

图1-7显示了一个正在用内置的Xcode编辑器编辑的Info.plist文件。在这个图中,左列中的URL类型入口相当于您直接加入到Info.plist文件的CFBundleURLTypes键。类似地,“URL identifier”和“URL Schemes”入口相当于CFBundleURLName和CFBundleURLSchemes键。

图1-7 在Info.plist文件中定义一个定制的URL模式 Defining a custom URL scheme in the Info.plist file

您在对CFBundleURLTypes属性进行定义,从而注册带有定制模式的URL类型之后,可以通过下面的方式来进行测试:

连编、安装、和运行您的应用程序。回到Home屏幕,启动Safari(在iPhone仿真器上,在菜单上选择Hardware > Home命令就可以回到Home屏幕)。在Safari的地址栏中,键入使用定制模式的URL。确认您的应用程序是否启动,以及应用程序委托是否收到application:handleOpenURL:消息。

处理URL请求

应用程序委托在application:handleOpenURL:方法中处理传递给应用程序的URL请求。如果您已经为自己的应用程序注册了定制的URL模式,则务必在委托中实现这个方法。

基于定制模式的URL采用的协议是请求服务的应用程序能够理解的。URL中包含一些注册模式的应用程序期望得到的信息,这些信息是该程序在处理或响应URL请求时需要的。传递给application:handleOpenURL:方法的NSURL对象表示的是Cocoa Touch框架中的URL。NSURL遵循RFC 1808规范,该类中包含一些方法,用于返回RFC 1808定义的各个URL要素,包括用户名、密码、请求、片断、和参数字符串。与您注册的定制模式相对应的“协议”可以使用这些URL要素来传递各种信息。

在程序清单1-2显示的application:handleOpenURL:方法实现中,传入的URL对象在其请求和片断部分带有具体应用程序的信息。应用程序委托抽出这些信息—在这个例子中,是指一个to-do任务的名称和到期日—并根据这些信息创建应用程序的模型对象。

程序清单1-2 处理基于定制模式的URL请求 [cpp] view plaincopy

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { if ([[url scheme] isEqualToString:@"todolist"]) { ToDoItem *item = [[ToDoItem alloc] init]; NSString *taskName = [url query]; if (!taskName || ![self isValidTaskString:taskName]) { // must have a task name [item release]; return NO; } taskName = [taskName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; item.toDoTask = taskName; NSString *dateString = [url fragment]; if (!dateString || [dateString isEqualToString:@"today"]) { item.dateDue = [NSDate date]; } else { if (![self isValidDateString:dateString]) { [item release]; return NO; } // format: yyyymmddhhmm (24-hour clock) NSString *curStr = [dateString substringWithRange:NSMakeRange(0, 4)]; NSInteger yeardigit = [curStr integerValue]; curStr = [dateString substringWithRange:NSMakeRange(4, 2)]; NSInteger monthdigit = [curStr integerValue]; curStr = [dateString substringWithRange:NSMakeRange(6, 2)]; NSInteger daydigit = [curStr integerValue]; curStr = [dateString substringWithRange:NSMakeRange(8, 2)]; NSInteger hourdigit = [curStr integerValue]; curStr = [dateString substringWithRange:NSMakeRange(10, 2)]; NSInteger minutedigit = [curStr integerValue]; NSDateComponents *dateComps = [[NSDateComponents alloc] init]; [dateComps setYear:yeardigit]; [dateComps setMonth:monthdigit]; [dateComps setDay:daydigit]; [dateComps setHour:hourdigit]; [dateComps setMinute:minutedigit]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDate *itemDate = [calendar dateFromComponents:dateComps]; if (!itemDate) { [dateComps release]; [item release]; return NO; } item.dateDue = itemDate; [dateComps release]; } [(NSMutableArray *)self.list addObject:item]; [item release]; return YES; } return NO; }

请务必对传入的URL输入进行验证。如果您希望了解如何避免URL处理的相关问题,请参见安全编码指南文档中的验证输入部分。如果要了解苹果定义的URL模式,请参见苹果的URL模式参考



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有