Thursday 31 July 2014

Common Error of iOS : 

1. xcode timed out waiting for app to launch:

Reason 1. : It turned out I was using my Distribution Profile instead of the Developer Profile in Debug mode. You can check the Profile you are using by going to Build Settings->Code Signing Entity.
Make sure you are using your Developer Profile in Beta and Debug mode.

Reason 2. : I had this problem when I used a Distribution certificate instead of Developer one when running the app from Xcode. You may check it out your target --> Build Settings --> Code signing.

Check Below Links:

Tuesday 29 July 2014

UIWebView in Objective C :

Loading Request in Webview

To load a request you can use the below methods.

1. requestWithURL is used to load a URL in Webview


NSMutableURLRequest * request =[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]];
[webview loadRequest:request];

2. loadHTMLString is used load HTML string in UIWebView
You can use the below code to load HTML string in webview



NSString * string = @"<html><body><h1>Hayageek</h1>How to <a href=\"/add-custom-fonts-ios/\">Add Custom Font</a>";
[webview loadHTMLString:string baseURL:[NSURL URLWithString:@"http://hayageek.com"]];

3. loadData method is used to Load NSData in webview. Documents like .doc,.pdf,.xls,.txt,…etc are opened with UIWebview.



NSString * path =[[NSBundle mainBundle] pathForResource:@"Resume" ofType:@"doc"];
NSData *data =[[NSFileManager defaultManager] contentsAtPath:path];

[webview loadData:data MIMEType:@"application/msword" textEncodingName:@"UTF-8"baseURL:nil];

To get the list of MIME types:http://webdesign.about.com/od/multimedia/a/mime-types-by-content-type.htm

UIWebViewDelegate methods.

In order to get UIWebViewDelegate callbacks, you need to add UIWebViewDelegate protocol to your class and set the webview delegate class.

ViewController.h Source:

@interface ViewController : UIViewController<UIWebViewDelegate>{
}
@property (weak, nonatomic) IBOutlet UIWebView *webview;
@end

 








ViewController.m Source:

@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    [webview setDelegate:self];
}
@end
 


UIWebViewDelegate has the following delegate methods.

 webView:shouldStartLoadWithRequest:navigationType: method is called before loading a frame. If the method returns YES, request is loaded. Otherwise NO.
Using this method, requests can be filtered.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
   NSLog(@"Loading URL :%@",request.URL.absoluteString);

    //return FALSE; //to stop loading
    return YES;
}






Filtering Clicks, Form Submission,Ads:

//To avoid link clicking.
if(navigationType ==UIWebViewNavigationTypeLinkClicked)
return  NO;

//To avoid form submission.
if(navigationType ==UIWebViewNavigationTypeFormSubmitted)
return  NO;

//To avoid loading all the google ads.
if([request.URL.absoluteString rangeOfString:@"googleads"].location != NSNotFound)
    return NO;





webViewDidStartLoad method is called when a frame starts loading.
- (void)webViewDidStartLoad:(UIWebView *)webView
{

}



webViewDidFinishLoad method is called when a frame finishes loading.
- (void)webViewDidStartLoad:(UIWebView *)webView
{

}





webView:didFailLoadWithError: method is called if frame loading is failed.
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"Failed to load with error :%@",[error debugDescription]);

}




Execute JavaScript in UIWebview.

You can execute javascript programmatically in WebView using the method stringByEvaluatingJavaScriptFromString.
Examples:
//Show Cookie as alert in WebView
[webview stringByEvaluatingJavaScriptFromString:@"alert(document.cookie)"];

//Get Cookie
NSString * cookie=[webview stringByEvaluatingJavaScriptFromString:@"document.cookie"];
NSLog(@"cookie =%@",cookie);

//Get sum of 2+3
NSString * result1 =[webview stringByEvaluatingJavaScriptFromString:@"2+3"];
NSLog(@"2+3:%ld",(long)[result1 integerValue]);

//Redirect the current page in WebView
[webview stringByEvaluatingJavaScriptFromString:@"location.href=\"http://apple.com\""];
 



4).How to add a UIWebview to View Programmatically

UIWebview is added to a View programmatically using the below code.

- (void)viewDidLoad
{
    [super viewDidLoad];

    //set desired width and height
    UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(10, 100, 300, 300)];
    [myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];
    [self.view addSubview:myWebView];

}



Wednesday 9 July 2014

Dotted line border around UIView:

Its a Very Easy..

[yourView.layer setBorderWidth:5.0]; 
 
///just add image name and create image with dashed or doted drawing and add here 
[yourView.layer setBorderColor:[[UIColor colorWithPatternImage:
                [UIImage imageNamed:@"DotedImage.png"]] CGColor]]; 

Here, 
1. You just required add QuartzCore/QuartzCore.h Framework in project and 
2. import it like bellow in .m file
  #import <QuartzCore/QuartzCore.h>
 
Hope this help you...