打電話發短信等常用小功能


打電話-方法3

創建一個UIWebView來加載URL,撥完后能自動回到原應用

if(_webView==nil) {

_webView= [[UIWebViewalloc]initWithFrame:CGRectZero];

}

[_webViewloadRequest:[NSURLRequestrequestWithURL:[NSURLURLWithString:@"tel://10010"]]];

撥號之前會彈框詢問用戶是否撥號,撥完后能自動回到原程序.

注意:這個webView千萬不要設置尺寸,不然會擋住其他界面,他只是用來打電話,不需要顯示


發短信-方法2

如果想指定短信內容,那就得使用MessageUI框架,包含主頭文件


顯示發短信的控制器(不會跳出當前控制器)

MFMessageComposeViewController*vc = [[MFMessageComposeViewControlleralloc]init];

設置短信內容

vc.body=@"吃飯了沒?";

設置收件人列表

vc.recipients=@[@"10010",@"02010010"];

設置代理

vc.messageComposeDelegate=self;

顯示控制器

[selfpresentViewController:vcanimated:YEScompletion:nil];

代理方法,當短信界面關閉的時候調用,發完后會自動回到原應用

#pragma mark MFMessageComposeViewControllerDelegate

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

? ? switch(result) {

? ? ? ? case MessageComposeResultCancelled:

? ? ? ? ? ? NSLog(@"取消發送!");

? ? ? ? ? ? break;

? ? ? ? case MessageComposeResultSent:

? ? ? ? ? ? NSLog(@"發送成功!");

? ? ? ? ? ? break;

? ? ? ? case MessageComposeResultFailed:

? ? ? ? ? ? NSLog(@"發送失敗!");

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? break;

? ? }

? ? [controllerdismissViewControllerAnimated:YES completion:nil];

}

郵件發送后的代理方法回調,發完后會自動回到原應用

發郵件方法二

/**

?*注釋:發送郵件,不會跳出當前應用

?*/

- (void)sendeEmail

{

? ? MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];

? ? mailPicker.mailComposeDelegate = self;

? ? [mailPickersetSubject:@"eMail主題"];

? ? //NSArray *toRecipients = [NSArray arrayWithObject: @"first@example.com",@"2@qq.com"];

? ? NSArray*toRecipients =@[@"1@qq.com",@"2@qq.com"];

? ? [mailPickersetToRecipients: toRecipients];

? ? [self presentViewController:mailPicker animated:YES completion:nil];


}

#pragma mark MFMailComposeViewControllerDelegate

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error

{

? ? if(error) {

? ? ? ? NSLog(@"error :%@",error);

? ? }

? ? if (result == MFMailComposeResultSent) {


? ? }

? ? switch(result) {

? ? ? ? case MFMailComposeResultCancelled:

? ? ? ? ? ? NSLog(@"取消發送!");

? ? ? ? ? ? break;

? ? ? ? case MFMailComposeResultFailed:

? ? ? ? ? ? NSLog(@"發送失?。?);

? ? ? ? ? ? break;

? ? ? ? case MFMailComposeResultSaved:

? ? ? ? ? ? NSLog(@"保存發送!");

? ? ? ? ? ? break;

? ? ? ? case MFMailComposeResultSent:

? ? ? ? ? ? NSLog(@"發送成功!");

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? break;

? ? }

? ? [controllerdismissViewControllerAnimated:YES completion:nil];

}

打開其他常見文件

如果想打開一些常見文件,比如html、txt、PDF、PPT等,都可以使用UIWebView打開.只需要告訴UIWebView文件的URL即可.至于打開一個遠程的共享資源,比如http協議的,也可以調用系統自帶的Safari瀏覽器:

NSURL*url = [NSURLURLWithString:@”http://www.baidu.com"];

[[UIApplicationsharedApplication]openURL:url];

打開AppStore(跳出當前應用)

為了提高應用的用戶體驗,經常需要邀請用戶對應用進行評分.應用評分無非就是跳轉到AppStore展示自己的應用,然后由用戶自己撰寫評論.如何跳轉到AppStore,并且展示自己的應用:

NSString*appid =@"725296055”;

//https://itunes.apple.com/cn/app/wang-yi-xin-wen/id425349261?mt=8

NSString*str = [NSStringstringWithFormat:

@"itms-apps://itunes.apple.com/cn/app/id%@?mt=8", appid];

[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:str]];

打開AppStore(不跳出當前應用)


/**

?*注釋:打開AppStore,不會跳出當前應用

?*/

- (void)sendeAppStore{

? ? SKStoreProductViewController *viewController =[[SKStoreProductViewController alloc] init];

? ? viewController.delegate=self;

? ? //這里的425349261是某一個應用的id(每一個用用程序都會配一個id)

? ? //這個id怎么獲取:打開iTunes,找到某一個應用(點進去),在應用的旁邊有一個按鈕,點擊按鈕有個復制鏈接,把鏈接復制出來,里面有一個id。

? ? NSDictionary *dict = [NSDictionary dictionaryWithObject:@"425349261" forKey:SKStoreProductParameterITunesItemIdentifier];

? ? [viewControllerloadProductWithParameters:dictcompletionBlock:^(BOOLresult,NSError*error) {

? ? ? ? if(result) {

? ? ? ? ? ? NSLog(@"加載成功");

? ? ? ? }

? ? ? ? if(error) {

? ? ? ? ? ? NSLog(@"-------%@",error);

? ? ? ? }

? ? }];

? ? [self presentViewController:viewController animated:nil completion:nil];

}

- (void)productViewControllerDidFinish:(SKStoreProductViewController*)viewController{

? ? [viewControllerdismissViewControllerAnimated:YES completion:nil];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容