iphone打开文本视图中的超连接显示在网页视图中。opening links in a UITextView in a web view

当设定了文本内容链接高亮事件监听后,UIApplication将对点击事件做出responser,比如调用safari处理http文本,如果要改变这种响应方法怎么做

Method1、

使用类别override UITextView的webView:decidePolicyForNavigationAction:request:frame:decisionListener:方法。


@interface UITextView (CommonOverrid)
@end

@class WebView, WebFrame;
@protocol WebPolicyDecisionListener;

@implementation UITextView (CommonOverrid)

- (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id < WebPolicyDecisionListener >)listener
{
NSLog(@"request: %@", request);
UIWebView *v1 = [[UIWebView alloc] initWithFrame:CGRectMake(0, self.superview.frame.size.height - 200, 764, 200)];
[v1 loadRequest:request];
[((UIView *)(self.superview)) addSubview:v1];
}


UITextView增加了类别,重写了方法。但必须注意这是一个privateAPI。如果内部框架调整可能导致实效。

Method2、

重写UIApplication openURL方法

@implementation UIApplication (Private)

- (BOOL)openURL:(NSURL*)url
{
//current delegate
MyAppDelegate *watcher = [[UIApplication sharedApplication] delegate];
//current controller,use it to handleURL the action
if (watcher.currentViewController) {
//do something here....
[watcher.currentViewController handleURL:url];
return YES;
}
return NO;
}

@end


还有个实现不通过重写方法,而是先命名别称然后交换方法实现。swap implementations between separate methods.

Method customOpenUrl = class_getInstanceMethod([UIApplication class], @selector(customOpenURL:));
Method openUrl = class_getInstanceMethod([UIApplication class], @selector(openURL:));
method_exchangeImplementations(customOpenUrl, openUrl);

替换:

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */

    Method customOpenUrl = class_getInstanceMethod([UIApplication class], @selector(customOpenURL:));
    Method openUrl = class_getInstanceMethod([UIApplication class], @selector(openURL:));
    
    method_exchangeImplementations(openUrl, customOpenUrl);	
}

取消:
- (void)appOpenURL:(NSURL*)url
{
    Method customOpenUrl = class_getInstanceMethod([UIApplication class], @selector(customOpenURL:));
    Method openUrl = class_getInstanceMethod([UIApplication class], @selector(openURL:));
    
    method_exchangeImplementations(customOpenUrl, openUrl);
    
    [[UIApplication sharedApplication] openURL:url];
}

详见地址:https://github.com/marksands/UITextViewLinkOptions


iphone调用系统电话、浏览器、地图、邮件等

openURL的使用方法:[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appString]];
其中系统的appString有:
1.Map http://maps.google.com/maps?q=Shanghai 
2.Email mailto://myname@google.com 
3.Tel tel://10086 
4.Msg sms://10086 

openURL能帮助你运行Maps,SMS,Browser,Phone甚至其他的应用程序。这是iPhone开发中我经常需要用到的一段代码,它仅仅只有一行而已。

//打开地图

- (IBAction)openMaps {
NSString*addressText = @"beijing"; //@"1Infinite Loop, Cupertino, CA 95014"; 
addressText =[addressText stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
// 中文
// addressText = [addressText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString*urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@",addressText]; 
NSLog(@"urlText=============== %@", urlText); 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
}

//打开mail

- (IBAction)openEmail { 

[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];

 }


//拨打电话

- (IBAction)openPhone {

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

}


//打开短信

- (IBAction)openSms { 

[[UIApplication sharedApplication] openURL:[NSURLURLWithString:@"sms://466453"]];

}


//打开浏览器

-(IBAction)openBrowser { 

[[UIApplication sharedApplication] openURL:[NSURLURLWithString:@"http://itunesconnect.apple.com"]]; 

}


This code generates the App Store link on iOS

NSString *appName = [NSString stringWithString:[[[NSBundle mainBundle] infoDictionary]   objectForKey:@"CFBundleName"]];
NSURL *appStoreURL = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.com/app/%@",[appName stringByReplacingOccurrencesOfString:@" " withString:@""]]];

Replace itms-apps with http on Mac:

NSURL *appStoreURL = [NSURL URLWithString:[NSString stringWithFormat:@"http:/itunes.com/app/%@",[appName stringByReplacingOccurrencesOfString:@" " withString:@""]]]; 

Open URL on iOS:

[[UIApplication sharedApplication] openURL:appStoreURL];

Mac:

[[NSWorkspace sharedWorkspace] openURL:appStoreURL];

http://stackoverflow.com/questions/433907/how-to-link-to-apps-on-the-app-store




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值