Skip to content

Instantly share code, notes, and snippets.

@Kursulla
Last active December 19, 2015 19:29
Show Gist options
  • Save Kursulla/6006802 to your computer and use it in GitHub Desktop.
Save Kursulla/6006802 to your computer and use it in GitHub Desktop.
Linkify text
/*It is common request by client to enable opening links in strings in your application. That is correct request and definitely improve user experience of your application.
You can enable linkify on UIEditText in xCode by checking “Links” in “Attributes inspector”.
This will guide user to the system browser that will open clicked URL.
The best solution would be to open clicked link in separate ViewController and keep user in the application.
Here, came in hand small amount of code written by nbuggia.
Details on http://mycode.restservices.org/2013/06/10/linkify-edit-text-with-custom-viewcontroller/
This post is explanation how to implement this lib.
1. Enable links in Attributes Inspector for desired UITextView. If you are working from code, tan you need to add following code for your UITextView instance
*/
[textViewInstance setEditable:NO];
[textViewInstance setDataDetectorTypes:UIDataDetectorTypeLink];
/*
2. Download code and place it somewhere in your project. Please, don’t just place it antwhere, Create directory and group for all external code you are using in your project. Prevent creating mess in your project.
3. In your AppDelegate add following method
*/
-(BOOL)openURL:(NSURL*)url{
BrowserViewController *bvc = [[BrowserViewController alloc] initWithUrls:url];
_window = [[UIApplication sharedApplication] keyWindow];
UIViewController *rootViewController = [_window rootViewController];
UINavigationController *navCon=[[UINavigationController alloc]initWithRootViewController:bvc];
[rootViewController presentModalViewController:navCon animated:YES];
}
// or if you are using NavigationViewController use
-(BOOL)openURL:(NSURL*)url{
BrowserViewController *bvc = [[BrowserViewController alloc] initWithUrls:url];
[self.window.rootViewController.navigationController pushViewController:bvc animated:YES];
[rootViewController presentModalViewController:bvc animated:YES];
}
/*
You are placing this method in order to enable this feature all across of your application. You can place it in you ViewController if you would like to keep it locale only for specific ViewController.
4. And final step is to change main.m in of your project and there replace
*/
return UIApplicationMain(argc, argv, nil, NSStringFromClass([CAppDelegate class]));
with
/*
return UIApplicationMain(argc, argv, NSStringFromClass([CustomUIApplication class]), NSStringFromClass([CAppDelegate class]));
You can see that we set principal class from nill to CustomUIApplication. This new class you can find in files you downloaded in step 2.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment