can't dial call from programmatically in iphone

佐手、 提交于 2020-01-06 08:13:28

问题


I have done call programmatically like this.

NSString *phoneNumber = [[self.arrCategory objectAtIndex:0]objectForKey:@"MOBILE"]; 

NSString *phoneURLString = [NSString stringWithFormat:@"tel:%@", phoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
[[UIApplication sharedApplication] openURL:phoneURL];

but I can't do call from iphone. What is the problem?


回答1:


now just replace this code with your code , may be its occurred because in phoneno or phone url somewhere whitespace is used so its not called..use this code..

    NSString *phoneNumber = [[self.arrCategory objectAtIndex:0]objectForKey:@"MOBILE"];
    NSArray* telComponents = [phoneNumber componentsSeparatedByCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet]];
    phoneNumber = [telComponents componentsJoinedByString: @""];

    NSString* urlString = [NSString stringWithFormat: @"tel:%@", phoneNumber];
    NSURL* telURL = [NSURL URLWithString: urlString];

    if ( [[UIApplication sharedApplication] canOpenURL: telURL] )
    {
        [[UIApplication sharedApplication] openURL: telURL];
    }
    else
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle: NSLocalizedString( @"Dialer Error", @"" ) 
                                                        message: [NSString stringWithFormat: NSLocalizedString( @"There was a problem dialing %@.", @"" ), phoneNumber] 
                                                       delegate: nil 
                                              cancelButtonTitle: NSLocalizedString( @"OK", @"" ) 
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
    }

UPDATE:

try this also...

#import "RegexKitLite.h"

NSString * number = @"(555) 555-555 Office";
NSString * strippedNumber = [number stringByReplacingOccurrencesOfRegex:@"[^0-9]" withString:@""];

see the link for this code from iphone sdk - Remove all characters except for numbers 0-9 from a string




回答2:


Try this & check:

 NSString *phoneNumber = [[self.arrCategory objectAtIndex:0]objectForKey:@"MOBILE"]; 
    NSString *phoneURLString = [@"tel://" stringByAppendingString:phoneNumber];
    NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
    [[UIApplication sharedApplication] openURL:phoneURL];



回答3:


Please try my sample code from here,

http://yuvarajmanickam.wordpress.com/2012/03/03/make-a-call-from-iphone-app/

Please use your phone number istead of phoneNumber string. Please let me know if it is useful for you. I hope it will help you. Thanks.




回答4:


Try this:

UIWebView *callWebview = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 0, 0)];
[self.view addSubview:callWebview];
NSString *phoneNumber = [[self.arrCategory objectAtIndex:0]objectForKey:@"MOBILE"]; 
    NSString *phoneWithoutSpaces = [[NSString stringWithFormat:@"tel://%@", phoneNumber] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSURL *telURL = [NSURL URLWithString:phoneWithoutSpaces];
[callWebview loadRequest:[NSURLRequest requestWithURL:telURL]];

It is working for me.



来源:https://stackoverflow.com/questions/14136786/cant-dial-call-from-programmatically-in-iphone

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!